matplotlib运行时配置(Runtime Configuration,rc)参数rcParams解析
作者:mighty13 时间:2022-08-31 12:17:15
什么是运行时配置(Runtime Configuration,rc)
Matplotlib使用matplotlibrc配置文件来自定义图形的各种属性,称之为rc配置或rc参数(rcParams)。通过rc参数可以修改matplotlib绝大多数属性的默认值,包括窗体大小、每英寸的点数、线条宽度、颜色、样式、坐标轴、坐标和网络属性、文本、字体等。
运行时配置的默认值
运行时配置的默认值存放在默认的matplotlibrc文件中。
matplotlibrc文件与rcParams的关系
rcParams是RcParams类的实例,结构类似于字典,用于处理matplotlib的默认运行时配置,它是matplotlib模块的全局变量。当导入matplotlib模块时,matplotlibrc文件中的所有rc 参数存储在matplotlib.rcParams中。
源码如下:
rcParamsDefault = _rc_params_in_file(
cbook._get_data_path("matplotlibrc"),
# Strip leading comment.
transform=lambda line: line[1:] if line.startswith("#") else line,
fail_on_error=True)
dict.update(rcParamsDefault, rcsetup._hardcoded_defaults)
rcParams = RcParams() # The global instance.
dict.update(rcParams, dict.items(rcParamsDefault))
dict.update(rcParams, _rc_params_in_file(matplotlib_fname()))
修改运行时配置参数的方法
通过rc文件:修改默认 matplotlibrc文件或者指定自定义rc文件。
修改默认 matplotlibrc文件。
指定自定义的rc文件。
matplotlib.rc_file(fname, *, use_default_template=True)
通过rcParams对象:直接修改rcParams对象。这种方法比较灵活,修改的方法有以下三种:
matplotlib.rc(group, **kwargs)
rcParams[group.params]
rcParams.update()
第一、二种方法是等价的,第三种方法不支持缩写和分组。
#第一种方法1
rc('lines', linewidth=2, color='r')
#第一种方法2
font = {'linewidth' : 2,
'color' : 'r'}
rc('lines', **font)
#第二种方法
rcParams['lines.linewidth'] = 2
rcParams['lines.color'] = 'r'
#第三种方法
rcParams.update({"lines.linewidth": 2,'lines.color': 'r'})
还原修改运行时配置默认值的方法
matplotlib.rcdefaults():根据matplotlib内置的默认样式还原rcParams对象。
matplotlib.rc_file_defaults():根据matplotlib加载的rc文件的源文件还原rcParams对象。
matplotlib.style.use(default):加载默认样式。
来源:https://blog.csdn.net/mighty13/article/details/112018769
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python对接六大主流数据库(只需三步)
![](https://img.aspxhome.com/file/2023/4/64674_0s.jpg)
几个常用的js小函数
python面向对象版学生信息管理系统
![](https://img.aspxhome.com/file/2023/8/87638_0s.jpg)
ASP.NET对路径"xxxxx"的访问被拒绝的解决方法小结
网站设计应当让用户选,别让用户想
python实现爬虫统计学校BBS男女比例(一)
![](https://img.aspxhome.com/file/2023/6/76806_0s.jpg)
Python return语句如何实现结果返回调用
pyautogui自动化控制鼠标和键盘操作的步骤
![](https://img.aspxhome.com/file/2023/2/101182_0s.png)
Pytorch使用shuffle打乱数据的操作
Python中jieba库的使用方法
![](https://img.aspxhome.com/file/2023/1/77821_0s.png)
用Python抢过年的火车票附源码
![](https://img.aspxhome.com/file/2023/2/93572_0s.gif)
python中列表添加的四种方法小结
![](https://img.aspxhome.com/file/2023/7/71337_0s.png)
Python创建一个空的dataframe,并循环赋值的方法
Pycharm运行加载文本出现错误的解决方法
![](https://img.aspxhome.com/file/2023/8/97548_0s.jpg)
Python高光谱遥感影像处理问题详细分析讲解
Python编程实现简易的音乐播放器基本操作
![](https://img.aspxhome.com/file/2023/5/80685_0s.jpg)
配置高可用性的MySQL服务器负载均衡群集
Python Numpy教程之排序,搜索和计数详解
由浅入深讲解python中的yield与generator
python学习实操案例(五)
![](https://img.aspxhome.com/file/2023/0/92220_0s.png)