如何在python live plot中从本地txt文件读取数据(How to read data from local txt file in python live plot)

编程入门 行业动态 更新时间:2024-10-27 04:33:44
如何在python live plot中从本地txt文件读取数据(How to read data from local txt file in python live plot)

在这段代码中,我不想在代码中写数据。 我如何从sample.txt读取数据。 例如,我删除了year , unemployment , deficit数据设置的代码和我在txt文件中添加。 但我无法从txt文件中获取数据。

尝试了几个示例代码(不工作):

year=open('sample.txt','r').read() unemployment=open('sample.txt','r').read() deficit=open('sample.txt','r').read()

码:

import matplotlib.pyplot as plt import matplotlib.animation as animation import numpy as np import matplotlib.dates as mdates year = [1, 2, 3, 4, 5, 6] unemployment = [10.0, 9.5, 8.8, 7.8, 7.2, 5.8] deficit = [12.8, 12.2, 10.7, 9.3, 6.4, 5.8] plt.plot(year, unemployment, color='r', marker='o', linestyle='--', linewidth = 2.0, label='unemployment') plt.plot(year, deficit, color='b', marker='o', linestyle='--', linewidth = 2.0, label='deficit (%GDP)') plt.title('sdfsdfsdf') plt.xlabel('x one') plt.ylabel('y one') plt.legend(loc='upper right') plt.grid() plt.show()

Sample.txt的:

1 , 10.0 , 12.8 2 , 9.5 , 12.2 3 , 8.8 , 10.7 4 , 7.8 , 9.3 5 , 7.2 , 6.4 6 , 5.8 , 5.8

In this code, I don't want to write data in code side. How can I read the data from sample.txt. For example I deleted year, unemployment, deficit data set on code and I added in txt file. But I couldnt get data from txt file.

tried a few sample code (not worked):

year=open('sample.txt','r').read() unemployment=open('sample.txt','r').read() deficit=open('sample.txt','r').read()

Code:

import matplotlib.pyplot as plt import matplotlib.animation as animation import numpy as np import matplotlib.dates as mdates year = [1, 2, 3, 4, 5, 6] unemployment = [10.0, 9.5, 8.8, 7.8, 7.2, 5.8] deficit = [12.8, 12.2, 10.7, 9.3, 6.4, 5.8] plt.plot(year, unemployment, color='r', marker='o', linestyle='--', linewidth = 2.0, label='unemployment') plt.plot(year, deficit, color='b', marker='o', linestyle='--', linewidth = 2.0, label='deficit (%GDP)') plt.title('sdfsdfsdf') plt.xlabel('x one') plt.ylabel('y one') plt.legend(loc='upper right') plt.grid() plt.show()

Sample.txt:

1 , 10.0 , 12.8 2 , 9.5 , 12.2 3 , 8.8 , 10.7 4 , 7.8 , 9.3 5 , 7.2 , 6.4 6 , 5.8 , 5.8

最满意答案

提供.txt文件的格式,至少有两个选项:

使用Python的内置csv库 。 使用numpy的loadtxt()功能。

鉴于你已经在使用numpy库了,我会推荐后一种选择,它更干净简单。


修改后的代码(假设sample.txt是包含逗号分隔数据的文件的名称):

import matplotlib.pyplot as plt import matplotlib.animation as animation import numpy as np import matplotlib.dates as mdates year, unemployment, deficit = np.loadtxt("sample.txt", delimiter=",").transpose() plt.plot(year, unemployment, color='r', marker='o', linestyle='--', linewidth = 2.0, label='unemployment') plt.plot(year, deficit, color='b', marker='o', linestyle='--', linewidth = 2.0, label='deficit (%GDP)') plt.title('sdfsdfsdf') plt.xlabel('x one') plt.ylabel('y one') plt.legend(loc='upper right') plt.grid() plt.show()

产生的情节是 程序的输出

Providing the format of the .txt file, there are at least two options:

use Python's built-in csv library. use the numpy's loadtxt() functionality.

Given that you are already using the numpy library, I would recommend the latter option, it is cleaner and simpler.


Modified code (assume sample.txt is the name of the file that contains comma seperated data):

import matplotlib.pyplot as plt import matplotlib.animation as animation import numpy as np import matplotlib.dates as mdates year, unemployment, deficit = np.loadtxt("sample.txt", delimiter=",").transpose() plt.plot(year, unemployment, color='r', marker='o', linestyle='--', linewidth = 2.0, label='unemployment') plt.plot(year, deficit, color='b', marker='o', linestyle='--', linewidth = 2.0, label='deficit (%GDP)') plt.title('sdfsdfsdf') plt.xlabel('x one') plt.ylabel('y one') plt.legend(loc='upper right') plt.grid() plt.show()

The produced plot is output of the program

更多推荐

本文发布于:2023-08-08 00:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466775.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   数据   如何在   live   plot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!