Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert pyplot figure into wand.image Image

Is there a way to convert a pyplot figure created with pyplot.Figure into a wand image? I have tried using the following to no avail:

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

The end goal of this is to convert a list of figures into a long png. The only other method (which is ugly) is to convert to pdf and then concatenate the pages.

like image 811
dlwlrma Avatar asked Jul 21 '26 20:07

dlwlrma


1 Answers

I was trying to figure out how to do this same thing. I went down a rabbit hole for a bit thinking I needed to also use PIL (Pillow) to accomplish this task. With the help of the previous answer I was able to come up with a complete example:

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
like image 154
Matthew McMillan Avatar answered Jul 24 '26 10:07

Matthew McMillan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!