减少 matplotlib 图中的左右边距 [英] Reduce left and right margins in matplotlib plot

查看:67
本文介绍了减少 matplotlib 图中的左右边距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力处理我在 matplotlib 中的图边距.我已使用以下代码生成图表:

I'm struggling to deal with my plot margins in matplotlib. I've used the code below to produce my chart:

plt.imshow(g)
c = plt.colorbar()
c.set_label("Number of Slabs")
plt.savefig("OutputToUse.png")

但是,我得到的输出图在图的两侧都有很多空白.我搜索过谷歌并阅读了 matplotlib 文档,但我似乎找不到如何减少这种情况.

However, I get an output figure with lots of white space on either side of the plot. I've searched google and read the matplotlib documentation, but I can't seem to find how to reduce this.

推荐答案

自动执行此操作的一种方法是将 bbox_inches='tight' kwarg 转换为 plt.savefig.

One way to automatically do this is the bbox_inches='tight' kwarg to plt.savefig.

例如

import matplotlib.pyplot as plt
import numpy as np
data = np.arange(3000).reshape((100,30))
plt.imshow(data)
plt.savefig('test.png', bbox_inches='tight')

另一种方法是使用 fig.tight_layout()

Another way is to use fig.tight_layout()

import matplotlib.pyplot as plt
import numpy as np

xs = np.linspace(0, 1, 20); ys = np.sin(xs)

fig = plt.figure()
axes = fig.add_subplot(1,1,1)
axes.plot(xs, ys)

# This should be called after all axes have been added
fig.tight_layout()
fig.savefig('test.png')

这篇关于减少 matplotlib 图中的左右边距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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