简体   繁体   English

如何从 matplotlib 图中提取数据

[英]How to extract data from matplotlib plot

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.我有一个wxPython程序,它从不同的数据集读取数据,对数据执行各种类型的简单即时分析,并将数据集的各种组合绘制到matplotlib画布上。 I would like to have the opportunity to dump currently plotted data to file for more sophisticated analysis later on.我希望有机会将当前绘制的数据转储到文件中,以便稍后进行更复杂的分析。

The question is: are there any methods in matplotlib that allow access to the data currently plotted in matplotlib.Figure ?问题是: matplotlib中是否有任何方法允许访问当前在matplotlib.Figure绘制的数据?

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; Jakub 修改 Python 脚本以直接从数据发送到绘图的源中写出数据是正确的; 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:或者,您可以分别获取 x 和 y 数据集:

line = gca().get_lines()[n]
xd = line.get_xdata()
yd = line.get_ydata()

The matplotlib.pyplot.gca can be used to extract data from matplotlib plots. matplotlib.pyplot.gca可用于从 matplotlib 图中提取数据。 Here is a simple example:这是一个简单的例子:

import matplotlib.pyplot as plt
plt.plot([1,2,3],[4,5,6])
ax = plt.gca()
line = ax.lines[0]
line.get_xydata()

On running this, you will see 2 outputs - the plot and the data:运行此程序时,您将看到 2 个输出 - 绘图和数据:

array([[1., 4.],
   [2., 5.],
   [3., 6.]])

在此处输入图片说明

You can also get the x data and y data seperately.您还可以分别获取 x 数据和 y 数据。 On running line.get_xdata() , you will get:在运行line.get_xdata() ,您将获得:

array([1, 2, 3])

And on running line.get_ydata() , you will get:在运行line.get_ydata() ,您将获得:

array([4, 5, 6])

Note: gca stands for get current axis注意: gca代表获取当前轴

它是 Python,因此您可以直接修改源脚本,以便在绘制数据之前转储数据

I know this is an old question, but I feel there is a solution better than the ones offered here so I decided to write this answer.我知道这是一个老问题,但我觉得有一个比这里提供的解决方案更好的解决方案,所以我决定写下这个答案。

You can use unittest.mock.patch to temporarily replace the matplotlib.axes.Axes.plot function:您可以使用unittest.mock.patch临时替换matplotlib.axes.Axes.plot函数:

from unittest.mock import patch

def save_data(self, *args, **kwargs):
    # save the data that was passed into the plot function
    print(args)

with patch('matplotlib.axes.Axes.plot', new=save_data):
    # some code that will eventually plot data
    a_function_that_plots()

Once you exit the with block, Axes.plot will resume normal behavior.一旦退出with块, Axes.plot将恢复正常行为。

To sum up, for future reference:总结一下,供以后参考:

If plotting with plt.plot() or plt.stem() or plt.step() you can get a list of Line2D objects with:如果使用plt.plot()plt.stem()plt.step()绘图,您可以获得 Line2D 对象的列表:

ax = plt.gca() # to get the axis
ax.get_lines()

For plt.pie() , plt.bar() or plt.barh() you can get a list of wedge or rectangle objects with:对于plt.pie()plt.bar()plt.barh()您可以获得楔形或矩形对象的列表:

ax = plt.gca() # to get the axis
ax.patches()

Then, depending on the situation you can get the data by running get_xdata() , get_ydata() (see Line2D ) for more info.然后,根据情况,您可以通过运行get_xdata()get_ydata() (请参阅Line2D )获取数据以获取更多信息。

or ie get_height() for a bar plot (see Rectangle ) for more info.或即get_height()用于条形图(请参阅Rectangle )了解更多信息。

In general for all basic plotting functions, you can find what you are looking for by running ax.get_children()一般来说,对于所有基本绘图功能,您可以通过运行ax.get_children()找到您要查找的内容

that returns a list of the children Artists (the base class the includes all of the figure's elements).返回子Artists的列表(基类包括图形的所有元素)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM