我试图在python中用matplotlib创建一个png图像。
这是我的情节代码
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(5.5,3),dpi=300)
ax = fig.add_subplot(111)
ax.grid(True,which='both')
ax.plot([0,1,2,3],[5,2,6,3],'o')
xlabel = ax.set_xlabel('xlab')
ax.set_ylabel('ylab')
from PIL import Image
import numpy as np
im = Image.open('./lib/Green&Energy-final-roundonly_xsmall.png')
im_w = im.size[0]
im_h = im.size[1]
# We need a float array between 0-1, rather than
# a uint8 array between 0-255
im = np.array(im).astype(np.float) / 255
fig.figimage(im,fig.bbox.xmax - im_w - 2,2,zorder=10 )
fig.savefig('test.png',bbox_extra_artists=[xlabel], bbox_inches='tight')该数字以pdf格式保存513x306px,但fig.bbox.xmax的值为1650.0。这就是为什么我的身材没有出现..。在打印图像之前,我如何知道图像的大小,这样我才能知道将im放在哪里
谢谢
发布于 2013-05-02 15:32:31
这里发生了两件事:
bbox_inches='tight'将生成的图像缩小第二项是常见的问题。默认情况下,matplotlib将图形保存在与图形的本机dpi不同的dpi (在rc参数中可配置)。
要解决这个问题,请将fig.dpi传递给fig.savefig
fig.savefig(filename, dpi=fig.dpi, ...)要想减少裁剪,可以这样做:( a)将bbox_inches='tight'完全排除在外,或者( b)调整图形内的大小。实现(b)的一个快速方法是使用fig.tight_layout,尽管它不会像使用bbox_inches和savefig那样“紧紧地”裁剪。
https://stackoverflow.com/questions/16334824
复制相似问题