如何在matplotlib中的文本文件中绘制特定范围的值

问题描述 投票:2回答:1

我有一个文本文件包含值:

0.00   -10.742    10.7888  6.33455
1.00   -17.75391  10.0000  4.66778
4.00   -19.62891  15.9999  4.232323
20.00  -20.7641   18.6666  3.99999
23.00  -34.2300   2.7777   2.00000
50.00  -50.000    1.87878  2.77778
65.88   -22.5000  2.99999  1.45555
78.00   -30.000   1.55555  2.45667
86.00   -37.7900  2.55556  7.55679
90.00   -45.00000 13.6667  2.677888

我希望在一段时间间隔之后仅绘制文本文件中的一系列值,以绘制另一组值。例如:首先,我希望仅绘制[0到50]:

0.00   -10.742
1.00   -17.75391
4.00   -19.62891
20.00  -20.7641
23.00  -34.2300
50.00  -50.000

经过一段时间间隔(比如10s),我希望绘制下一组值,即:

65.88   -22.5000
78.00   -30.000
86.00   -37.7900
90.00   -45.00000

期待以幻灯片形式展示这一点。

我试过的是:

import matplotlib.pyplot as plt
import sys
import numpy as np
from matplotlib import style
fileName=input("Enter Input File Name: ")
f1=open(fileName,'r')
style.use('ggplot')
x1,y1=np.loadtxt(fileName,unpack=True, usecols=(0,1));
plt.plot(x1,y1,'r')
plt.plot
plt.title('example1')
plt.xlabel('Time')
plt.ylabel('Values')
plt.grid(True,color='k')
plt.show()

我想以幻灯片形式展示这一点。如果有人帮助我,我将感激不尽。

python matplotlib
1个回答
0
投票

您可以使用for循环并迭代行并显示它们。假设您有100行数据,并且您希望一次显示25行。

data = np.genfromtxt('/path/to/data/file')

m = np.size(data, 0) # Getting total no of rows
n = np.size(data, 1) # Getting total no of columns

x = data[:, 0].reshape(m, 1) # X data 
y = data[:, 1].reshape(m, 1) # Y data

iters = m // 4
current_iter = 0
colors = ['red', 'blue', 'orange', 'yellow', 'green', 'cyan']

for i in range(3):
    plt.scatter(x[current_iter:current_iter+iters, :], y[current_iter:current_iter+iters, :], color=colors[i])
    plt.title('example1')
    plt.xlabel('Time')
    plt.ylabel('Values')
    plt.pause(10)
    plt.clf()
    current_iter = current_iter + iters

我们需要计算iterations的数量。这是由iters = m // 4完成的。现在,您使用for循环遍历迭代次数。要暂停和绘制图形,请使用plt.pause(10),它会显示图形10秒钟(您可以设置时间以方便使用)。然后使用plot.clf()清除它。这一直持续到loop结束。

希望这可以帮助!。

© www.soinside.com 2019 - 2024. All rights reserved.