I want to make like this graph using python where the data in the text file looks like this (Each row represents the data for the day, 5 days of data are shown):
我想使用python制作这样的图形,其中文本文件中的数据看起来像这样(每行代表当天的数据,显示5天的数据):
-110 -108 -95 -92 -88 -87 -85 -75 -73 -69 -67 -59 -51 -49 -47 -42 -39 -35 -36 -36 -32 -27 -29 -32
-30 -23 -34 -33 -29 -25 -16 -18 -16 -17 -16 -11 -9 -13 -14 -17 -21 -20 -16 -17 -18 -15 -11 -12
-15 -12 -12 -12 -10 -14 -16 -15 -14 -12 -10 -9 -9 -9 -5 -5 -4 -6 -4 -8 -13 -17 -18 -19
-19 -20 -21 -21 -17 -14 -11 -11 -6 -3 -1 0 -1 -4 -5 -1 0 0 3 1 -5 -5 -6 -4
-3 0 1 2 1 3 1 4 12 9 6 6 8 11 14 15 18 12 5 3 5 10 12 16
-110 -108 -95 -92 -88 -87 -85 -75 -73 -69 -67 -59 -51 -49 -47 -42 -39 -35 -36 -36 -32 -27 -29 -32 -30 -30 -23 -34 -33 -29 -25 -16 -18 -16 -17 -16 -11 -9 -13 -14 -17 -21 -20 -16 -17 -18 -15 -11 -12 -15 -12 -12 -12 -10 -14 -16 -15 -14 -12 -10 -9 -9 -9 -5 -5 -4 -6 -4 -8 -13 -17 -18 -19 -19 -20 -21 -21 -17 -14 -11 -11 -6 -3 -1 0 -1 -4 -5 -1 0 0 3 1 -5 -5 -6 -4 -3 0 1 2 1 3 1 4 12 9 6 6 8 11 14 15 18 12 5 3 5 10 12 16
The problem is that when I run the code, the data for each day overlaps each other day.
问题是,当我运行代码时,每天的数据会相互重叠。
The code is below, how can I modify it to work as expected?
代码如下,如何修改它以按预期工作?
from pylab import xlabel,ylabel,xlim,ylim,show,plot
from numpy import loadtxt
data = loadtxt("2016_1.txt",float)
x = range(1,6)
y = data[:,1]
for i in range(1,23):
y= data[:,i+1]
plot(x,y)
xlim(0,6)
ylim(-120,100)
show()
This is what i get when I run it: Output
这是我运行时得到的结果:输出
1 个解决方案
#1
0
The issue is that the plot is called 23 times and all the times, x axis is limited to 5 units and y axis is getting overwritten and therefore 23 different plots.
问题是该图被称为23次并且所有时间,x轴被限制为5个单位并且y轴被覆盖,因此23个不同的图。
The solution is to append y into a single large array and then plot that array. See the below sample code:
解决方案是将y附加到单个大型数组中,然后绘制该数组。请参阅以下示例代码:
from pylab import xlabel,ylabel,xlim,ylim,show,plot
from numpy import loadtxt
import numpy as np
data = loadtxt("2016_1.txt",float)
#x = range(1,6) ## not needed
y = data[:,1]
z = y
for i in range(1,23):
y= data[:,i+1]
z = np.append(z,y) ## append y to z to create an entire range of values
plot(z)
#xlim(0,6) ## commented this to cover entire range
ylim(-120,100)
show()
#1
0
The issue is that the plot is called 23 times and all the times, x axis is limited to 5 units and y axis is getting overwritten and therefore 23 different plots.
问题是该图被称为23次并且所有时间,x轴被限制为5个单位并且y轴被覆盖,因此23个不同的图。
The solution is to append y into a single large array and then plot that array. See the below sample code:
解决方案是将y附加到单个大型数组中,然后绘制该数组。请参阅以下示例代码:
from pylab import xlabel,ylabel,xlim,ylim,show,plot
from numpy import loadtxt
import numpy as np
data = loadtxt("2016_1.txt",float)
#x = range(1,6) ## not needed
y = data[:,1]
z = y
for i in range(1,23):
y= data[:,i+1]
z = np.append(z,y) ## append y to z to create an entire range of values
plot(z)
#xlim(0,6) ## commented this to cover entire range
ylim(-120,100)
show()