1 簡介
matplotlib
作為Python
生態中最流行的資料視覺化框架,雖然功能非常強大,但預設樣式比較簡陋,想要製作具有簡潔商務風格的圖表往往需要編寫眾多的程式碼來調整各種引數。
而今天要為大家介紹的dufte
,就是用來通過簡短的程式碼,對預設的matplotlib
圖表樣式進行自動改造的Python
庫:
2 利用dufte自動改造matplotlib圖表
通過pip install dufte
安裝完成後,我們就可以將dufte
的幾個關鍵API
穿插在常規matplotlib
圖表的繪製過程中,目前主要有以下幾種功能:
2.1 主題設定
dufte
最重要的功能是其自帶的主題風格,而在matplotlib
中有兩種設定主題的方式,一種是利用plt.style.use(主題)
來全域性設定,一般不建議這種方式。
另一種方式則是以下列方式來在with
的作用範圍內區域性使用主題:
# 區域性主題設定
with plt.style.context(主題):
# 繪圖程式碼
...
我們今天就都使用第二種方式,首先匯入演示所需的依賴庫,並從本地註冊思源宋體
:
import dufte
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import font_manager
# 註冊本地思源宋體
fontproperties = font_manager.FontProperties(fname='NotoSerifSC-Regular.otf')
接下來我們以折線圖和柱狀圖為例:
- 折線圖
# 折線圖示例
with plt.style.context(dufte.style):
x = range(100)
y = np.random.standard_normal(100).cumsum()
fig, ax = plt.subplots(figsize=(10, 5), facecolor='white', edgecolor='white')
ax.plot(x, y, linestyle='-.', color='#607d8b')
ax.set_xlabel('x軸示例', fontproperties=fontproperties, fontsize=16)
ax.set_ylabel('y軸示例', fontproperties=fontproperties, fontsize=16)
ax.set_title('折線圖示例', fontproperties=fontproperties, fontsize=20)
fig.savefig('圖2.png', dpi=300, bbox_inches='tight')
- 柱狀圖
# 柱狀圖示例
with plt.style.context(dufte.style):
x = range(25)
y = np.random.standard_normal(25)
fig, ax = plt.subplots(figsize=(10, 5), facecolor='white', edgecolor='white')
ax.bar(x, y)
ax.set_xlabel('x軸示例', fontproperties=fontproperties, fontsize=16)
ax.set_ylabel('y軸示例', fontproperties=fontproperties, fontsize=16)
ax.set_title('柱狀圖示例', fontproperties=fontproperties, fontsize=20)
fig.savefig('圖3.png', dpi=300, bbox_inches='tight')
可以看到,dufte
自帶了一套簡潔的繪圖風格,主張去除多餘的軸線,只保留必要的參考線,適用於我們日常工作中的通用出圖需求。
2.2 自動圖例美化
除了前面介紹的整體主題風格之外,dufte
還自帶了一套圖例風格化策略,只需要在繪圖過程中利用dufte.legend()
來代替matplotlib
原有的legend()
即可,以下面的折線圖為例:
# 折線圖示例
with plt.style.context(dufte.style):
x = range(100)
y1 = np.random.randint(-5, 6, 100).cumsum()
y2 = np.random.randint(-5, 10, 100).cumsum()
y3 = np.random.randint(-5, 6, 100).cumsum()
fig, ax = plt.subplots(figsize=(10, 5), facecolor='white', edgecolor='white')
ax.plot(x, y1, linestyle='dotted', label='Series 1')
ax.plot(x, y2, linestyle='dashed', label='Series 2')
ax.plot(x, y3, linestyle='dashdot', label='Series 3')
ax.set_xlabel('x軸示例', fontproperties=fontproperties, fontsize=16)
ax.set_ylabel('y軸示例', fontproperties=fontproperties, fontsize=16)
dufte.legend()
ax.set_title('dufte.legend()示例', fontproperties=fontproperties, fontsize=20)
fig.savefig('圖4.png', dpi=300, bbox_inches='tight')
可以看到,對於多系列圖表,只需要一行dufte.legend()
就可以自動添加出下列別緻的圖例說明:
2.3 柱狀圖自動標註
很多時候我們在繪製柱狀圖時,希望把每個柱體對應的y值標註在柱體上,而通過dufte.show_bar_values()
,只要其之前的繪圖流程中設定了xticks
,它就會幫我們自動往柱體上標註資訊:
# 柱狀圖示例
with plt.style.context(dufte.style):
x = range(15)
y = np.random.randint(5, 15, 15)
fig, ax = plt.subplots(figsize=(10, 5), facecolor='white', edgecolor='white')
ax.bar(x, y)
ax.set_xticks(x)
ax.set_xticklabels([f'專案{i}' for i in x], fontproperties=fontproperties, fontsize=10)
dufte.show_bar_values()
ax.set_xlabel('x軸示例', fontproperties=fontproperties, fontsize=16)
ax.set_ylabel('y軸示例', fontproperties=fontproperties, fontsize=16)
ax.set_title('柱狀圖示例', fontproperties=fontproperties, fontsize=20)
fig.savefig('圖5.png', dpi=300, bbox_inches='tight')
作為一個處於開發初期的庫,dufte
未來勢必會加入更多的實用功能,感興趣的朋友可以對其持續關注。
以上就是本文的全部內容,歡迎在評論區分享你的觀點與建議。