3

Now I am making some plots using python. When the xtick is too long, then the x label is outside of the plot. Like below: enter image description here

I know we can adjust it when we save the figure by the command plt.savefig("plot_name",bbox_inches='tight'), but this will change the size of the figures if I am making several plots when I compile in the latex, like below.

enter image description here

I want all my plots have the same size, then when I compile in the latex, they will have the same size.

Do anyone can offer me some hint to solve this problem? Thanks a lot.

The code to make those plots are: (this posted code is not exactly the same code to make above plots, since I can not post that one. But those two pretty much the same, the only difference is that one has a for loop to make several figures, the other one enumerate all figures.)

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(params)

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')
Huanian Zhang
  • 830
  • 3
  • 16
  • 37
  • Make the font smaller as demonstrated here http://stackoverflow.com/questions/3899980/how-to-change-the-font-size-on-a-matplotlib-plot – Derrops Jun 30 '15 at 03:37
  • Can you post the code that you use to make the figure? – gabra Jun 30 '15 at 03:55
  • Are you creating both columns as part of the same grid/figure? If not, maybe having them all in the figure will mean they all get adjusted the same when converting to tight layout? – Marius Jun 30 '15 at 03:58
  • It seems there is some error when I try to edit in the code format. I will try it again. – Huanian Zhang Jun 30 '15 at 04:12
  • No, I create differently. The left figure contain three subplots, the same as right. – Huanian Zhang Jun 30 '15 at 04:13

1 Answers1

1

You can adjust all the plot parameters in the matplotlibrc file including the size of the margins. The file is typically in ~/.config/matplotlib/matplotlibrc on Linux. The file is not created by default, so I suggest copying this template and then modifying it.

You can be interested in the following settings:

# 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

which determine how much space is left around the plot.

Andrzej Pronobis
  • 33,828
  • 17
  • 76
  • 92
  • That is weird, there is no matplotlibrc file in the directory ~/.config/matplotlib/. It is an empty directory. – Huanian Zhang Jun 30 '15 at 04:51
  • You need to create the file. I now added a link to my answer to a template file which you can place there and modify. – Andrzej Pronobis Jun 30 '15 at 05:00
  • Thank you very much for your help. The subplot parameters can use this command: plt.subplots_adjust(left=0.16, right=0.9, top=0.95, bottom=0.05) for one specific figure. BTW, I do not find x,y marginal parameter in the matplotlibrc file. – Huanian Zhang Jun 30 '15 at 05:15