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)
4

1 回答 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 轴文档中找到。

您还可以在 API 中的 legend 中找到图例实例的函数,您可能已经找到了,或者在子对象的文档中四处寻找,您也可以以与此行相同的方式调用子函数:

leg.get_title().set_fontsize(tick_size)
于 2012-06-26T13:12:44.647 回答