[英]How to I set different colors to subsets of line plot iterations in matplotlib?
I am iteratively plotting the np.exp
results of 12 rows of data from a 2D array (12,5000)
, out_array
. 我正在迭代地绘制来自2D数组
(12,5000)
out_array
的12行数据的np.exp
结果。 All data share the same x values, ( x_d
). 所有数据共享相同的x值(
x_d
)。 I want the first 4 iterations to all plot as the same color, the next 4 to be a different color, and next 4 a different color...such that I have 3 different colors each corresponding to the 1st-4th, 5th-8th, and 9th-12th iterations respectively. 我希望所有图形的前4个迭代都使用相同的颜色,接下来的4个使用不同的颜色,接下来的4个使用不同的颜色...这样我可以使用3种不同的颜色,每种颜色分别对应于1th-4th,5th-8th ,以及第9-12次迭代。 In the end, it would also be nice to define these sets with their corresponding colors in a legend.
最后,最好在图例中定义这些集合及其相应的颜色。
I have researched cycler
( https://matplotlib.org/examples/color/color_cycle_demo.html ), but I can't figure out how to assign colors into sets of iterations > 1. (ie 4 in my case). 我已经研究了
cycler
( https://matplotlib.org/examples/color/color_cycle_demo.html ),但是我不知道如何将颜色分配给大于1的迭代集(即本例中为4)。 As you can see in my code example, I can have all 12 lines plotted with different (default) colors -or- I know how to make them all the same color (ie ...,color = 'r',...
) 如您在我的代码示例中所看到的,我可以用不同的(默认)颜色绘制所有12条线-或者-我知道如何使它们全部具有相同的颜色(即
...,color = 'r',...
)
plt.figure()
for i in range(out_array.shape[0]):
plt.plot(x_d, np.exp(out_array[i]),linewidth = 1, alpha = 0.6)
plt.xlim(-2,3)
I expect a plot like this, only with a total of 3 different colors, each corresponding to the chunks of iterations described above. 我期望这样的图,总共只有3种不同的颜色,每种颜色对应于上述迭代的大块。
An other solution 另一种解决方案
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(10)
color = ['r', 'g', 'b', 'p']
for i in range(12):
plt.plot(x, i*x, color[i//4])
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.