- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试将 png 图像添加到使用 python 中的 matplotlib
创建的绘图中。
这是我的剧情代码
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的513x306 px,但是fig.bbox.xmax
的值为1650.0
...这就是我的图没有出现的原因.. .. 我如何才能在打印图像之前知道图像的大小,以便知道将我的 im
放在哪里?
谢谢
最佳答案
这里发生了两件事:
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 那样“紧密”裁剪。
关于python - matplotlib + python : figure size for fig. figimage 和 fig.savefig,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16334824/
我正在尝试以其真实尺寸(未缩放或拉伸(stretch))渲染图像,使用 matplotlib 执行此操作的最简单方法似乎是 figimage。 但是,当我尝试在 Jupyter notebook 中使
我正在尝试将 png 图像添加到使用 python 中的 matplotlib 创建的绘图中。 这是我的剧情代码 import matplotlib as mpl mpl.use('Agg') imp
我是一名优秀的程序员,十分优秀!