我正在尝试为我的 jupyter 笔记本(Python 3)中的 matplotlib 设置默认 颜色图(不仅仅是特定图的颜色)。我找到了命令:plt.set_cmap("gray") 和 mpl.rc('image', cmap='gray'),它们应该将默认颜色图设置为灰色,但是这两个命令在执行过程中都被忽略了,我仍然得到旧的颜色图。
我试过这两个代码:
import matplotlib as mpl
mpl.rc('image', cmap='gray')
plt.hist([[1,2,3],[4,5,6]])
import matplotlib.pyplot as plt
plt.set_cmap("gray")
plt.hist([[1,2,3],[4,5,6]])
请您参考如下方法:
感谢 Chris 的评论,我发现了问题,我需要更改的不是默认的colormap,而是默认的color cycle。它在这里描述:How to set the default color cycle for all subplots with matplotlib?
import matplotlib as mpl
import matplotlib.pyplot as plt
from cycler import cycler
# Set the default color cycle
colors=plt.cm.gray(np.linspace(0,1,3))
mpl.rcParams['axes.prop_cycle'] = mpl.cycler(color=colors)
plt.hist([[1,2,3],[4,5,6]])
