5

I'm trying to determine what the data points are on a matplotlib axes. Is there an attribute I'm missing on the Axes object to get the x/y data values?

For example, say my code is passed a line plot, and I want to print out the x/y values that are plotted.

1 Answer 1

9

Your plot call will give you a lines.Line2D, which has the get_xdata(orig=True) and the get_ydata(orig=True) methods.

You can check axes.get_children() for Line2D instances.

Note that what you're doing sounds horrible from a software design point of view. You should rather implement something like a wrapper for plot that prints your raw data.

@JRichardSnape adds that iff your plot is only lines, you can use get_lines() rather than filtering the output of get_children().

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the help. I disagree with your comment about being 'horrible from a software design point of view'. I'm trying to work with a third-party library that returns plots. I'm not creating the plots myself. So, I have to ask the plot what its' data is.
@durden2.0: I didn't say your design choices were horrible :)
@durden2.0 furthermore, maybe you could just rename the axes.plot function to something like axes.oldplot in your matplotlib installation, and write that minimal wrapper. Or the hell, just modify the source code of the software you're using, if you can get it. For anything that plots data, I'd urge the author twice to give you the source code -- don't trust any graphical representation that you haven't faked yourself!
That's a good point. I could just change the source code in matplotlib or the other library. However, then I have to remember that change when I upgrade either library. I'm trying to avoid modifying other packages if possible to avoid breaking anything in those libraries and maintaining my own forks for such a small change.
A little sub-note - I was going to write an answer, but it appears this has resolved it. If (and only if, I think) the data on your plots are lines (rather than collections generated by plt.scatter() for instance) you can use get_lines() rather than get_children() and then you get only Line2D objects, not all artists that you then have to filter.
|

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.