Mikkel Rev
Mikkel Rev

Reputation: 901

Python matplotlib: Setting the default colours of the colour cycle

This article How to set the default color cycle for all subplots with matplotlib? teaches us how to use mpl.rcParams['axes.color_cycle'] = ['r', 'k', 'c'] to change the default colour 'r', 'k', 'c' cycle to red, black and cyan. However, how can I change the actual hex-triplets which 'r', 'k', 'c' represent? Say I want 'r' to represent #8F00FF (violet)? Or even better, how can I add colours, such that 'v' represent the hex-triplet #8F00FF? I hope to use them to implement them in mpl.rcParams['axes.color_cycle'].

Upvotes: 0

Views: 2082

Answers (1)

tom10
tom10

Reputation: 69182

You can specify the colors using hex notation directly in for the color cycle, like this:

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
from cycler import cycler # for mpl>2.2

mpl.rcParams['axes.prop_cycle'] = cycler(color=['r', '#8F00FF', '#A0F0A0'])

# for mpl < 2.2
#mpl.rcParams['axes.color_cycle'] = ['r', '#8F00FF', '#A0F0A0']

y = np.arange(5)
for i in range(7):
    plt.plot(y + i, linewidth=5)

plt.show()

enter image description here

Upvotes: 2

Related Questions