2

Is it possible to get the numpy array describing an arbitrary matplolib/seaborn plot without explicitly saving it as an image file and reading it?

For example, say I have the attached image which is a sns.kdeplot() of an array. Now, can I get the numpy array of this plot without first saving it to a file and reading it? Something like:

img = sns.kdeplot(arr)
img_arr = img.some_function() # Returns a numpy array describing the plot

enter image description here

1 Answer 1

2

fig.canvas.tostring_rgb() would be helpful:

fig, ax = plt.subplots()
sns.kdeplot(arr, ax=ax)

img_arr = np.fromstring(fig.canvas.tostring_rgb(), 
                        dtype=np.uint8,
                        sep='')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.