在这段代码中,我不想在代码中写数据。 我如何从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.8In 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
更多推荐
发布评论