在matplotlib中自动调整图的大小
问题描述:
有没有一种方法可以自动调整图形的大小以正确适合matplotlib/pylab图像中包含的图形?
Is there a way to automatically resize a figure to properly fit contained plots in a matplotlib/pylab image?
我正在根据所使用的数据创建纵横比不同的热图(子)图.
I'm creating heatmap (sub)plots that differ in aspect ratio according to the data used.
我意识到我可以计算出宽高比并手动设置它,但是肯定有更简单的方法吗?
I realise I could calculate the aspect ratio and manually set it, but surely there's an easier way?
答
使用 bbox_inches ='tight'
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
X = 10*np.random.rand(5,3)
fig = plt.figure(figsize=(15,5),facecolor='w')
ax = fig.add_subplot(111)
ax.imshow(X, cmap=cm.jet)
plt.savefig("image.png",bbox_inches='tight',dpi=100)
...只在保存图像时有效,不显示.
...only works when saving images though, not showing them.