I am plotting two different graphs with matplotlib. The two plots are separate and must be kept that way since I am saving them onto file and have different uses for them.
As it can be seen on the picture below they are misaligned because the ylabels on the top graph are occupying more space than the one on the bottom. Is there a way to fix the width occupied by the labels? I have tried:
ax.set_ylabel('Y Axis Label', labelpad=50)
But it didn't do much
Importantly, I cannot change the label format (scientific or other).
the whole function that produces the single graph follows:
def prepare_plot(inputDataFrame : pd.DataFrame(), median_columns : list(str()), upper_lower_quantiles : int, labels : list(str())) :
"""
inputDataFrame: dataframe with values
median_columns: names of the columns to be used for median values
upper_lower_quantiles: Delta (1, 2 or 3) compared to the median, if invalid input will default to 1
labels: labels to be set
"""
if upper_lower_quantiles not in [1,2,3] : upper_lower_quantiles = 1
flike = io.BytesIO()
df = inputDataFrame
x = df.index
fig, ax = plt.subplots()
fig.set_size_inches(15, 5)
colors = ['g','r','b']
for median, label, color in zip(median_columns,labels, colors):
lower = median[:-1] + f'{3 - upper_lower_quantiles}'
upper = median[:-1] + f'{3 + upper_lower_quantiles}'
try:
ax.plot(x, df[median], label=label, color=color)
ax.fill_between(x,
df[lower], #Lower Bound
df[upper], #Upper Bound
color=color,
alpha=.1
)
except:
print(f'Error on Key ({median}), label ({label}), color ({color})')
print(f'Not graphing the line')
ax.grid()
lgd = ax.legend(loc='center left', bbox_to_anchor=(1.0, 0.5), fontsize=10)
fig.savefig(flike, bbox_extra_artists=(lgd,), bbox_inches='tight')
b64 = base64.b64encode(flike.getvalue()).decode()
#Saving in a format that is embeddable in different other platforms
return b64