【问题标题】:plot from a large data set into several figures将大数据集绘制成多个图形
【发布时间】:2013-11-21 01:10:53
【问题描述】:

我有大量粒子位置数据,这些数据来自物理实验中的检测相机制作的电影。第一列只是给出帧号,第二到第四列是x、y、z位置。 现在我想绘制前 10 帧的 x 和 y 位置,但对于不同图中的每一帧。我正在使用compress 命令来提取每一帧的数据。到目前为止,这是我的代码:

import numpy as np
from itertools import compress

data = np.genfromtxt('beta_tracking.csv', delimiter=' ')

f=data[:,0]
x=data[:,1]
y=data[:,2]
z=data[:,3]

k=range(1,11)
for j in k:
    g=list(compress(x, (abs(i)==j for i in f)))
    h=list(compress(y, (abs(i)==j for i in f)))
    plot(g,h,',')
    savefig('plots/framedata.png')

但是,这给了我前 10 帧的 x 和 y 位置都在一个图中。如何告诉 python 为每一帧创建一个新图形并自动保存它们?

【问题讨论】:

  • np.loadtxtnp.genfromtxt 快​​得多,如果您的数据仅以空格分隔,则应该可以使用。

标签: python numpy plot


【解决方案1】:

http://matplotlib.org/faq/usage_faq.html

matplotlib 的 OO 接口允许您跟踪绘制线条的位置,如下所示:

FigList = [ ] # to store the figures
for x,y in zip(g,h):
    FigList.append(plt.figure())
    ax = FigList[-1].add_subplot(1,1,1)
    ax.plot(x,y,',')
    FigList[-1].savefig(FigureName)

【讨论】:

    【解决方案2】:

    所以,这段代码完全符合我的要求:

    import numpy as np
    import matplotlib.pyplot as plt    
    from itertools import compress
    
    
    data = np.genfromtxt('beta_tracking.csv', delimiter=' ')
    
    f=data[:,0]
    x=data[:,1]
    y=data[:,2]
    z=data[:,3]
    
    k=range(1,11)
    for j in k:
        g=list(compress(x, (abs(i)==j for i in f)))
        h=list(compress(y, (abs(i)==j for i in f)))
        FigList = [ ] 
        FigList.append(plt.figure())
        ax = FigList[-1].add_subplot(1,1,1)
        ax.plot(g,h,',')
        for i in plt.get_fignums():
              plt.figure(i)
              plt.savefig('plots/figure%d.png' % i)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-01
      • 2021-04-22
      • 1970-01-01
      • 2021-09-16
      • 2016-05-16
      • 2023-01-20
      相关资源
      最近更新 更多