繁体   English   中英

将pyplot图转换成wand.image图片

[英]Convert pyplot figure into wand.image Image

有没有办法将使用 pyplot.Figure 创建的pyplot.Figure图转换为魔杖图像? 我尝试使用以下无济于事:

image_data = BytesIO()
figure.savefig(image_data, format='png')
image_data.seek(0)
image = Image(file=image_data, resolution=250)

这样做的最终目标是将图形列表转换为长 png。 唯一的其他方法(很难看)是转换为 pdf,然后连接页面。

我相信您的方向正确。 如果没有看到该图,我将认为问题与使用with关键字持有C结构指针的有关。

image_data = BytesIO()
figure.savefig(image_data, dpi=250, format='png')
image_data.seek(0)
with Image(file=image_data) as img:
    # ... do work
    img.save(filename='/tmp/out.png')

我试图弄清楚该怎么做。 我掉进一个兔子洞有点想我也需要使用PIL(枕头)来完成此任务。 在上一个答案的帮助下,我得以提出一个完整的例子:

import matplotlib
from io import BytesIO
import numpy
import matplotlib.pyplot as plt
from wand.display import display
from wand.image import Image

plt.plot([1,5,3,2])
plt.ylabel('y axis numbers')
plt.xlabel('x axis numbers')

image_data = BytesIO() #Create empty in-memory file
plt.savefig(image_data, format='png') #Save pyplot figure to in-memory file
image_data.seek(0) #Move stream position back to beginning of file 
img = Image(file=image_data) #Create wand.image
display(img) #Use wand to display the img

我尝试了上面推荐的代码,但没有成功。 我将问题发布到 WandB 论坛(此处),并推荐了以下内容:

fig, ax1 = plt.subplots(...)
...
buf = io.BytesIO()
plt.savefig(buf, format='png')
buf.seek(0)
wandb.log(({"chart": wandb.Image(Image.open(buf)) }))
fig.show()

似乎不再允许使用file参数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM