脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - 详解matplotlib绘图样式(style)初探

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

2021-09-01 00:34mighty13 Python

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

样式是定义图表可视化外观的配置,它由一组预置的rcParams参数构成。matplotlib预置了一系列样式风格,可直接使用。

样式使用方法

样式相关模块为style

1. 显示本机可用样式

matplotlib.style.available返回本机可用样式的列表。
列表只读,样式更新后,需要使用reload_library()重新加载样式。

  1. In [1]: import matplotlib.style as style
  2. In [2]: style.available
  3. Out[2]:
  4. ['Solarize_Light2',
  5. '_classic_test_patch',
  6. 'bmh',
  7. 'classic',
  8. 'dark_background',
  9. 'fast',
  10. 'fivethirtyeight',
  11. 'ggplot',
  12. 'grayscale',
  13. 'seaborn',
  14. 'seaborn-bright',
  15. 'seaborn-colorblind',
  16. 'seaborn-dark',
  17. 'seaborn-dark-palette',
  18. 'seaborn-darkgrid',
  19. 'seaborn-deep',
  20. 'seaborn-muted',
  21. 'seaborn-notebook',
  22. 'seaborn-paper',
  23. 'seaborn-pastel',
  24. 'seaborn-poster',
  25. 'seaborn-talk',
  26. 'seaborn-ticks',
  27. 'seaborn-white',
  28. 'seaborn-whitegrid',
  29. 'tableau-colorblind10']

2. 显示样式详细设置

matplotlib.style.library以字典的形式返回所有样式的定义,字典键为样式名称,键为定义样式的 RcParams对象。
字典对象也是只读的,更新样式后,需要使用reload_library()重新加载样式。

  1. In [6]: style.library['fast']
  2. Out[6]:
  3. RcParams({'agg.path.chunksize': 10000,
  4. 'path.simplify': True,
  5. '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,列表右边的元素会覆盖前面元素的配置。
  1. import matplotlib.pyplot as plt
  2. plt.bar([1,2,3],[1,2,3])
  3. plt.show()

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

  1. import matplotlib.pyplot as plt
  2. plt.style.use('ggplot')
  3. plt.bar([1,2,3],[1,2,3])
  4. plt.show()

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

  1. import matplotlib.pyplot as plt
  2. plt.style.use(['ggplot','dark_background'])
  3. plt.bar([1,2,3],[1,2,3])
  4. plt.show()

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

  1. import matplotlib.pyplot as plt
  2. plt.subplot(221)
  3. plt.bar([1,2,3],[1,2,3])
  4. plt.style.use('ggplot')
  5. plt.subplot(222)
  6. plt.bar([1,2,3],[1,2,3])
  7. plt.style.use('grayscale')
  8. plt.subplot(223)
  9. plt.bar([1,2,3],[1,2,3])
  10. plt.style.use(['ggplot','grayscale'])
  11. plt.subplot(224)
  12. plt.bar([1,2,3],[1,2,3])
  13. plt.show()

样式样例

参见https://matplotlib.org/gallery/style_sheets/style_sheets_reference.html

自定义样式

https://matplotlib.org/tutorials/introductory/customizing.html

到此这篇关于详解matplotlib绘图样式(style)初探的文章就介绍到这了,更多相关matplotlib绘图样式内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/mighty13/article/details/111836980

延伸 · 阅读

精彩推荐