Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

x position of matplotlib

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')
like image 580
Huanian Zhang Avatar asked Aug 06 '26 13:08

Huanian Zhang


1 Answers

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.

like image 126
Andrzej Pronobis Avatar answered Aug 09 '26 01:08

Andrzej Pronobis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!