zl程序教程

您现在的位置是:首页 >  .Net

当前栏目

如何美化 Matplotlib 的工具栏和绘图风格

2023-02-18 16:32:01 时间

前言

matplotlib 功能十分强大,就是工具栏丑了点。忍了一个学期之后,还是决定自己动手,魔改一波 matplotlib 的工具栏样式。同时给大家分享一下自己按照 MATLAB 写的 matplotlib 样式文件。注意:代码中 matplotlib 版本为 3.3.4,其他版本效果会不一样

工具栏的美化

matplotlib 工具栏有三种模式:Nonetoolbar2toolmanager,默认 toolbar2, 我们可以通过 plt.rcParams['toolbar'] 来更换模式。在 None 模式下,图窗中不存在工具栏;toolbar2toolmanager 长得很像,就是后者多了一个帮助按钮。先来看一下 matplotlib 工具栏本来的样子(后端使用的是Qt5Agg):

可以看到又黑又粗的图标搭配上灰色的背景,效果十分感人。上面也已经提到了,工具栏有三种模式,抛开 None 不看,如果我们想要修改工具栏的样式,就需要分别修改 toolbar2toolmanager 这两种模式下的样式。从 matplotlib.backends.backend_qt5 的第600行开始有:

def _get_toolbar(self, canvas, parent):
    # must be inited after the window, drawingArea and figure
    # attrs are set
    if matplotlib.rcParams['toolbar'] == 'toolbar2':
        toolbar = NavigationToolbar2QT(canvas, parent, True)
    elif matplotlib.rcParams['toolbar'] == 'toolmanager':
        toolbar = ToolbarQt(self.toolmanager, self.window)
    else:
        toolbar = None
    return toolbar

从源码中可以得知两种模式下的工具栏分别是 NavigationToolbar2QTToolbarQt 的实例。既然是用 QT 写的,那要美化工具栏岂不是易如反掌?代码撸起来 []~( ̄▽ ̄)~*。分别在 NavigationToolbar2QTToolbarQt 的构造函数的最后一行加上:

self.setStyleSheet("""QToolBar{background:white}
                      QLabel{font:11pt 'Consolas'}""")

QToolBar{background:white} 设置工具栏的背景色为白色,QLabel{font:11pt 'Consolas'} 设置的工具栏的字体为 11pt 的 Consolas 。修改完层叠样式之后,我们来换一波图标,图标、字体以及mplstyle文件放在了百度网盘(提取码:nlnv),用这些图标替换掉 Lib\site-packages\matplotlib\mpl-data\images 中的自带图标,工具栏的美化就大功告成了( ̄︶ ̄)↗ 。

绘图样式的美化

matplotlib 可以自定义绘图风格,这是它强大之处的体现。打开C盘用户文件夹的.matplotlib\stylelib(没有的话可以新建一个),我们可以看到一些 .mplstyle 文件,这些文件就是某种绘图风格的具体配置。我们在这个目录下创建一个matlab.mplstye 文件,往里面写入:

## ***************************************************************************
## * AXES                                                                    *
## ***************************************************************************
axes.grid :       True
axes.axisbelow :  True
axes.facecolor :  white
axes.prop_cycle : cycler('color', ['0072BD', 'D95319', 'EDB120', '7E2F8E', '77AC30', '4DBEEE', 'A2142F'])



## ***************************************************************************
## * FIGURE                                                                  *
## ***************************************************************************
figure.dpi :        140
figure.figsize :    8, 6




## ***************************************************************************
## * TICKS                                                                   *
## ***************************************************************************
xtick.direction :       in
xtick.major.size :      6
xtick.major.width :     0.5
xtick.minor.size :      1.5
xtick.minor.width :     0.5
# xtick.minor.visible :   True
xtick.top : True

ytick.direction :       in
ytick.major.size :      6
ytick.major.width :     0.5
ytick.minor.size :      1.5
ytick.minor.width :     0.5
# ytick.minor.visible :   True
ytick.right :           True



## ***************************************************************************
## * LINES                                                                   *
## ***************************************************************************
axes.linewidth :  0.5
lines.linewidth : 1



## ***************************************************************************
## * SAVING FIGURES                                                          *
## ***************************************************************************
savefig.dpi : 	     300
savefig.bbox :       tight
savefig.pad_inches : 0.05



## ***************************************************************************
## * FONT                                                                    *
## ***************************************************************************
font.size :        11
font.family :      serif
font.serif :       Latin Modern Roman, Times New Roman
font.sans-serif :  Helvetica
mathtext.default:  it
mathtext.fallback: cm
mathtext.fontset : cm



## ***************************************************************************
## * GRIDS                                                                   *
## ***************************************************************************
grid.color :     0.8
grid.alpha :     0.64
grid.linewidth : 0.5
grid.linestyle : -



## ***************************************************************************
## * LEGEND                                                                  *
## ***************************************************************************
legend.loc:         best
legend.shadow :     False
legend.frameon :    True
legend.fancybox :   False
legend.numpoints :  1
legend.edgecolor:   0
legend.framealpha : 1

在绘图之前,调用一下 plt.style.use('matlab'),就可以将默认绘图风格换成上面定义的风格。

测试

import numpy as np
import matplotlib.pyplot as plt


x = np.arange(0, 2 * np.pi, 0.01)
y = np.sin(x)
z = 0.5 * np.sin(x)

# 更换工具栏模式
# plt.rcParams['toolbar'] = 'toolmanager'
# 设置绘图样式
plt.style.use('matlab')

fig = plt.figure(num='sine wave')  #type:plt.Figure
plt.plot(x, y, x, z)
plt.axis((0, 2 * np.pi, -1, 1))

plt.xlabel(r'$x$')
plt.ylabel(r'$y$')
plt.title(r'$Sine\ Wave$')
plt.legend([r'${\rm sin}(x)$', r'$0.5\cdot {\rm sin}(x)$'])
plt.show()

运行代码后效果如下:

美化后的matplotlib

可以看到图窗中的字体在数学模式下变成了好康的 Computer Modern,最重要的是工具栏也被成功美化。如果对工具栏不满意的话,可以自己改下 style sheet。以上*( •̀ ω •́ )✧