Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 用于将颜色指定给绘图的For循环_Python_For Loop_Plot_Data Visualization - Fatal编程技术网

Python 用于将颜色指定给绘图的For循环

Python 用于将颜色指定给绘图的For循环,python,for-loop,plot,data-visualization,Python,For Loop,Plot,Data Visualization,我正在使用for循环为类分配数字,我在这方面取得了成功,但我发现很难根据使用相同for循环的类的数量同时插入不同的颜色,但我得到了错误:'list'对象不能解释为整数 代码如下: 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=

我正在使用for循环为类分配数字,我在这方面取得了成功,但我发现很难根据使用相同for循环的类的数量同时插入不同的颜色,但我得到了错误:'list'对象不能解释为整数

代码如下:

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);

错误在这一行

for i, c in range(n_class, colors):
这很简单,你所要做的就是得到列表的长度

for i, c in range(n_class, len(colors)):
但我不认为这是主要的错误,因为那样的话,它会被认为是错误的

range(5,6)
如果你想给所有的绘图都加上你需要的颜色

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

您没有正确使用
范围
。它的两个参数形式是range(低、高)。您实际需要的是枚举(颜色)中i,c的