Change subplot size in python matplotlib

岁酱吖の 提交于 2020-01-15 09:23:34

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!