tun*_*nuz 7 python plot colors matplotlib
我正在使用matplotlib.pyplot比较一些算法结果,但由于几行具有相同的确切颜色,因此很难理解发生了什么.有办法避免这种情况吗?我不认为pyplot只有七种颜色,有吗?
Uly*_*ses 11
对于Python 3,您可以从上面的解决方案中使用:
colormap = plt.cm.nipy_spectral
colors = [colormap(i) for i in np.linspace(0, 1,number_of_plots)]
ax.set_prop_cycle('color', colors)
Run Code Online (Sandbox Code Playgroud)
或者:
import seaborn as sns
colors = sns.color_palette("hls", number_of_plots)
ax.set_prop_cycle('color', colors)
Run Code Online (Sandbox Code Playgroud)
如果您知道要绘制多少个绘图,最好的事情是在之前定义色彩映射:
import matplotlib.pyplot as plt
import numpy as np
fig1 = plt.figure()
ax1 = fig1.add_subplot(111)
number_of_plots=10
colormap = plt.cm.nipy_spectral #I suggest to use nipy_spectral, Set1,Paired
ax1.set_color_cycle([colormap(i) for i in np.linspace(0, 1,number_of_plots)])
for i in range(1,number_of_plots+1):
ax1.plot(np.array([1,5])*i,label=i)
ax1.legend(loc=2)
Run Code Online (Sandbox Code Playgroud)
运用 nipy_spectral
小智 6
Matplotlib有七种以上的颜色.您可以通过多种方式指定颜色(请参阅http://matplotlib.sourceforge.net/api/colors_api.html).
例如,您可以使用html十六进制字符串指定颜色:
pyplot.plot(x, y, color='#112233')
Run Code Online (Sandbox Code Playgroud)
我还建议使用Seaborn。使用这个库,可以很容易地生成具有您需要的颜色数量的顺序或定性调色板。还有一个工具可以可视化调色板。例如:
import seaborn as sns
colors = sns.color_palette("hls", 4)
sns.palplot(colors)
plt.savefig("pal1.png")
colors = sns.color_palette("hls", 8)
sns.palplot(colors)
plt.savefig("pal2.png")
colors = sns.color_palette("Set2", 8)
sns.palplot(colors)
plt.savefig("pal3.png")
Run Code Online (Sandbox Code Playgroud)
这些是生成的调色板:
| 归档时间: |
|
| 查看次数: |
10185 次 |
| 最近记录: |