[英]Python plot for beginners - ValueError: x and y must have same first dimension (code inside)
我是一位绝对的python初学者,他希望/必须编程一个Web采集器脚本,以从天气预报网页中提取数据以进行绘制。
我输入了raw_input,以便人们可以选择要查看天气详细信息的城市,这与该情节有关:
import os
import matplotlib.pyplot as plt
from datetime import datetime
path = "data\\"
names = os.listdir(path)
namelist=[]
for element in names:
element = element[:-4]
namelist.append(element)
print namelist
while True:
nennDenOrt = raw_input("tell me the name of your city\n")
if nennDenOrt in namelist:
print "here's your weather: "
temp = []
relfeuc = []
ns = []
sonne = []
timestamps = []
with open(path + nennDenOrt + ".dat", "r") as infile:
for line in infile:
line = line.strip() # remove whitespaces newlines etc
data = line.split(",")
temp.append[2]
relfeuc.append[3]
ns.append[4]
sonne.append(data[5])
print data[0]
actual_time = datetime.strptime(str(data[0]),"%Y-%m-%d %H-%M-%S")
timestamps.append(actual_time)
print data
plt.figure(1) # the first figure
plt.title("Ihr Wetter für " + nennDenOrt + " von " + timestamps[0] + " bis " + timestamps[1])
plt.subplot(221)
plt.plot(timestamps,temp)
plt.xlabel('Zeit')
plt.ylabel('°C')
...
我用于绘图的数据在* .dat内部,如下所示:
2015-06-17 18-38-30,1184,12.9,68,0.0,45
2015-06-17 18-38-33,1184,12.9,68,0.0,45
2015-06-17 18-38-36,1184,12.9,68,0.0,45
2015-06-17 18-38-40,1184,12.9,68,0.0,45 ...
(时间[0]将是x轴,其他4个值将分别在4个不同绘图的y轴上。)
我收到此错误消息,不知道如何解决该问题:
plt.plot(timestamps,temp)
File "C:\Python27\ArcGIS10.2\lib\site-packages\matplotlib\pyplot.py", line 2987, in plot
ret = ax.plot(*args, **kwargs)
File "C:\Python27\ArcGIS10.2\lib\site-packages\matplotlib\axes.py", line 4137, in plot
for line in self._get_lines(*args, **kwargs):
File "C:\Python27\ArcGIS10.2\lib\site-packages\matplotlib\axes.py", line 317, in _grab_next_args
for seg in self._plot_args(remaining, kwargs):
File "C:\Python27\ArcGIS10.2\lib\site-packages\matplotlib\axes.py", line 295, in _plot_args
x, y = self._xy_from_xy(x, y)
File "C:\Python27\ArcGIS10.2\lib\site-packages\matplotlib\axes.py", line 237, in _xy_from_xy
raise ValueError("x and y must have same first dimension")
ValueError: x and y must have same first dimension
也许你们中有些人该怎么办? 在此先多谢!
您正在使用此代码-
plt.plot(timestamps,temp)
但是,在代码中没有任何地方给temp
变量sonne
任何值,您的意思是改为使用sonne
变量吗? 喜欢 -
plt.plot(timestamps,sonne)
下面的代码也是错误的-
temp.append[2]
relfeuc.append[3]
ns.append[4]
我不认为temp
或relfeuc
有这样的属性append
是标化。 您需要做-
temp.append(data[2])
relfeuc.append(data[3])
ns.append(data[4])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.