将默认颜色旋转matplotlib更改为特定的颜色图

将默认颜色旋转matplotlib更改为特定的颜色图

本文介绍了将默认颜色旋转matplotlib更改为特定的颜色图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将matplotlib的标准颜色旋转更改为另一个颜色图.具体来说,我想使用"gdist_rainbow".那有可能吗,如果可以,我该如何实现呢?

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.

推荐答案

您需要为"axes.prop_cycle" rcParameter提供颜色循环.一个颜色循环由颜色列表组成.可以根据颜色图进行选择.请参见下面的示例:

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()

这篇关于将默认颜色旋转matplotlib更改为特定的颜色图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 01:50