likes
comments
collection
share

Matplotlib更改plot样式

作者站长头像
站长
· 阅读数 4

觉得Matplotlib.pyplot原本的样式不够好看,于是就研究了一下怎么更改其样式

plt是自带了很多样式的,输入以下代码查看自带样式:

import matplotlib.pyplot as plt

plt.style.available

输出结果如下:

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

这里就是全部自带的主题了

应用样式:

plt.style.use('seaborn-darkgrid')

再进行画图就可以看到效果了

Matplotlib更改plot样式

如果想要将该样式设为默认,就要修改matplotlibrc文件使其永久生效:

找到要应用的样式文件(以seaborn-darkgrid为例):

windows下可以用everything进行搜索,mac下可以进到python包目录下的matplotlib文件夹下搜索

搜索seaborn-darkgrid.mplstyle文件,如下:

Matplotlib更改plot样式

用文本编辑器打开内容如下:

# Seaborn common parameters
# .15 = dark_gray
# .8 = light_gray
figure.facecolor: white
text.color: .15
axes.labelcolor: .15
legend.frameon: False
legend.numpoints: 1
legend.scatterpoints: 1
xtick.direction: out
ytick.direction: out
xtick.color: .15
ytick.color: .15
axes.axisbelow: True
image.cmap: Greys
font.family: sans-serif
font.sans-serif: Arial, Liberation Sans, DejaVu Sans, Bitstream Vera Sans, sans-serif
grid.linestyle: -
lines.solid_capstyle: round

# Seaborn darkgrid parameters
axes.grid: True
axes.facecolor: EAEAF2
axes.edgecolor: white
axes.linewidth: 0
grid.color: white
xtick.major.size: 0
ytick.major.size: 0
xtick.minor.size: 0
ytick.minor.size: 0

接下来找到matplotlibrc文件:

  • Mac:直接打开终端输入vim ~/.matplotlib/matplotlibrc
  • Win:在C:/Users找到用户目录进入后把显示隐藏文件打开,进入到.matplotlib文件夹即可找到该文件(如果没有就新建一个)

seaborn-darkgrid.mplstyle文件中的内容复制到matplotlibrc文件中即可。

重启JupyterLab即可看到效果。