是否可以忽略用于绘图的 Matplotlib 第一个默认颜色? [英] Is it possible to ignore Matplotlib first default color for plotting?
本文介绍了是否可以忽略用于绘图的 Matplotlib 第一个默认颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Matplotlib 用蓝色、黄色、绿色、红色的 4 列绘制我的矩阵 a
的每一列.
然后,我只绘制矩阵 a[:,1:4]
中的第二、第三、第四列.是否可以让 Matplotlib 从默认值开始忽略蓝色并从黄色开始(所以我的每一行都与以前的颜色相同)?
a = np.cumsum(np.cumsum(np.random.randn(7,4),axis=0),axis=1)lab = np.array(["A","B","C","E"])图, ax = plt.subplots()ax.plot(a)ax.legend(标签=实验室)# plt.show()图, ax = plt.subplots()ax.plot(a[:,1:4])ax.legend(标签=实验室[1:4])plt.show()
解决方案
为了跳过第一种颜色,我建议使用
获取当前颜色的列表plt.rcParams['axes.prop_cycle'].by_key()['color']
如
Matplotlib plots each column of my matrix a
with 4 columns by blue, yellow, green, red.
Then, I plot only the second, third, fourth columns from matrix a[:,1:4]
. Is it possible to make Matplotlib ignore blue from default and start from yellow (so my every lines get the same color as previous)?
a = np.cumsum(np.cumsum(np.random.randn(7,4), axis=0), axis=1)
lab = np.array(["A","B","C","E"])
fig, ax = plt.subplots()
ax.plot(a)
ax.legend(labels=lab )
# plt.show()
fig, ax = plt.subplots()
ax.plot(a[:,1:4])
ax.legend(labels=lab[1:4])
plt.show()
解决方案
In order to skip the first color I would suggest getting a list of the current colors by using
plt.rcParams['axes.prop_cycle'].by_key()['color']
As shown in this question/answer. Then set the color cycle for the current axes by using:
plt.gca().set_color_cycle()
Therefore your full example would be:
a = np.cumsum(np.cumsum(np.random.randn(7,4), axis=0), axis=1)
lab = np.array(["A","B","C","E"])
colors = plt.rcParams['axes.prop_cycle'].by_key()['color']
fig, ax = plt.subplots()
ax.plot(a)
ax.legend(labels=lab )
fig1, ax1 = plt.subplots()
plt.gca().set_color_cycle(colors[1:4])
ax1.plot(a[:,1:4])
ax1.legend(labels=lab[1:4])
plt.show()
Which gives:
这篇关于是否可以忽略用于绘图的 Matplotlib 第一个默认颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文