I would like to increase the horizontal spacing between two subplots, otherwise the ylabel of the second one (the right one) overlaps with the right plot itself (I prefer to do that rather than change the font sizes). Here is a sample code:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as mtick
x = np.arange(0,1,0.01)
y = x**2
z = x**3
fig = plt.figure('Sample')
plt.subplot(2,2,1)
plt.xlabel("$x$", fontsize=33)
plt.ylabel("My function", fontsize=33)
plt.plot(x, y)
ax = plt.gca()
ax.set_xticklabels(ax.get_xticks(),fontsize=26)
ax.set_yticklabels(ax.get_yticks(),fontsize=26)
ax.xaxis.set_major_formatter(mtick.FormatStrFormatter('%.1f'))
ax.yaxis.set_major_formatter(mtick.FormatStrFormatter('%.1f'))
plt.subplot(2,2,2)
plt.xlabel("$x$", fontsize=33)
plt.ylabel("My function 2", fontsize=33)
plt.plot(x, z)
ax = plt.gca()
ax.set_xticklabels(ax.get_xticks(),fontsize=26)
ax.set_yticklabels(ax.get_yticks(),fontsize=26)
ax.xaxis.set_major_formatter(mtick.FormatStrFormatter('%.1f'))
ax.yaxis.set_major_formatter(mtick.FormatStrFormatter('%.1f'))
Is there a simple way to achieve this?
I tried to use “fig.tight_layout()” but it had no effect
New contributor
Goose is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2