使用bbox_inches ='tight'的matplotlib savefig图像大小 [英] matplotlib savefig image size with bbox_inches='tight'

查看:163
本文介绍了使用bbox_inches ='tight'的matplotlib savefig图像大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须制作一个矢量图,我只想看到没有轴、标题等的矢量,所以我尝试这样做:

I have to make a vector plot and I want to just see the vectors without the axes, titles etc so here is how I try to do it:

pyplot.figure(None, figsize=(10, 16), dpi=100)
pyplot.quiver(data['x'], data['y'], data['u'], data['v'], 
              pivot='tail', 
              units='dots', 
              scale=0.2,
              color='black')

pyplot.autoscale(tight=True)
pyplot.axis('off')
ax = pyplot.gca()
ax.xaxis.set_major_locator(pylab.NullLocator())
ax.yaxis.set_major_locator(pylab.NullLocator())
pyplot.savefig("test.png", 
               bbox_inches='tight', 
               transparent=True,
               pad_inches=0)

尽管我努力使图像由1000放大到1600,但得到775放大到1280.如何将其设置为所需的尺寸?谢谢.

and despite my efforts to have an image 1000 by 1600 I get one 775 by 1280. How do I make it the desired size? Thank you.

更新提出的解决方案有效,除了在我的情况下,我还必须手动设置轴限制.否则,matplotlib无法找出紧密"边界框.

UPDATE The presented solution works, except in my case I also had to manually set the axes limits. Otherwise, matplotlib could not figure out the "tight" bounding box.

推荐答案

import matplotlib.pyplot as plt
import numpy as np
sin, cos = np.sin, np.cos

fig = plt.figure(frameon = False)
fig.set_size_inches(5, 8)
ax = plt.Axes(fig, [0., 0., 1., 1.], )
ax.set_axis_off()
fig.add_axes(ax)

x = np.linspace(-4, 4, 20)
y = np.linspace(-4, 4, 20)
X, Y = np.meshgrid(x, y)
deg = np.arctan(Y**3-3*Y-X)
plt.quiver(X, Y, cos(deg), sin(deg), pivot = 'tail', units = 'dots', color = 'red', )
plt.savefig('/tmp/test.png', dpi = 200)

收益

通过将数字设置为5x8英寸,可以使结果图像为1000x1600像素

You can make the resultant image 1000x1600 pixels by setting the figure to be 5x8 inches

fig.set_size_inches(5, 8)

并使用 DPI = 200 保存:

and saving with DPI = 200:

plt.savefig('/tmp/test.png', dpi = 200)

删除边框的代码来自此处.

(由于1000x1600很大,因此上面的图片无法缩放).

(The image posted above is not to scale since 1000x1600 is rather large).

这篇关于使用bbox_inches ='tight'的matplotlib savefig图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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