1

I can set the color style for pyplot using

import matplotlib.pyplot as plt
plt.style.use('tableau-colorblind10')

And list the available color styles using

plt.style.available

But how can I actually access the colors from the color table? plt.style will set a color table for the plots, but I would like to be able to manually select them: select the first and second color used by plt.plot.

For example:

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
from numpy import pi, sin, cos

plt.rcParams['figure.dpi'] = 200

plt.style.use('tableau-colorblind10')

x = np.linspace(0, pi, 100)
sinx = [sin(xi) for xi in x]
cosx = [cos(xi) for xi in x]
sqrx = [xi*xi for xi in x]

plt.plot(x, sinx, label='sinx')
plt.plot(x, cosx, label='cosx')
plt.plot(x, sqrx, color='k', label='sqrx')
plt.legend()

Instead of the black color, I would like to use the blue color from tableau-colorblind10 again for the sqrx plot:

enter image description here

tmaric
  • 5,347
  • 4
  • 42
  • 75
  • 3
    Your question [has been asked already](https://stackoverflow.com/q/56211675/2749397) and has an [accepted answer](https://stackoverflow.com/a/56212312/2749397) that we can summarize as `colors = plt.rcParams['axes.prop_cycle'].by_key()['color']` – gboffi Jul 14 '19 at 15:13

2 Answers2

5

You can refer to the cycle colours as c1, c2 etc. For example:

plt.plot(x, sqrx, color='C1', label='sqrx')
tuomastik
  • 4,559
  • 5
  • 36
  • 48
Jody Klymak
  • 4,979
  • 2
  • 15
  • 31
4

Try:

plt.style.library['tableau-colorblind10']

[Out]:
RcParams({'axes.prop_cycle': cycler('color', ['#006BA4', '#FF800E', '#ABABAB', '#595959', '#5F9ED1', '#C85200', '#898989', '#A2C8EC', '#FFBC79', '#CFCFCF']),
          'patch.facecolor': '#006BA4'})

Thus, the color should be #006BA4 and set it to the line you wanna:

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
from numpy import pi, sin, cos

plt.rcParams['figure.dpi'] = 200

plt.style.use('tableau-colorblind10')

x = np.linspace(0, pi, 100)
sinx = [sin(xi) for xi in x]
cosx = [cos(xi) for xi in x]
sqrx = [xi*xi for xi in x]

plt.plot(x, sinx, label='sinx')
plt.plot(x, cosx, label='cosx')
plt.plot(x, sqrx, color='#006BA4', label='sqrx')
plt.legend()

enter image description here

steven
  • 2,130
  • 19
  • 38