如何读取 .txt 文件来绘制图表

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

我得到了这个 .txt 文件,我每 15 秒构建一次并以特定格式保存它,如下所示:

13:57:06,389,492,140
13:57:21,139,181,484
13:57:36,483,294,414
13:57:51,373,382,195
13:58:06,425,360,277
13:58:21,394,310,307
13:58:36,327,291,303
13:58:52,461,352,133
13:59:07,242,426,492
13:59:22,389,448,296
13:59:37,301,443,366
13:59:52,212,229,106
14:02:08,112,442,347
14:02:23,284,361,329
14:02:39,353,438,467
14:02:54,254,385,401
14:03:09,217,125,410
14:03:24,295,266,326
14:03:39,389,209,196
14:03:54,323,203,147
14:04:10,346,230,365
14:04:25,143,181,261
14:04:40,294,104,230
14:04:55,376,264,357

如你所见 第一列值给出讲座的时间,第二、第三和第四列是从 PLC 读取的值。我想用这个值同时构建一个包含 3 个图的图表 我如何读取这个 .txt,分离这个值并绘制图表

我尝试使用 pandas 框架读取此 .txt,但它只读取 excel (CSV) 或者我可能没有完全使用 pandas

python plot
1个回答
0
投票

尝试:

import matplotlib.pyplot as plt
import pandas as pd

df = pd.read_csv("your_file.txt", header=None) # header=None <-- assuming you have no header row
df.columns = ["time", "col1", "col2", "col3"]  # give names to columns

plt.plot(df["time"], df["col1"], label="Col 1")
plt.plot(df["time"], df["col2"], label="Col 2")
plt.plot(df["time"], df["col3"], label="Col 3")

plt.xlabel("Time")
plt.ylabel("Value")
plt.title("My Graph")


plt.xticks(rotation=45)

plt.legend()
plt.show()

这将创建此图表:

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