ValueError("color参数每个数据集必须有一个颜色")?
我只是简单地把数据保存到文件里,然后再读取出来画直方图。但是,尽管我没有对原始代码做任何修改,却出现了这个错误。有人能告诉我哪里出问题了吗?非常感谢。
这是用来画直方图的代码:
f_120 = plt.figure(1)
plt.hist(tfirst_list, bins=6000000, normed = True, histtype ="step", cumulative = True, color = 'g',label = 'first answer')
plt.axvline(x = 30, ymin = 0, ymax = 1, color = 'r', linestyle = '--', label = '30 min')
plt.axvline(x = 60, ymin = 0, ymax = 1, color = 'c', linestyle = '--', label = '1 hour')
plt.legend()
plt.ylabel('Percentage of answered questions')
plt.xlabel('Minutes elapsed after questions are posted')
plt.title('Cumulative histogram: time elapsed \n before questions receive answer (first 2 hrs)')
plt.ylim(0,1)
plt.xlim(0,120)
f_120.show()
f_2640 = plt.figure(2)
plt.hist(tfirst_list, bins=6000000, normed = True, histtype ="step", cumulative = True, color = 'g',label = 'first answer')
plt.axvline(x = 240, ymin = 0, ymax = 1, color = 'r', linestyle = '--', label = '4 hours')
plt.axvline(x = 1440, ymin = 0, ymax = 1, color = 'c', linestyle = '--', label = '1 day')
plt.legend(loc= 4)
plt.ylabel('Percentage of answered questions')
plt.xlabel('Minutes elapsed after questions are posted')
plt.title('Cumulative histogram: time elapsed \n before questions receive answer (first 48)')
plt.ylim(0,1)
plt.xlim(0,2640)
f_2640.show()
下面是错误的完整信息:
plt.hist(tfirst_list, bins=6000000, normed = True, histtype ="step",
cumulative = True, color = 'b',label = 'first answer')
File "C:\Python26\lib\site-packages\matplotlib\pyplot.py", line 2160, in hist
ret = ax.hist(x, bins, range, normed, weights, cumulative, bottom, histtype,
align, orientation, rwidth, log, color, label, **kwargs)
File "C:\Python26\lib\site-packages\matplotlib\axes.py", line 7606, in hist
raise ValueError("color kwarg must have one color per dataset")
ValueError: color kwarg must have one color per dataset
3 个回答
-1
只需要删除颜色属性就可以了。
把 color = 'r'
和 color = 'c'
删除掉,这样问题就会暂时消失——这不是解决办法,但算是向前迈出了一步。
0
检查一下这个 Series 的数据类型是不是 numeric
(数字型)。这就是我遇到的问题...
df.series.apply(pd.to_numeric)
21
这个问题是因为你把tfirst_list定义成了一个N维数组。
举个例子:
tfirst_list = [1, 2, 3, 4, 5, 6, 7, 8] #works
tfirst_list = [[1, 2, 3, 4], [5, 6, 7, 8]] #produces the Exception you have
如果你在使用N维数据(N代表数据集的数量),那么color
这个参数(也就是color kwarg)也必须是N维的(也就是说每个数据集要有一种颜色)。比如在上面的例子中:
color = ['b', 'r']