0

so I am making 4 types of plots in matplotlib through functions. Those include Pie Charts, Line Charts, Scatter Plot and area graphs. I want to get a numpy array of it, so I can display it using opencv2 or something else on django. I have tried this so far:

import matplotlib.pyplot as plt

import numpy as np

# Make a random plot...
fig = plt.figure()
fig.add_subplot(111)

# If we haven't already shown or saved the plot, then we need to
# draw the figure first...
fig.canvas.draw()

# Now we can save it to a numpy array.
data = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8, sep='')
data = data.reshape(fig.canvas.get_width_height()[::-1] + (3,))

But the problem is I cannot use a plot here, I have to use a figure which I don't want to use. I tried doing plt.plot([1,2,3,4,5]) for line graph as a test, but it turns out it returns a list, while I need a figure to use the tostring_rgb() What should be the alternative to this?

EDIT: Suggested in comments for another question, I am not wanting to make a figure, I want to make a normal Plot with plt.plot() of line graph, and also plt.pie()

  • Your current example technically isn't making a plot, it is just creating an empty figure. Thus there would be nothing to load. – code-lukas Jun 16 '20 at 16:07
  • Does this answer your question? [Matplotlib figure to image as a numpy array](https://stackoverflow.com/questions/35355930/matplotlib-figure-to-image-as-a-numpy-array) – code-lukas Jun 16 '20 at 16:11
  • I know that, but it atleast makes an empty figure. But I am making a plot, with ```plt.plot()``` and ```plt.pie()``` so how do I get a numpy array of it? Do I use subplots? – aditya raj chopra Jun 17 '20 at 06:31

0 Answers0