7

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()
Jan
  • 1,389
  • 4
  • 17
  • 43

4 Answers4

8

The colors used for the successive lines are the one from a color cycler. In order to skip a color in this color cycle, you may call

ax._get_lines.prop_cycler.next()  # python 2
next(ax._get_lines.prop_cycler)   # python 2 or 3

The complete example would look like:

import numpy as np
import matplotlib.pyplot as plt

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 )

fig, ax = plt.subplots()
# skip first color
next(ax._get_lines.prop_cycler)
ax.plot(a[:,1:4])
ax.legend(labels=lab[1:4])
plt.show()
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
4

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

DavidG
  • 24,279
  • 14
  • 89
  • 82
  • 3
    The use of [`set_color_cycle`](https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.set_color_cycle.html#matplotlib.axes.Axes.set_color_cycle) is deprecated as of version 2.0. It should be replaced by [`set_prop_cycle`](https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.set_prop_cycle.html#matplotlib.axes.Axes.set_prop_cycle). – ImportanceOfBeingErnest Oct 10 '17 at 17:01
3

You can insert an extra call to ax.plot([],[]) before calling ax.plot(a[:,1:4]).

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([],[])
ax.plot(a[:,1:4])
ax.legend(labels=lab[1:4])
plt.show()
saintsfan342000
  • 1,724
  • 1
  • 13
  • 16
1

I have the impression that you want to make sure that every colone keeps a defined color. To do this you can create a color vector that matches each column to display. You can create a color vector. color = ["blue", "yellow", "green", "red"]

a = np.cumsum(np.cumsum(np.random.randn(7,4), axis=0), axis=1)

lab = np.array(["A","B","C","E"])
color = ["blue", "yellow", "green", "red"]

fig, ax = plt.subplots()
ax.plot(a, color = color)
ax.legend(labels=lab )
# plt.show()
fig, ax = plt.subplots()
ax.plot(a[:,1:4])
ax.legend(labels=lab[1:4], color = color[1:4])
plt.show()
Ludo Schmidt
  • 1,283
  • 11
  • 16