1

I would like to change matplotlib's default colors. As I already tried by myself and found a solution to my problem by changing the default color cycler, this is not the perfect solution I was looking for :

import matplotlib as mpl

mpl.rcParams['axes.prop_cycle'] = mpl.cycler(color=["#013c9b",# my Blue
                                                    "#e0012e",# my Red
                                                    "#80cd59",# my Green
                                                    "#f6960a",# my Orange
                                                    "#4f94d4",# my Light Blue
                                                    "#ffd800",# my Yellow
                                                    "#6f7072",# my Grey
                                                    ])

x = np.linspace(1, 10, 1000)
for i in range(1, 6):
  y = np.sin(i*x) * i
  plt.plot(x,y)

Solution partially working

While I achieve the results with the default color cycle, it is not very convenient if I want to plot with one of my specific color as I need to do something like that :

y1_in_red = np.sin(2*x) * 2
y2_in_green = np.sin(3*x) * 3

my_Red = "#e0012e"    # <-it's not very convenient/clean to do that
my_Blue = "#80cd59" # <-it's not very convenient/clean to do that

plt.plot(x, y1_in_red, color=my_Red)
plt.plot(x, y2_in_green, color=my_Blue)

enter image description here

The first problem with this solution is the yticks but i can set it back to normal. The second problem is it is not as clean/as efficient as I want. I would like to know if there is a way to change the default red (and the other colors) of matplotlib so I can just do something like that to achieve the same result as above :

y1_in_red = np.sin(2*x) * 2
y2_in_green = np.sin(3*x) * 3

# ...
# some code
# ...

plt.plot(x, y1_in_red, color='r')
plt.plot(x, y2_in_green, color='b')

1 Answer 1

2

The colors come from a dictionary of named colors you can access with matplotlib.colors.get_named_colors_mapping(), so you could for example replace r and b with your red and blue

from matplotlib.colors import get_named_colors_mapping
get_named_colors_mapping()["r"] = "#e0012e"
get_named_colors_mapping()["b"] = "#013c9b"

Then you can use them as you normally would

import matplotlib.pyplot as plt 
plt.plot([1, 2, 3], [1, 2, 3], "r")  # will use your red
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.