Matplotlib设置用于保存交互式图表中的出版物和报告数据

时间:2012-06-26 06:43:59

标签: python numpy matplotlib python-2.7

我编写了一个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)

1 个答案:

答案 0 :(得分: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

中找到

您还可以在legend in the API中找到您可能找到的图例实例的函数,或者在子对象的文档中查找,您也可以像调用子对象一样调用子函数有这条线:

leg.get_title().set_fontsize(tick_size)