How to extract data from matplotlib plot

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()

Leave a Comment