繁体   English   中英

输入错误,然后输入值错误:x 和 y 必须具有相同的第一维

[英]Type error, and then ValueError: x and y must have same first dimension

所以我有这个:

def graph_data(dateList, countList, name):
xarray = [0,1,2,3,4,5,6]
xarray = np.asarray(xarray)
myticks = dateList
plt.figure(figsize=(9,5))
plt.xticks(xarray, myticks)
plt.plot(xarray, countList, color='r', linewidth='3.0')
plt.ylabel("Activity")
plt.xlabel("Date")
plt.title(name + "'s Activity for the past 7 days")
plt.savefig("graph.png")

效果很好,但是一旦我在不同的 VPS 上运行它(是的,我已经用 pip 安装了所有依赖项),但是它给了我一个类型错误,指出在 plt.plot 中,countList 需要是浮动的,所以我把代码改成这样:

def graph_data(dateList, countList, name):
for n in countList:
    fixedList = []
    fixedList.append(float(n))
xarray = [0,1,2,3,4,5,6]
myticks = dateList
plt.figure(figsize=(9,5))
plt.xticks(xarray, myticks)
plt.plot(xarray, fixedList, color='r', linewidth='3.0')
plt.ylabel("Activity")
plt.xlabel("Date")
plt.title(name + "'s Activity for the past 7 days")
plt.savefig("graph.png")

但后来它给了我这个错误:

 "have shapes {} and {}".format(x.shape, y.shape))
 ValueError: x and y must have same first dimension, but have shapes (7,) and (1,)

所以我添加了xarray = np.asarray(xarray)fixedList = np.asarray(fixedList)但它仍然给了我形状错误。 我究竟做错了什么?

当然,您需要确保countListxarray具有相同数量的元素。 假设是这种情况,问题是您在每次循环迭代中创建一个空列表并向其附加一个元素。 在下一次迭代中,您重新创建一个空列表,再次添加一个元素。

相反,您需要在循环外创建fixedList

fixedList = []
for n in countList:
    fixedList.append(float(n)) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM