ValueError(“color kwarg每个数据集必须有一种颜色”)?

时间:2011-10-13 16:36:00

标签: python matplotlib

我只需将数据保存到文件中并将其读出然后绘制直方图。然而,虽然我实际上没有改变原始代码,但看起来这个错误。谁能告诉我什么是错的?非常感谢。

这是hist()

的代码
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 个答案:

答案 0 :(得分:14)

这个问题是因为你已经将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关键字参数(颜色kwarg )也必须是N维的( 每个数据集一种颜色)。例如,对于上述情况:

color = ['b', 'r']

答案 1 :(得分:0)

检查系列的数据类型为numeric 这是我的问题...

df.series.apply(pd.to_numeric)

答案 2 :(得分:-1)

只需删除颜色属性即可。

删除color = 'r'color = 'c',一切正常。