使用樣式表自定義繪圖
style包爲易於切換的繪圖『樣式』增加了支持,它們與matplotlibrc文件參數相同。 有一些預定義樣式由matplotlib提供。 例如,有一個名爲『ggplot』的預定義樣式,它模擬ggplot(R 的一種流行的繪圖軟件包)的美學。 爲了使用此樣式。首先,調出所有可以選擇的樣式列表
print(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']
%reset -f %clear # In[*] import numpy as np import matplotlib.pyplot as plt # In[*] # 生成一張12*4的圖 fig = plt.figure(figsize=(12,4)) # 生成第一個子圖在1行2列第一列位置 ax1 = fig.add_subplot(121) # 生成第二子圖在1行2列第二列位置 ax2 = fig.add_subplot(122) # 柱狀圖數據 x1 = [0.3, 1.7, 4, 6, 7] y1 = [5, 20, 15, 25, 10] # 折線圖數據 x2 = np.arange(0,10) y2 = [25,2,12,30,20,40,50,30,40,15] # 第一個子圖繪圖和設置 ax1.bar(x1,y1) ax1.set(xlabel='x',ylabel='y',title='title') # 第二個子圖繪圖和設置 ax2.plot(x2,y2) ax2.set(xlabel='x',ylabel='y',title='title') plt.show()
使用經典的style,plt.style.use('classic')
# In[*] plt.style.use('classic') # 生成一張12*4的圖 fig = plt.figure(figsize=(12,4)) # 生成第一個子圖在1行2列第一列位置 ax1 = fig.add_subplot(121) # 生成第二子圖在1行2列第二列位置 ax2 = fig.add_subplot(122) # 柱狀圖數據 x1 = [0.3, 1.7, 4, 6, 7] y1 = [5, 20, 15, 25, 10] # 折線圖數據 x2 = np.arange(0,10) y2 = [25,2,12,30,20,40,50,30,40,15] # 第一個子圖繪圖和設置 ax1.bar(x1,y1) ax1.set(xlabel='x',ylabel='y',title='title') # 第二個子圖繪圖和設置 ax2.plot(x2,y2) ax2.set(xlabel='x',ylabel='y',title='title') plt.show()
使用ggplot2
plt.style.use('ggplot') # 生成一張12*4的圖 fig = plt.figure(figsize=(12,4)) # 生成第一個子圖在1行2列第一列位置 ax1 = fig.add_subplot(121) # 生成第二子圖在1行2列第二列位置 ax2 = fig.add_subplot(122) # 柱狀圖數據 x1 = [0.3, 1.7, 4, 6, 7] y1 = [5, 20, 15, 25, 10] # 折線圖數據 x2 = np.arange(0,10) y2 = [25,2,12,30,20,40,50,30,40,15] # 第一個子圖繪圖和設置 ax1.bar(x1,y1) ax1.set(xlabel='x',ylabel='y',title='title') # 第二個子圖繪圖和設置 ax2.plot(x2,y2) ax2.set(xlabel='x',ylabel='y',title='title') plt.show()
最常用的style樣式
# In[*] plt.style.use('seaborn-whitegrid') # 生成一張12*4的圖 fig = plt.figure(figsize=(12,4)) # 生成第一個子圖在1行2列第一列位置 ax1 = fig.add_subplot(121) # 生成第二子圖在1行2列第二列位置 ax2 = fig.add_subplot(122) # 柱狀圖數據 x1 = [0.3, 1.7, 4, 6, 7] y1 = [5, 20, 15, 25, 10] # 折線圖數據 x2 = np.arange(0,10) y2 = [25,2,12,30,20,40,50,30,40,15] # 第一個子圖繪圖和設置 ax1.bar(x1,y1) ax1.set(xlabel='x',ylabel='y',title='title') # 第二個子圖繪圖和設置 ax2.plot(x2,y2) ax2.set(xlabel='x',ylabel='y',title='title') plt.show()
最整潔的一套樣式
plt.style.use('seaborn-white') # 生成一張12*4的圖 fig = plt.figure(figsize=(12,4)) # 生成第一個子圖在1行2列第一列位置 ax1 = fig.add_subplot(121) # 生成第二子圖在1行2列第二列位置 ax2 = fig.add_subplot(122) # 柱狀圖數據 x1 = [0.3, 1.7, 4, 6, 7] y1 = [5, 20, 15, 25, 10] # 折線圖數據 x2 = np.arange(0,10) y2 = [25,2,12,30,20,40,50,30,40,15] # 第一個子圖繪圖和設置 ax1.bar(x1,y1) ax1.set(xlabel='x',ylabel='y',title='title') # 第二個子圖繪圖和設置 ax2.plot(x2,y2) ax2.set(xlabel='x',ylabel='y',title='title') plt.show()