问题
I would like to change the size of the subplot when I use Python's matplotlib. I already know how to change the size of the figure, however, how does one go about changing the size of the corresponding subplots themselves? This has remained somewhat elusive in my googling. Thanks.
(For example, given a figure where we have 2x4 subplots. I want to make the subplots within the figure occupy more area).
回答1:
You can also play with the axis size. http://matplotlib.org/api/axes_api.html
set_position() can be used to change width and height of your axis.
import matplotlib.pyplot as plt
ax = plt.subplot(111)
box = ax.get_position()
ax.set_position([box.x0, box.y0, box.width * 1.1 , box.height * 1.1])
回答2:
You can use .subplots_adjust(), e.g.
import seaborn as sns, matplotlib.pyplot as plt
titanic = sns.load_dataset('titanic')
g = sns.factorplot(x='sex',y='age',hue='class',data=titanic,
kind='bar',ci=None,legend=False)
g.fig.suptitle('Oversized Plot',size=16)
plt.legend(loc='best')
g.fig.subplots_adjust(top=1.05,bottom=-0.05,left=-0.05,right=1.05)
Results in this monstrosity:
来源:https://stackoverflow.com/questions/38863262/change-subplot-size-in-python-matplotlib