关键词:style,rcParams,rc
全局参数的设置——
###使用style包:
style包能让你很轻松的切花绘图的类型,预先在matplotlib中定义好了许多style类型供选择,使用的方式如下
|
|
获得所有提供的styles的方法如下:
|
|
输出的所有类型如下(使用的时候可以如下选择):
|
|
定义属于自己的属性
你可以定义属于自己风格的style,具体的定义方式请查看源文档。
复合风格
|
|
|
|
类型 | 介绍 |
---|---|
str | The name of a style or a path/URL to a style file. For a list of available style names, see style.available . |
dict | Dictionary with valid key/value pairs formatplotlib.rcParams . |
list | A list of style specifiers (str or dict) applied from first to last in the list. |
暂时风格
不做介绍
rcParams模块(十分常用)
动态rc设定
我们可以动态的改变已经在style中预设好的rc参数,所有的rc设置都被存储在一个类似字典的变量中——matplotlib.rcParams,这是一个全局包,工作方式有如下几种:
|
|
|
|
|
|
NOTICE:
|
|
所有Group类型包含的属性见英文原文最后的matplotlibrc文档的示例
以下为原文:
Customizing matplotlib
Using style sheets
The style
package adds support for easy-to-switch plotting “styles” with the same parameters as a matplotlibrc file (which is read at startup to configure matplotlib).
There are a number of pre-defined styles provided by matplotlib. For example, there’s a pre-defined style called “ggplot”, which emulates the aesthetics of ggplot (a popular plotting package for R). To use this style, just add:
|
|
To list all available styles, use:
|
|
Defining your own style
You can create custom styles and use them by calling style.use
with the path or URL to the style sheet. Additionally, if you add your <style-name>.mplstyle
file to mpl_configdir/stylelib
, you can reuse your custom style sheet with a call to style.use(<style-name>)
. By default mpl_configdir
should be ~/.config/matplotlib
, but you can check where yours is with matplotlib.get_configdir()
; you may need to create this directory. You also can change the directory where matplotlib looks for the stylelib/ folder by setting the MPLCONFIGDIR environment variable, see matplotlib configuration and cache directory locations.
Note that a custom style sheet in mpl_configdir/stylelib
will override a style sheet defined by matplotlib if the styles have the same name.
For example, you might want to create mpl_configdir/stylelib/presentation.mplstyle
with the following:
|
|
Then, when you want to adapt a plot designed for a paper to one that looks good in a presentation, you can just add:
|
|
Composing styles
Style sheets are designed to be composed together. So you can have a style sheet that customizes colors and a separate style sheet that alters element sizes for presentations. These styles can easily be combined by passing a list of styles:
|
|
ote that styles further to the right will overwrite values that are already defined by styles on the left.
Temporary styling
If you only want to use a style for a specific block of code but don’t want to change the global styling, the style package provides a context manager for limiting your changes to a specific scope. To isolate your styling changes, you can write something like the following:
|
|
matplotlib rcParams
Dynamic rc settings
You can also dynamically change the default rc settings in a python script or interactively from the python shell. All of the rc settings are stored in a dictionary-like variable called matplotlib.rcParams
, which is global to the matplotlib package. rcParams can be modified directly, for example:
|
|
Matplotlib also provides a couple of convenience functions for modifying rc settings. The matplotlib.rc()
command can be used to modify multiple settings in a single group at once, using keyword arguments:
|
|
The matplotlib.rcdefaults()
command will restore the standard matplotlib default settings.
There is some degree of validation when setting the values of rcParams, see matplotlib.rcsetup
for details.
The matplotlibrc
file
matplotlib uses matplotlibrc
configuration files to customize all kinds of properties, which we call rc settings
or rc parameters
. You can control the defaults of almost every property in matplotlib: figure size and dpi, line width, color and style, axes, axis and grid properties, text and font properties and so on. matplotlib looks for matplotlibrc
in four locations, in the following order:
matplotlibrc
in the current working directory, usually used for specific customizations that you do not want to apply elsewhere.$MATPLOTLIBRC/matplotlibrc
.It next looks in a user-specific place, depending on your platform:
- On Linux and FreeBSD, it looks in
.config/matplotlib/matplotlibrc
(or$XDG_CONFIG_HOME/matplotlib/matplotlibrc
) if you’ve customized your environment. - On other platforms, it looks in
.matplotlib/matplotlibrc
.
- On Linux and FreeBSD, it looks in
*INSTALL*/matplotlib/mpl-data/matplotlibrc
, where*INSTALL*
is something like/usr/lib/python3.5/site-packages
on Linux, and maybeC:\Python35\Lib\site-packages
on Windows. Every time you install matplotlib, this file will be overwritten, so if you want your customizations to be saved, please move this file to your user-specific matplotlib directory.
To display where the currently active matplotlibrc
file was loaded from, one can do the following:
|
|
See below for a sample matplotlibrc file.
A sample matplotlibrc file
|
|