• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

详解matplotlib绘图样式(style)初探

python 搞代码 4年前 (2022-01-08) 26次浏览 已收录 0个评论
文章目录[隐藏]

这篇文章主要介绍了详解matplotlib绘图样式(style)初探,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

样式是定义图表可视化外观的配置,它由一组预置的rcParams参数构成。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样式可以将样式为恢复到默认样式。
该函数只会更新style中定义的rcParams配置,其余rcParams配置保持不变。

参数style有四种取值:

  • str:样式名称或者样式文件的路径/url。通过style.available查看可用的样式名称。
  • dict:以rcParams配置项和值为键值对的字典。
  • Path:指向样式文件的Path对象。
  • list:样式支持组合使用,将多个样式配置配置放置在列表中,matplotlib将逐个执行列表中每个元素的配置,元素可以为strPath或者dict,列表右边的元素会覆盖前面元来源gao($daima.com搞@代@#码网素的配置。
 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)初探的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:详解matplotlib绘图样式(style)初探

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址