从matplotlib图检索XY数据 [英] Retrieve XY data from matplotlib figure
问题描述
我正在wxPython中编写一个小应用程序,该应用程序具有matplotlib图形(使用wxagg后端)面板.我想为用户添加将图中当前绘制的X,Y数据导出到文本文件的功能.有无创方法可以做到这一点吗?我已经搜索了很多,似乎什么也找不到,尽管我觉得它非常简单并且就在我的面前.
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()
希望这有道理:)
非常感谢!任何帮助,我们将不胜感激!
Thanks so much! Any help is greatly appreciated!
编辑:澄清一下,我想知道如何做就是获取X,Y数据.在那之后写入文本文件很简单;)
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 [1]: import matplotlib.pyplot as plt
In [2]: plt.plot([1,2,3],[4,5,6])
Out[2]: [<matplotlib.lines.Line2D at 0x30b2b10>]
In [3]: ax = plt.gca() # get axis handle
In [4]: line = ax.lines[0] # get the first line, there might be more
In [5]: line.get_xdata()
Out[5]: array([1, 2, 3])
In [6]: line.get_ydata()
Out[6]: array([4, 5, 6])
In [7]: line.get_xydata()
Out[7]:
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.
编辑:Joe Kington展示了一种更简洁的方法:
Edit: Joe Kington showed a slightly neater way to do this:
In [1]: import matplotlib.pyplot as plt
In [2]: lines = plt.plot([1,2,3],[4,5,6],[7,8],[9,10])
In [3]: lines[0].get_data()
Out[3]: (array([1, 2, 3]), array([4, 5, 6]))
In [4]: lines[1].get_data()
Out[4]: (array([7, 8]), array([ 9, 10]))
这篇关于从matplotlib图检索XY数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!