在matplotlib中自动调整图的大小 [英] Resize a figure automatically in matplotlib

查看:422
本文介绍了在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.

这篇关于在matplotlib中自动调整图的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆