如何从Matplotlib图中提取数据 [英] How to extract data from matplotlib plot
问题描述
我有一个wxPython
程序,该程序从不同的数据集中读取数据,对数据执行各种简单的即时分析,并将数据集的各种组合绘制到matplotlib
画布上.我希望有机会将当前绘制的数据转储到文件中,以便稍后进行更复杂的分析.
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.
问题是:matplotlib
中是否有任何方法可以访问matplotlib.Figure
中当前绘制的数据?
The question is: are there any methods in matplotlib
that allow access to the data currently plotted in matplotlib.Figure
?
推荐答案
Jakub正确地修改了Python脚本,以便直接从发送数据的源中写入数据;这就是我更愿意这样做的方式.但作为参考,如果您确实需要从绘图中获取数据,我认为应该这样做
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()
或者,您可以分别获取x和y数据集:
Alternatively you can get the x and y data sets separately:
line = gca().get_lines()[n]
xd = line.get_xdata()
yd = line.get_ydata()
这篇关于如何从Matplotlib图中提取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!