I'm writing a little app in wxPython which has a matplotlib figure (using the wxagg backend) panel. I'd like to add the ability for the user to export X,Y data of what is currently plotted in the figure to a text file. Is there a non-invasive way to do this? I've searched quite a bit and can't seem to find anything, though I feel like it is incredibly simple and right in front of my face.
I could definitely get the data and store it somewhere when it is plotted, and use that - but that would be fairly invasive, into the lower levels of my code. It would be so much easier, and universal, if I could do something as easy as:
x = FigurePanel.axes.GetXData() y = FigurePanel.axes.GetYData()
Hopefully that makes some sense :)
Thanks so much! Any help is greatly appreciated!
edit: to clarify, what I'd like to know how to do is get the X,Y data. Writing to the text file after that is trivial ;)
In : import matplotlib.pyplot as plt In : plt.plot([1,2,3],[4,5,6]) Out: [<matplotlib.lines.Line2D at 0x30b2b10>] In : ax = plt.gca() # get axis handle In : line = ax.lines # get the first line, there might be more In : line.get_xdata() Out: array([1, 2, 3]) In : line.get_ydata() Out: array([4, 5, 6]) In : line.get_xydata() Out: array([[ 1., 4.], [ 2., 5.], [ 3., 6.]])
I found these by digging around in the axis object. I could only find some minimal information about these functions, apperently you can give them a boolean flag to get either original or processed data, not sure what the means.
Edit: Joe Kington showed a slightly neater way to do this:
In : import matplotlib.pyplot as plt In : lines = plt.plot([1,2,3],[4,5,6],[7,8],[9,10]) In : lines.get_data() Out: (array([1, 2, 3]), array([4, 5, 6])) In : lines.get_data() Out: (array([7, 8]), array([ 9, 10]))