Is it possible to ignore Matplotlib first default color for plotting?

 

Questions


Matplotlib plots each column of my matrix a with 4 columns by blue, yellow, green, red.
enter image description here

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)?
enter image description here

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

 

 

————————————————-

Answer

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:

enter image description here

enter image description here

matplotlib,plot,python,python-2.7

Facebook Comments

Post a comment