matplotlib
做爲Python
生態中最流行的數據可視化框架,雖然功能很是強大,但默認樣式比較簡陋,想要製做具備簡潔商務風格的圖表每每須要編寫衆多的代碼來調整各類參數。python
而今天要爲你們介紹的dufte
,就是用來經過簡短的代碼,對默認的matplotlib
圖表樣式進行自動改造的Python
庫:框架
經過pip install dufte
安裝完成後,咱們就能夠將dufte
的幾個關鍵API
穿插在常規matplotlib
圖表的繪製過程當中,目前主要有如下幾種功能:dom
dufte
最重要的功能是其自帶的主題風格,而在matplotlib
中有兩種設置主題的方式,一種是利用plt.style.use(主題)
來全局設置,通常不建議這種方式。code
另外一種方式則是如下列方式來在with
的做用範圍內局部使用主題:orm
# 局部主題設置 with plt.style.context(主題): # 繪圖代碼 ...
咱們今天就都使用第二種方式,首先導入演示所需的依賴庫,並從本地註冊思源宋體
:blog
import dufte import numpy as np import matplotlib.pyplot as plt from matplotlib import font_manager # 註冊本地思源宋體 fontproperties = font_manager.FontProperties(fname='NotoSerifSC-Regular.otf')
接下來咱們以折線圖和柱狀圖爲例:ip
# 折線圖示例 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
自帶了一套簡潔的繪圖風格,主張去除多餘的軸線,只保留必要的參考線,適用於咱們平常工做中的通用出圖需求。開發
除了前面介紹的總體主題風格以外,dufte
還自帶了一套圖例風格化策略,只須要在繪圖過程當中利用dufte.legend()
來代替matplotlib
原有的legend()
便可,如下面的折線圖爲例:it
# 折線圖示例 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()
就能夠自動添加出下列別緻的圖例說明:pip
不少時候咱們在繪製柱狀圖時,但願把每一個柱體對應的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
將來勢必會加入更多的實用功能,感興趣的朋友能夠對其持續關注。
以上就是本文的所有內容,歡迎在評論區分享你的觀點與建議。