3
我试图添加一个PNG图像到在Python中使用matplotlib
创建的图。matplotlib + python:fig.figimage和fig.savefig的图尺寸
这里是我的阴谋代码
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')
这个数字已经513x306像素保存为PDF格式,但fig.bbox.xmax
值1650.0
...这就是为什么我的身影没有出现....我怎么能在打印之前知道图像的大小,所以我可以知道把我的im
放在哪里?
感谢
取出'bbox_inches ='tight''这是收缩包装边框,所以你失去了对最终尺寸的一些控制。 – tacaswell 2013-05-02 14:50:48
[这] [1]答案可能是你要找的。 [1]:http://stackoverflow.com/a/15145013/2297781 – Saethlin 2015-09-15 15:52:25