偶然间发现matplotlib中设置样式表(style sheet)的功能,这个函数是plt.style,主要用来选择plot的呈现风格。
废话不多少,先看下效果
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
plt.style.use('dark_background')
np.random.seed(1)
data = np.random.randn(50)
plt.plot(data)
plt.show()
来看下有多少个可用的style
print(len(plt.style.available))
print(plt.style.available)
26
[‘bmh’, ‘classic’, ‘dark_background’,‘fast’, ‘fivethirtyeight’, ‘ggplot’, ‘grayscale’, ‘seaborn-bright’,‘seaborn-colorblind’, ‘seaborn-dark-palette’, ‘seaborn-dark’,‘seaborn-darkgrid’, ‘seaborn-deep’, ‘seaborn-muted’, ‘seaborn-notebook’, ‘seaborn-paper’,‘seaborn-pastel’, ‘seaborn-poster’, ‘seaborn-talk’, ‘seaborn-ticks’,‘seaborn-white’, ‘seaborn-whitegrid’, ‘seaborn’, ‘Solarize_Light2’,‘tableau-colorblind10’, ‘_classic_test’]
既然有26种(不过,有很多种没什么区别啦= =!)
接下来逐一试试这些风格
plt.style.use('bmh')
#也可以使用mpl.style.use(' bmh ')
plt.style.use(‘classic’)
plt.style.use(‘dark_background’)
plt.style.use(‘fast’)
plt.style.use(‘fivethirtyeight’)
plt.style.use(‘ggplot’) ##据说是模仿ggplot(R语言的一个的流行绘图包)的美学
plt.style.use(‘grayscale’)
plt.style.use(‘seaborn-bright’)
plt.style.use(‘seaborn-colorblind’)
plt.style.use(‘seaborn-dark-palette’)
plt.style.use(‘seaborn-dark’)
plt.style.use(‘seaborn-darkgrid’)
plt.style.use(‘seaborn-deep’)
plt.style.use(‘seaborn-muted’)
plt.style.use(‘seaborn-notebook’)
plt.style.use(‘seaborn-paper’)
plt.style.use(‘seaborn-pastel’)
plt.style.use(‘seaborn-poster’)
plt.style.use(‘seaborn-talk’)
plt.style.use(‘seaborn-ticks’)
plt.style.use(‘seaborn-white’)
plt.style.use(‘seaborn-whitegrid’)
plt.style.use(‘seaborn’)
plt.style.use(‘Solarize_Light2’)
plt.style.use(‘tableau-colorblind10’)
plt.style.use(’_classic_test’)
最后可以使用默认格式的
mpl.style.use(‘default’)
总结以上,
可以使用mpl.style也可以使用plt.style,效果一样;
可以应用 .available 方法查看具体的style名称
感兴趣的可以关注公众号“分享猿”啦~