matplotlib 使用简明教程(七)-样式定义

matplotlib 使用简明教程(七)-样式定义

    • 定义 matplotlib 样式文件
    • 使用样式文件
      • 默认样式文件的使用
      • matplotlib 使用某种样式
      • 查询当前可用的样式列表
      • 在某个块内使用某种样式
    • 直接定义样式参数
      • 直接设置 matplotlib.rcParams
      • 恢复默认的配置状态
    • 在各类绘制函数中定义样式d

定义 matplotlib 样式文件

官网提供了一份 matplotlibrc 文件的样例参考,这里不作赘述:

https://matplotlib.org/tutorials/introductory/customizing.html#sphx-glr-tutorials-introductory-customizing-py

使用样式文件

默认样式文件的使用

matplotlib 会在以下地方搜索名称为 matplotlibrc 的这个文件,一旦找到就会停止继续搜索并使用该文件内定义的样式作为默认样式

  1. 当前工作目录
  2. $MATPLOTLIBRC 定义的文件或文件夹内
  3. 用户自定义的配置文件位置,可以通过 matplotlib.get_configdir() 来查询,通过 $MPLCONFIGDIR 环境变量设置
  4. INSTALL/matplotlib/mpl-data/matplotlibrc,其中 INSTALL 是 matplotlib 安装的位置

可以通过 matplotlib.matplotlib_fname() 查询当前使用的默认样式文件

matplotlib 使用某种样式

plt.style.use()

  • 参数可以是一个 URL 或者路径,指向自己定义的 mplstyle 文件
  • 可以把自己的 mplstyle 文件放到 mpl_configdir/stylelib 文件夹下,这样就能通过文件的名称来使用定义的样式,其中 mpl_configdir 可以通过 matplotlib.get_configdir() 来查询
  • 参数也可以是一个列表,这样就会整合多个 mplstyle 中的样式

查询当前可用的样式列表

plt.style.available

在某个块内使用某种样式

plt.style.context() 使用方式见示例:

with plt.style.context(('dark_background')):
    plt.plot(np.sin(np.linspace(0, 2 * np.pi)), 'r-o')
plt.show()

直接定义样式参数

matplotlib.rcParams 这个全局字典里存放了大量的样式定义,可以直接修改。

直接设置 matplotlib.rcParams

mpl.rcParams['group.key'] = valuempl.rc(group, key1 = value2, key2 = value2)

直接设置 matplotlib.rcParams 字典里某个 group 的某个 key 为 value,例如:

rc('lines', linewidth=2, color='r')

rcParams['lines.linewidth'] = 2
rcParams['lines.color'] = 'r'

恢复默认的配置状态

mpl.rcdefaults()matplotlib.rc_file_defaults()

其中 rcdefaults() 是恢复成 matplotlib 内建的样式,rc_file_defaults() 是恢复成最初导入的 rc 文件定义的样式。

在各类绘制函数中定义样式d

matplotlib 的绘制函数有很多,每个函数支持的样式定义也各不相同,大部分文档都有说明。
但是有的函数参数,官方文档也没有描述清楚。给大家一个小技巧,就是看一下这个函数的返回值类型,看一下这个类型的描述文档,这里会有最全的样式定义。

举个例子:

>>> help(plt.plot) # 查看 plt.plot 函数帮助
>>> lines = plt.plot([1, 2, 4, 8])
>>> lines
[]
>>> line = lines[0]
>>> help(line) # 查看 matplotlib.lines.Line2D 的帮助

有些时候函数的样式定义是“复合”的,例如 line 可以定义 linewidth, linestyle, color, marker 等属性,但是某些函数直接使用了“复合”参数来一次性定义多个属性。
例如 plt.plot 函数使用 fmt 参数, plt.stem 函数使用了 linefmt, markerfmt, basefmt 参数

这些“复合”参数,可用的内容基本是固定的:

线的样式 描述
'-' 实线
'--' 杠线
'-.' 点杠线
':' 点线
标记点的样式 描述
'.'
','
'o'
'v' 下三角
'^' 上三角
'<' 左三角
'>' 油三角
'1' 下三角
'2' 上三角
'3' 左三角
'4' 油三角
's' 方形
'p' 五边形
'*' 星形
'h' 六边形1
'H' 六边形2
'+' 加号
'x' 叉号
'D' 钻石
'd' 细钻石
'\|' 短竖线
'_' 短横线
颜色的样式 描述
'b' 蓝色
'g' 绿色
'r' 红色
'c' 青色
'm' 品红
'y' 黄色
'k' 黑色
'w' 白色

系列文章:

matplotlib 使用简明教程(一)-基础概念:
https://blog.csdn.net/fenghuizhidao/article/details/79352882
matplotlib 使用简明教程(二)-常用图表
https://blog.csdn.net/fenghuizhidao/article/details/83090043
matplotlib 使用简明教程(三)-一些专业图表简介
https://blog.csdn.net/fenghuizhidao/article/details/83090165
matplotlib 使用简明教程(四)-辅助性元件
https://blog.csdn.net/fenghuizhidao/article/details/83090249
matplotlib 使用简明教程(五)-画布、图表、元素基础操作
https://blog.csdn.net/fenghuizhidao/article/details/83090320
matplotlib 使用简明教程(六)-图像、动画相关
https://blog.csdn.net/fenghuizhidao/article/details/83090512
matplotlib 使用简明教程(七)-样式定义
https://blog.csdn.net/fenghuizhidao/article/details/83090553

你可能感兴趣的:(Python学习,Matplotlib)