简体   繁体   English

用于为绘图 python 分配颜色的循环

[英]For loop for assigning colors to a plot python

I am working on a for loop to assign numbers to the classes and I am successful in that but I find it hard to simultaneously insert different colors based on the number of classes using the same for loop but I am getting the error : 'list' object cannot be interpreted as an integer我正在使用 for 循环为类分配数字,我在这方面取得了成功,但我发现很难根据使用相同 for 循环的类的数量同时插入不同的颜色,但我收到错误: 'list'对象不能解释为整数

Below is the code:下面是代码:

n_class = 5
colors = ['r', 'g', 'b', 'y','k', 'y']
# plotting
for i, c in range(n_class, colors):
    plt.plot(fpr[i], tpr[i], linestyle='--',color=[c], label= 'Class %d' %i )
        

plt.title('Multiclass ROC curve')
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive rate')
plt.legend(loc='best')
plt.savefig('Multiclass ROC',dpi=300);

The error is on this line错误在这一行

for i, c in range(n_class, colors):

It's pretty simple, all you have to do is to get the length of the list很简单,你要做的就是得到列表的长度

for i, c in range(n_class, len(colors)):

But I don't think that's the main error are, because then it will take it as但我不认为这是主要的错误,因为那样它就会把它当作

range(5,6)

if you want to give all the plots a color you'll need如果你想给所有的图一个你需要的颜色

for i in range(len(colors)):
    plt.plot(fpr[i], tpr[i], linestyle='--',color=colors[i], label= 'Class %d' %i )

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

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