Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

python - change default color rotation matplotlib to specific colormap

I would like to change the standard color rotation of matplotlib to another colormap. To be specific, I would like to use 'gdist_rainbow'. Is that possible and if so, how can i achieve it?

I already have custom settings like

import matplotlib as mpl
import matplotlib.pyplot as plt
params = {'legend.fontsize': 'x-large',
         'axes.labelsize': 'xx-large',
         'axes.titlesize':'xx-large',
         'xtick.labelsize':'xx-large',
         'ytick.labelsize':'xx-large',
         'lines.markersize':8,
         'figure.autolayout':True}
plt.rcParams.update(params)

So I guess I am just looking for the right key to add.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to supply a color cycle to the "axes.prop_cycle" rcParameter. A color cycle consists of a list of colors. Those can be chosen according to a colormap. See example below:

import matplotlib.pyplot as plt
from cycler import cycler
import numpy as np

# get colormap
cmap=plt.cm.gist_rainbow
# build cycler with 5 equally spaced colors from that colormap
c = cycler('color', cmap(np.linspace(0,1,5)) )
# supply cycler to the rcParam
plt.rcParams["axes.prop_cycle"] = c


x = np.linspace(0,2*np.pi)
f = lambda x, phase:np.sin(x+phase)
for i in range(30):
    plt.plot(x,f(x,i/30.*np.pi) )

plt.show()

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...