In Python Matplotlib, I would need give advice how to keep exactly same axes/spine border size for multiple plots. Independent of how many digits the axis contains.
Code bellow shows example of generating two plots, each with different axis value (1 digit vs. 3/4 digit on axis), axis+spine border size is different. Respective in case lower digit count is border larger, for higher digit count it is sligtly smaller. I would need to keep same size. Thank you for help
import matplotlib.pyplot as plt
from matplotlib.ticker import FormatStrFormatter
import matplotlib.ticker as ticker
plt.style.use('classic')
plt.rcParams.update({'mathtext.default': 'regular'})
plt.rcParams['font.family'] = 'Arial'
plt.rcParams["axes.linewidth"] = 0.50
i=0
plots=['plot1','plot2']
xlabel=['x_plot1','x_plot2']
ylabel=['y_plot1','y_plot2']
for i in range(len(plots)):
fig, ax = plt.subplots( figsize=[6.0, 4.8])
fig.tight_layout()
if(i==0):
ax.plot([0,9], [0,9])
ax.axis([0, 9, 0, 9])
if(i==1):
ax.plot([0,1000], [0,1000])
ax.axis([0, 1000, 0, 1000])
ax.set_xlabel(xlabel[i],fontsize='12')
ax.set_ylabel(ylabel[i],fontsize='12')
ax.grid(linestyle='solid', linewidth=0.6, color='#000000', which='major', zorder=1, clip_on=False) #New #EAEAEA old #C3C3C3
plt.gcf().canvas.draw()
ax.set_axisbelow(True)
ax.spines['bottom'].set_linewidth(1.5)
ax.spines['left'].set_linewidth(1.5)
plt.yticks(fontsize='12', zorder=0)
plt.xticks(fontsize='12', zorder=0)
plt.savefig(plots[i]+'.png', dpi=300, bbox_inches='tight', transparent=False)
plt.clf()
Tried change figsize value, aspect ratio of graphs, others, but no satisfactory results.
I expect set static value where should be place axes in figure, create enough margin for more digits on axis.
This is expected behavior when passing
bbox_inches='tight'
toplt.savefig
: the plot margins will be adjusted to fit the axes, but since the width of your tick labels vary, it comes to different results. Removebbox_inches='tight'
and specify the layout withsubplots_adjust
:Output: