前言
matplotlib 功能十分强大,就是工具栏丑了点。忍了一个学期之后,还是决定自己动手,魔改一波 matplotlib 的工具栏样式。同时给大家分享一下自己按照 MATLAB 写的 matplotlib 样式文件。注意:代码中 matplotlib 版本为 3.3.4,其他版本效果会不一样。
工具栏的美化
matplotlib 工具栏有三种模式:None 、toolbar2 和 toolmanager,默认 toolbar2, 我们可以通过 plt.rcParams['toolbar'] 来更换模式。在 None 模式下,图窗中不存在工具栏;toolbar2 和 toolmanager 长得很像,就是后者多了一个帮助按钮。先来看一下 matplotlib 工具栏本来的样子(后端使用的是Qt5Agg):

可以看到又黑又粗的图标搭配上灰色的背景,效果十分感人。上面也已经提到了,工具栏有三种模式,抛开 None 不看,如果我们想要修改工具栏的样式,就需要分别修改 toolbar2 和 toolmanager 这两种模式下的样式。从 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从源码中可以得知两种模式下的工具栏分别是 NavigationToolbar2QT 和 ToolbarQt 的实例。既然是用 QT 写的,那要美化工具栏岂不是易如反掌?代码撸起来 []( ̄▽ ̄)*。分别在 NavigationToolbar2QT 和 ToolbarQt 的构造函数的最后一行加上:
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 : Trueaxes.axisbelow : Trueaxes.facecolor : whiteaxes.prop_cycle : cycler('color', ['0072BD', 'D95319', 'EDB120', '7E2F8E', '77AC30', '4DBEEE', 'A2142F'])
## ***************************************************************************## * FIGURE *## ***************************************************************************figure.dpi : 140figure.figsize : 8, 6
## ***************************************************************************## * TICKS *## ***************************************************************************xtick.direction : inxtick.major.size : 6xtick.major.width : 0.5xtick.minor.size : 1.5xtick.minor.width : 0.5# xtick.minor.visible : Truextick.top : True
ytick.direction : inytick.major.size : 6ytick.major.width : 0.5ytick.minor.size : 1.5ytick.minor.width : 0.5# ytick.minor.visible : Trueytick.right : True
## ***************************************************************************## * LINES *## ***************************************************************************axes.linewidth : 0.5lines.linewidth : 1
## ***************************************************************************## * SAVING FIGURES *## ***************************************************************************savefig.dpi : 300savefig.bbox : tightsavefig.pad_inches : 0.05
## ***************************************************************************## * FONT *## ***************************************************************************font.size : 11font.family : seriffont.serif : Latin Modern Roman, Times New Romanfont.sans-serif : Helveticamathtext.default: itmathtext.fallback: cmmathtext.fontset : cm
## ***************************************************************************## * GRIDS *## ***************************************************************************grid.color : 0.8grid.alpha : 0.64grid.linewidth : 0.5grid.linestyle : -
## ***************************************************************************## * LEGEND *## ***************************************************************************legend.loc: bestlegend.shadow : Falselegend.frameon : Truelegend.fancybox : Falselegend.numpoints : 1legend.edgecolor: 0legend.framealpha : 1在绘图之前,调用一下 plt.style.use('matlab'),就可以将默认绘图风格换成上面定义的风格。
测试
import numpy as npimport 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.Figureplt.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()运行代码后效果如下:

可以看到图窗中的字体在数学模式下变成了好康的 Computer Modern,最重要的是工具栏也被成功美化。如果对工具栏不满意的话,可以自己改下 style sheet。以上*( •̀ ω •́ )✧
If this article helped you, please share it with others!
Some information may be outdated