python中matplotlib图表类内置style属性详解

419 阅读2分钟

当前使用matplotlib version_:3.9.2

如何使用eg: plt.style.use('ggplot')

属性列表集合:

['Solarize_Light2', '_classic_test_patch', '_mpl-gallery', '_mpl-gallery-nogrid', 'bmh', 'classic', 'dark_background', 'fast', 'fivethirtyeight', 'ggplot', 'grayscale', 'seaborn-v0_8', 'seaborn-v0_8-bright', 'seaborn-v0_8-colorblind', 'seaborn-v0_8-dark', 'seaborn-v0_8-dark-palette', 'seaborn-v0_8-darkgrid', 'seaborn-v0_8-deep','seaborn-v0_8-muted', 'seaborn-v0_8-notebook', 'seaborn-v0_8-paper','seaborn-v0_8-pastel', 'seaborn-v0_8-poster', 'seaborn-v0_8-talk', 'seaborn-v0_8-ticks', 'seaborn-v0_8-white', 'seaborn-v0_8-whitegrid', 'tableau-colorblind10']

具体简要说明如下

  1. Solarize_Light2

    • 一种明亮的颜色方案,使用对比度高的颜色,适合打印和屏幕显示。
  2. _classic_test_patch_mpl-gallery

    • 这些似乎是 Matplotlib 内部测试或示例使用的风格,通常不用于常规绘图。
  3. bmh

    • 以《Beautiful Mathematical Hair》风格命名,使用蓝、橙、灰、绿等颜色。
  4. classic

    • Matplotlib 的默认风格,使用黑色线条和白色背景。
  5. dark_background

    • 黑色背景风格,适合在暗光环境下展示。
  6. fast

    • 一种快速渲染的风格,不包含任何背景网格或颜色,主要用于提高绘图速度。
  7. fivethirtyeight

    • 模仿 FiveThirtyEight 网站图表的风格,使用鲜艳的颜色和清晰的布局。
  8. ggplot

    • 模仿 R 语言的 ggplot2 库的风格,使用柔和的颜色和清晰的线条。
  9. grayscale

    • 灰度风格,所有颜色都转换为灰度,适合打印或在黑白显示设备上使用。
  10. seaborn-v0_8 系列:

    • 这些风格是与 Seaborn 0.8 版本兼容的 Matplotlib 风格。Seaborn 是基于 Matplotlib 的统计数据可视化库,提供了多种美观的风格。
    • seaborn-v0_8:Seaborn 默认风格。
    • seaborn-v0_8-bright:明亮风格。
    • seaborn-v0_8-colorblind:为色盲用户设计的风格。
    • seaborn-v0_8-dark:深色背景风格。
    • seaborn-v0_8-darkgrid:深色背景和网格风格。
    • seaborn-v0_8-deep:深色背景和深色调风格。
    • seaborn-v0_8-muted:柔和色调风格。
    • seaborn-v0_8-notebook:适合 Jupyter 笔记本的风格。
    • seaborn-v0_8-paper:适合论文和出版的风格。
    • seaborn-v0_8-pastel:粉笔色调风格。
    • seaborn-v0_8-poster:适合海报的风格。
    • seaborn-v0_8-talk:适合演讲的风格。
    • seaborn-v0_8-ticks:只有刻度线的风格。
    • seaborn-v0_8-white:白色背景风格。
    • seaborn-v0_8-whitegrid:白色背景和网格风格。
  11. tableau-colorblind10

    • 为色盲用户设计的,使用 Tableau 色盲友好调色板。

eg :论文风格

squares_plot.png 调试代码如下:

import matplotlib.pyplot as plt  
  
plt.style.use('seaborn-v0_8-paper')  
  
# 数据  
input_values = [1, 2, 3, 4, 5]  
squares = [1, 4, 9, 16, 25]  
  
# 创建图表和轴  
fig, ax = plt.subplots()  
  
# 绘制数据  
ax.plot(input_values, squares, linewidth=4)  
  
# 设置标题和轴标签  
ax.set_title('style:seaborn-v0_8-paper')  
ax.set_xlabel("Value")  
ax.set_ylabel("Square of Value")  
  
# 设置刻度标签的大小  
ax.tick_params(labelsize=14)  
  
# 显示图表  
# plt.show()  
plt.savefig('squares_plot.png', bbox_inches='tight')