Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract data from matplotlib plot

Tags:

I have a wxPython program which reads from different datasets, performs various types of simple on-the-fly analysis on the data and plots various combinations of the datasets to matplotlib canvas. I would like to have the opportunity to dump currently plotted data to file for more sophisticated analysis later on.

The question is: are there any methods in matplotlib that allow access to the data currently plotted in matplotlib.Figure?

like image 287
Andrey Sobolev Avatar asked Jan 20 '12 08:01

Andrey Sobolev


People also ask

How do I export a matplotlib graph?

Save as PDF File If you want to export a graph with matplotlib, you will always call . savefig(path) . matplotlib will figure out the file type based on the passed file path .

How do I get xy data in python?

Use get_xdata() and get_ydata() methods on the line to get xy data. To display the figure, use show() method.

How do you show a value in a plot in python?

Just call the plot() function and provide your x and y values. Calling the show() function outputs the plot visually.


1 Answers

Jakub is right about modifying the Python script to write out the data directly from the source from which it was sent into the plot; that's the way I'd prefer to do this. But for reference, if you do need to get data out of a plot, I think this should do it

gca().get_lines()[n].get_xydata() 

Alternatively you can get the x and y data sets separately:

line = gca().get_lines()[n] xd = line.get_xdata() yd = line.get_ydata() 
like image 191
David Z Avatar answered Oct 21 '22 11:10

David Z