交互式图形的Matplotlib设置,用于保存出版物和报告中的图像

3

我曾编写过一个Python函数,可以修改任何当前交互式图形的标签和刻度标签文本大小。然后删除顶部和右侧轴以获得更喜欢的图表格式进行输出。接着它输出这些图表。但我现在遇到了一个问题,就是如何设置坐标轴的大小,以便保持xlabel在图形的画布上,并且在输出时显示出来。我知道手动创建轴时可以用下面的方法实现:

ax = plt.axes([0.125,0.2,0.95-0.125,0.95-0.2])

有没有办法在图像绘制之后设置这些参数?如果可以,我该如何访问或修改它呢?(还可以在matplotlibrc文件中设置吗?)

另一个问题与图例有关:

我通常使用以下一组图例参数,但是如何在事后设置其他规格?我只找到了下面的规格:

legArgs = dict(bbox_to_anchor=[.44,1.18], borderpad=0.1, labelspacing=0,
               handlelength=1.8, handletextpad=0.05, frameon=False, ncol=5,
                columnspacing=0.02)
               #ncol,numpoints,columnspacing,title,bbox_transform,prop
leg = ax.legend(tuple(legendLabels),tuple(modFreq),'upper center',**legArgs)
leg.get_title().set_fontsize(tick_size)
leg.set_frame_on(False)
leg.set_bbox_to_anchor([1.1,1.05])
for i in leg.get_texts():
    i.set_fontsize(8)

完整的绘图函数包括辅助函数:
def plot_for_publication_output(fig, ax, filename, dir_addition='', 
                                fulldir=None, column_width=1, dpi=600,
                                out_formats=['.eps','.pdf','.png']):
    """
    column_width assumes a standard 2 column width format (default 1 column,
    2 meaning spreads across both columns)
    """
    if fulldir == None:
        store_dir = os.path.expanduser('~/'+'Dropbox/Research/Results/' + dir_addition)
    else:
        store_dir = fulldir

    spineLineWidth = 0.5
    tick_size = 9
    fontlabel_size = 10.5
    mydpi = dpi
    outExt = out_formats
    dashs = ['-.', '-', '--', ':']
    dashes = [(1.5, 0), (3.5, 1.5), (1.5, 1.5, 3,1.5), (1, 1)]
    plot_color = "bgrkcykw"
    fig_width_pt = 246.0 * column_width       # Get this from LaTeX using
                                              # \showthe\columnwidth
    inches_per_pt = 1.0 / 72.27               # Convert pt to inches
    golden_mean = (np.sqrt(5) - 1.0) / 2.0    # Aesthetic ratio
    fig_width = fig_width_pt * inches_per_pt  # width in inches
    fig_height = fig_width * golden_mean      # height in inches
    fig.set_size_inches(fig_width, fig_height)
    plotSetAxisTickLabels(ax, tick_size)
    plotSetAxisLabels(ax, fontlabel_size)
    #params = {'axes.labelsize': fontlabel_size, 'text.fontsize': fontlabel_size,
    #          'legend.fontsize': fontlabel_size, 'xtick.labelsize': tick_size,
    #          'ytick.labelsize': tick_size, 'text.usetex': True,
    #          'figure.figsize': fig_size}
    #plt.rcParams.update(params)
    #ax = plt.axes([0.125, 0.2, 0.95 - 0.125, 0.95 - 0.2])
    set_spineLineWidth(ax,spineLineWidth)
    clear_spines(ax)
    ax.yaxis.set_ticks_position('left')
    ax.xaxis.set_ticks_position('bottom')
    for i in outExt:
        plt.savefig(os.path.join(store_dir, filename) + i, dpi = mydpi)

def clear_spines(ax):
    ax.spines['top'].set_color('none')
    ax.spines['right'].set_color('none')
def set_spineLineWidth(ax, lineWidth):
    for i in ax.spines.keys():
        ax.spines[i].set_linewidth(lineWidth)
def showOnlySomeTicks(x, pos):
    s = str(int(x))
    if x == 5000:
        return    '5e3'#'%.0e' % x
    return ''
def plotSetAxisTickLabels(ax, size=16):
    for i in ax.xaxis.get_ticklabels():
        i.set_fontsize(size)
    for i in ax.yaxis.get_ticklabels():
        i.set_fontsize(size)

def plotSetAxisLabels(ax, size=16):
    ax.xaxis.get_label().set_fontsize(size)
    ax.yaxis.get_label().set_fontsize(size)

请查看 https://dev59.com/SWgv5IYBdhLWcg3wCcWZ#10927146 以了解坐标轴的定位。 - bmu
1个回答

3
您可以使用set_position函数更改轴的位置:
# save the instance 
ax = plt.axes([0.125,0.2,0.95-0.125,0.95-0.2])

# change the axes position
ax.set_position([0.52, 0.85, 0.16, 0.075])

上述命令的文档在matplotlib axes documentation中找到。
您还可以在API中的图例中找到图例实例的函数,这可能是您已经发现的,或者您可以在子对象的文档中查找,您可以以与此行相同的方式调用子函数。
leg.get_title().set_fontsize(tick_size)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接