Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
913 views
in Technique[技术] by (71.8m points)

python - ValueError("color kwarg must have one color per dataset")?

I just simply save the data into files and read them out and then draw histogram. However, it appears this mistake though I changed nothing from the original code actually. Could anyone tell me what's wrong? Thanks a lot.

Here is the code for 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 
 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 
 before questions receive answer (first 48)')
plt.ylim(0,1)
plt.xlim(0,2640)

f_2640.show()

And there is the full-text for the error:

 plt.hist(tfirst_list, bins=6000000, normed = True, histtype ="step",
          cumulative = True, color = 'b',label = 'first answer')

  File "C:Python26libsite-packagesmatplotlibpyplot.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:Python26libsite-packagesmatplotlibaxes.py", line 7606, in hist
    raise ValueError("color kwarg must have one color per dataset")

ValueError: color kwarg must have one color per dataset
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This problem is because you have defined tfirst_list as a N-dimensional array.
For example:

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

If you are using N-dimensional data (N datasets), then the color keyword argument (color kwarg) must be also N-dimensional (one color per dataset). For example for the case above:

color = ['b', 'r']

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...