详解matplotlib绘图样式(style)初探
样式是定义图表可视化外观的配置,它由一组预置的rcParams
参数构成。matplotlib
预置了一系列样式风格,可直接使用。
样式使用方法
样式相关模块为style
。
1. 显示本机可用样式
matplotlib.style.available
返回本机可用样式的列表。
列表只读,样式更新后,需要使用reload_library()
重新加载样式。
In [1]: import matplotlib.style as styleIn [2]: style.availableOut[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
样式可以将样式为恢复到默认样式。
该函数只会更新style
中定义的rcParams
配置,其余rcParams
配置保持不变。
参数style
有四种取值:
str
:样式名称或者样式文件的路径/url。通过style.available
查看可用的样式名称。dict
:以rcParams
配置项和值为键值对的字典。Path
:指向样式文件的Path
对象。list
:样式支持组合使用,将多个样式配置配置放置在列表中,matplotlib
将逐个执行列表中每个元素的配置,元素可以为str
、Path
或者dict
,列表右边的元素会覆盖前面元素的配置。
import matplotlib.pyplot as pltplt.bar([1,2,3],[1,2,3])plt.show()
import matplotlib.pyplot as pltplt.style.use('ggplot')plt.bar([1,2,3],[1,2,3])plt.show()
import matplotlib.pyplot as pltplt.style.use(['ggplot','dark_background'])plt.bar([1,2,3],[1,2,3])plt.show()
import matplotlib.pyplot as pltplt.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绘图样式内容请搜索 以前的文章或继续浏览下面的相关文章希望大家以后多多支持 !
最后更新于 2021-10-24 08:09:03 并被添加「」标签,已有 位童鞋阅读过。
本站使用「署名 4.0 国际」创作共享协议,可自由转载、引用,但需署名作者且注明文章出处
相关文章
- 如何让Win10系统的电脑自动运行?
- 2023新版阳光同学暑假衔接作业一1升二2升三3升四4升五5升六6上下册语文数学英语全国人教通用版小学生预习复习暑假总动员练习题
- input file上传文件样式支持html5的浏览器解决方案
- 垃圾袋混色装家用平口加厚卫生间一次性垃圾袋
- 100百分百黑巧克力纯可可脂零食礼物高纯度健身代餐无糖精朱古力