python - matplotlib 的 x 位置

标签 python matplotlib plot

现在我正在使用 python 绘制一些图。当 xtick 太长时,x 标签在绘图之外。如下所示:enter image description here

我知道我们可以在通过命令 plt.savefig("plot_name",bbox_inches='tight') 保存图形时调整它,但是如果我在编译时制作多个图,这将改变图形的大小在 latex 中,如下所示。

enter image description here

我希望我所有的图都具有相同的大小,然后当我在 latex 中编译时,它们将具有相同的大小。

有没有人可以给我一些提示来解决这个问题?非常感谢。

制作这些图的代码是:(这个发布的代码与上面的图不完全相同,因为我不能发布那个。但这两个几乎相同,唯一的区别是一个有一个for循环做几个数字,另一个枚举所有数字。)

params = {'backend': 'ps',
      'font.size': 30,
      'font.style': 'normal',
      'axes.labelsize': 30,
      #'text.fontsize': 30,
      'axes.linewidth': 2,
      'legend.fontsize': 12,
      'xtick.labelsize': 25,
      'ytick.labelsize': 25,
      'text.usetex': True,
      'ps.usedistiller': 'xpdf'}

rcParams.update(参数)

for i in range(len(sepa_step)):
if i == len(sepa_step) - 1:
    continue
if i % 3 == 0:        ###########  calculating average flux
    stack_flux1 = stack[i] / stack_num[i]
    stack_flux2 = stack[i+1] / stack_num[i+1]
    stack_flux3 = stack[i+2] / stack_num[i+2]

    f, (ax1, ax2, ax3) = plt.subplots(3, sharex=True, sharey=False,figsize=(10,20))
    ax1.plot(wave_alpha,stack_flux1,'-b',linewidth=4)
    ax1.set_ylabel(r'$10^{-17} \ {\rm erg} \ {\rm cm}^{-2} \ {\rm s}^{-1} \ {\rm \AA}^{-1}$',fontsize = 30)
    ax1.tick_params('both', length=10, width=2, which='major')
    ax1.tick_params('both', length=5, width=2, which='minor')
    ax1.axvline(x=6562.81, linewidth=4, color='r',linestyle='--')

    ax2.plot(wave_alpha,stack_flux2,'-b',linewidth=4)
    ax2.set_ylabel(r'$10^{-17} \ {\rm erg} \ {\rm cm}^{-2} \ {\rm s}^{-1} \ {\rm \AA}^{-1}$',fontsize = 30)
    ax2.tick_params('both', length=10, width=2, which='major')
    ax2.tick_params('both', length=5, width=2, which='minor')
    ax2.axvline(x=6562.81, linewidth=4, color='r',linestyle='--')

    ax3.plot(wave_alpha,stack_flux3,'-b',linewidth=4)
    ax3.set_ylabel(r'$10^{-17} \ {\rm erg} \ {\rm cm}^{-2} \ {\rm s}^{-1} \ {\rm \AA}^{-1}$',fontsize = 30)
    ax3.set_xlabel(r'$\lambda$ ($\AA$)',fontsize = 30)
    ax3.tick_params('both', length=10, width=2, which='major')
    ax3.tick_params('both', length=5, width=2, which='minor')
    ax3.axvline(x=6562.81, linewidth=4, color='r',linestyle='--')

    title1 = 'separation ' + str(sepa_step[i]) + '$\sim$' + str(sepa_step[i+1]) + ' mpc'
    title2 = 'separation ' + str(sepa_step[i+1]) + '$\sim$' + str(sepa_step[i+2]) + ' mpc'
    title3 = 'separation ' + str(sepa_step[i+2]) + '$\sim$' + str(sepa_step[i+3]) + ' mpc'

    ax1.set_title(title1,fontsize=20)
    ax2.set_title(title2,fontsize=20)
    ax3.set_title(title3,fontsize=20)
    #f.subplots_adjust(hspace=0.5)
    plot_name = './plots_fiton/separation_' + str(i/3 + 1) + '.eps'
    #plt.subplots_adjust(left=0.25, right=0.9, top=0.95, bottom=0.05)
    plt.savefig(plot_name,bbox_inches='tight')

最佳答案

您可以在 matplotlibrc file 中调整所有绘图参数包括边距的大小。在 Linux 上,该文件通常位于 ~/.config/matplotlib/matplotlibrc 中。该文件不是默认创建的,所以我建议 copying this template然后修改它。

您可能对以下设置感兴趣:

# The figure subplot parameters.  All dimensions are a fraction of the
# figure width or height
figure.subplot.left    : 0.125  # the left side of the subplots of the figure
figure.subplot.right   : 0.9    # the right side of the subplots of the figure
figure.subplot.bottom  : 0.1    # the bottom of the subplots of the figure
figure.subplot.top     : 0.9    # the top of the subplots of the figure
figure.subplot.wspace  : 0.2    # the amount of width reserved for blank space between subplots
figure.subplot.hspace  : 0.2    # the amount of height reserved for white space between subplots

这决定了情节周围留下多少空间。

关于python - matplotlib 的 x 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31129079/

相关文章:

python - 在 html 表中访问带有空格的 json 对象

python - 在 pygame 中使用 matplotlib

python - 使用 Pandas 和 Matplotlib 在一个图中绘制 5 个数据帧?

matlab - 使用新的 Matlab 图形引擎绘制存储在 .mat 文件中的图形对象

python - Python 中的 GUI 与 TUI

python - 如何在 anaconda 本地卸载软件包?

r - 用不同的颜色覆盖两个随机数图

python - Matplotlib - 箱线图根据 log10 值计算但以对数刻度显示

python - 相互引用 Enum 成员

python - 如何使用 Python 在 3D 图形表面上绘制图像文件? - 不绘制为平面