詳解matplotlib繪圖樣式(style)初探
樣式是定義圖表可視化外觀的配置,它由一組預置的rcParams
參數(shù)構成。matplotlib
預置了一系列樣式風格,可直接使用。
樣式使用方法
樣式相關模塊為style
。
1. 顯示本機可用樣式
matplotlib.style.available
返回本機可用樣式的列表。
列表只讀,樣式更新后,需要使用reload_library()
重新加載樣式。
In [1]: import matplotlib.style as style In [2]: style.available Out[2]: ['Solarize_Light2', '_classic_test_patch', 'bmh', 'classic', 'dark_background', 'fast', 'fivethirtyeight', 'ggplot', 'grayscale', 'seaborn', 'seaborn-bright', 'seaborn-colorblind', 'seaborn-dark', 'seaborn-dark-palette', 'seaborn-darkgrid', 'seaborn-deep', 'seaborn-muted', 'seaborn-notebook', 'seaborn-paper', 'seaborn-pastel', 'seaborn-poster', 'seaborn-talk', 'seaborn-ticks', 'seaborn-white', 'seaborn-whitegrid', 'tableau-colorblind10']
2. 顯示樣式詳細設置
matplotlib.style.library
以字典的形式返回所有樣式的定義,字典鍵為樣式名稱,鍵為定義樣式的 RcParams
對象。
字典對象也是只讀的,更新樣式后,需要使用reload_library()
重新加載樣式。
In [6]: style.library['fast'] Out[6]: RcParams({'agg.path.chunksize': 10000, 'path.simplify': True, 'path.simplify_threshold': 1.0})
3. 重新加載樣式
matplotlib.style.reload_library()
重新加載樣式。
4. 使用樣式
matplotlib.style.use(style)
將matplotlib
的繪圖樣式設置為某種樣式。
使用default
樣式可以將樣式為恢復到默認樣式。
該函數(shù)只會更新style
中定義的rcParams
配置,其余rcParams
配置保持不變。
參數(shù)style
有四種取值:
str
:樣式名稱或者樣式文件的路徑/url。通過style.available
查看可用的樣式名稱。dict
:以rcParams
配置項和值為鍵值對的字典。Path
:指向樣式文件的Path
對象。list
:樣式支持組合使用,將多個樣式配置配置放置在列表中,matplotlib
將逐個執(zhí)行列表中每個元素的配置,元素可以為str
、Path
或者dict
,列表右邊的元素會覆蓋前面元素的配置。
import matplotlib.pyplot as plt plt.bar([1,2,3],[1,2,3]) plt.show()
import matplotlib.pyplot as plt plt.style.use('ggplot') plt.bar([1,2,3],[1,2,3]) plt.show()
import matplotlib.pyplot as plt plt.style.use(['ggplot','dark_background']) plt.bar([1,2,3],[1,2,3]) plt.show()
import matplotlib.pyplot as plt plt.subplot(221) plt.bar([1,2,3],[1,2,3]) plt.style.use('ggplot') plt.subplot(222) plt.bar([1,2,3],[1,2,3]) plt.style.use('grayscale') plt.subplot(223) plt.bar([1,2,3],[1,2,3]) plt.style.use(['ggplot','grayscale']) plt.subplot(224) plt.bar([1,2,3],[1,2,3]) plt.show()
樣式樣例
參見https://matplotlib.org/gallery/style_sheets/style_sheets_reference.html
自定義樣式
https://matplotlib.org/tutorials/introductory/customizing.html
到此這篇關于詳解matplotlib繪圖樣式(style)初探的文章就介紹到這了,更多相關matplotlib繪圖樣式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python?Pygame實戰(zhàn)之五款童年經(jīng)典游戲合集
本文為大家總結了五款利用Python+Pygame實現(xiàn)的童年經(jīng)典游戲:推箱子、滑雪、八分音符醬、保衛(wèi)蘿卜和飛機大戰(zhàn),快跟隨小編一起學習一下2022-04-04用Python的Tornado框架結合memcached頁面改善博客性能
這篇文章主要介紹了用Python的Tornado框架結合memcached頁面改善vLog性能,主要使用到了緩存來提升性能,需要的朋友可以參考下2015-04-04python的type?hints(類型標注、類型注解、類型提示)示例詳解
在Python編程中,類型提示(Type Hints)是一種強大的工具,它能夠幫助開發(fā)者在編寫代碼時明確表達變量、函數(shù)參數(shù)和返回值的預期類型,這篇文章主要給大家介紹了關于python的type?hints(類型標注、類型注解、類型提示)的相關資料,需要的朋友可以參考下2024-08-08Python+Dlib+Opencv實現(xiàn)人臉采集并表情判別功能的代碼
這篇文章主要介紹了Python+Dlib+Opencv實現(xiàn)人臉采集并表情判別,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07