matplotlib中提供了許多整體的風格樣式以供我們選擇。
>>> plt.style.available ['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']
接下來就展示一下其中的一些風格,基於此設計了一個可以繪制直方圖和正弦余弦曲線的函數,然后使用with的上下文管理器語法,測試不同的風格:
def hist_and_lines(): np.random.seed(10) fig, ax = plt.subplots(1, 2, figsize=(11, 4)) ax[0].hist(np.random.randn(1000)) x = np.linspace(0,10,100) ax[1].plot(np.sin(x)) ax[1].plot(np.cos(x)) ax[1].legend(['a', 'b', 'c'], loc='lower left')
with語句:
with plt.style.context('classic'): hist_and_lines()
plt.show()
將其中的‘classic’字符串替換成你想要的風格名稱,就能在with管理區內使用風格,而不影響后面的繪圖。
- 默認風格classic
- fivethirtyeight
- ggplot
- 灰度grayscale
- seaborn
matplotlib中支持的風格樣式共有二十多種,這里只是選出幾種來作為展示,具體可以自行選擇。