matplotlib更改窗口图标的方法示例


Posted in Python onFebruary 03, 2021

matplotlib窗口图标默认是matplotlib的标志,如果想修改怎么改呢?

由于我选择的matplotlib后端是PyQT5,直接查看matplotlib.backends.backend_qt5模块源码。

原理

查看源码可知,窗口图标功能定义在FigureManagerQT类中,设置的默认图标是mpl-data\images\matplotlib.svg。
FigureManagerQT的父类是FigureManagerBase,其功能是作为容器隔离matplotlib图像和后端实现的窗口,并与窗口进行交互,它会自动适配matplotlib选用的后端。
这样只用找到当前图像中FigureManagerQT类的实例(即当前图像的图像管理器)后调用setWindowIcon方法即可完成窗口图标的更改。
获取当前图像的图像管理器有两种写法,因此,更改窗口图标的实现有两种。
根据matplotlib.pyplot.get_current_fig_manager()函数源码可知这两种方法是等价的。

实现代码

import matplotlib.pyplot as plt
from PyQt5 import QtGui

plt.plot([1,2])
# 构建图标
PATH_TO_ICON = r"c:\quit.png"
new_icon = QtGui.QIcon(PATH_TO_ICON)
# 方法一:使用figure.canvas.manager获取当前图像的`FigureManagerQT`类实例
fig =plt.gcf()
fig.canvas.manager.window.setWindowIcon(QtGui.QIcon(new_icon))

# 方法二:使用plt.get_current_fig_manager()获取当前图像的`FigureManagerQT`类实例
plt.get_current_fig_manager().window.setWindowIcon(new_icon)
plt.show()

matplotlib源码

class FigureManagerQT(FigureManagerBase):
  """
  Attributes
  ----------
  canvas : `FigureCanvas`
    The FigureCanvas instance
  num : int or str
    The Figure number
  toolbar : qt.QToolBar
    The qt.QToolBar
  window : qt.QMainWindow
    The qt.QMainWindow
  """

  def __init__(self, canvas, num):
    FigureManagerBase.__init__(self, canvas, num)
    self.window = MainWindow()
    self.window.closing.connect(canvas.close_event)
    self.window.closing.connect(self._widgetclosed)

    self.window.setWindowTitle("Figure %d" % num)
    image = str(cbook._get_data_path('images/matplotlib.svg'))
    self.window.setWindowIcon(QtGui.QIcon(image))
def get_current_fig_manager():
  return gcf().canvas.manager

到此这篇关于matplotlib更改窗口图标的方法示例的文章就介绍到这了,更多相关matplotlib更改窗口图标内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
整理Python 常用string函数(收藏)
May 30 Python
tensorflow识别自己手写数字
Mar 14 Python
python输出100以内的质数与合数实例代码
Jul 08 Python
Python定义一个跨越多行的字符串的多种方法小结
Jul 19 Python
详解python 3.6 安装json 模块(simplejson)
Apr 02 Python
PyCharm搭建Spark开发环境的实现步骤
Sep 05 Python
python tkinter图形界面代码统计工具(更新)
Sep 18 Python
利用Python自动化操作AutoCAD的实现
Apr 01 Python
Python使用Selenium实现淘宝抢单的流程分析
Jun 23 Python
keras的ImageDataGenerator和flow()的用法说明
Jul 03 Python
Python激活Anaconda环境变量的详细步骤
Jun 08 Python
PyTorch device与cuda.device用法
Apr 03 Python
python中添加模块导入路径的方法
Feb 03 #Python
浅谈matplotlib默认字体设置探索
Feb 03 #Python
python sleep和wait对比总结
Feb 03 #Python
Python实现简单猜数字游戏
Feb 03 #Python
python 实现图片裁剪小工具
Feb 02 #Python
python向xls写入数据(包括合并,边框,对齐,列宽)
Feb 02 #Python
Python datetime模块的使用示例
Feb 02 #Python
You might like
多文件上传的例子
2006/10/09 PHP
php 变量定义方法
2009/06/14 PHP
使用php 获取时间今天明天昨天时间戳的详解
2013/06/20 PHP
PHP+memcache实现消息队列案例分享
2014/05/21 PHP
php表单习惯用的正则表达式
2017/10/11 PHP
PHP crypt()函数的用法讲解
2019/02/15 PHP
php连接mysql数据库最简单的实现方法
2019/09/24 PHP
javascript delete 使用示例代码
2010/03/29 Javascript
常见JS效果之图片减速度滚动实现代码
2011/12/08 Javascript
有关于eclipse配置spket需要注意的一些地方
2013/04/07 Javascript
详解JavaScript中|单竖杠运算符的使用方法
2016/05/23 Javascript
BootStrap table表格插件自适应固定表头(超好用)
2016/08/24 Javascript
AngularJs  Creating Services详解及示例代码
2016/09/02 Javascript
解决easyui日期时间框ie的兼容的问题
2018/03/01 Javascript
最后说说Vue2 SSR 的 Cookies 问题
2018/05/25 Javascript
[25:59]Newbee vs TNC 2018国际邀请赛小组赛BO2 第二场 8.16
2018/08/17 DOTA
python中元类用法实例
2014/10/10 Python
python 实现自动远程登陆scp文件实例代码
2017/03/13 Python
python3.5+tesseract+adb实现西瓜视频或头脑王者辅助答题
2018/01/17 Python
深入浅析python with语句简介
2018/04/11 Python
Python 存储字符串时节省空间的方法
2019/04/23 Python
win10环境下配置vscode python开发环境的教程详解
2019/10/16 Python
python3中sys.argv的实例用法
2020/04/24 Python
关于python的缩进规则的知识点详解
2020/06/22 Python
python基于openpyxl生成excel文件
2020/12/23 Python
css3实现书本翻页效果的示例代码
2021/03/08 HTML / CSS
尤为Wconcept中国官网:韩国设计师品牌服饰
2019/01/10 全球购物
银河香水:Galaxy Perfume
2019/03/25 全球购物
倩碧澳大利亚官网:Clinique澳大利亚
2019/07/22 全球购物
教你打造完美的创业计划书
2014/01/06 职场文书
C++程序员求职信范文
2014/04/14 职场文书
机票销售员态度不好检讨书
2014/09/27 职场文书
副总经理岗位职责
2015/02/02 职场文书
保护环境的宣传语
2015/07/13 职场文书
2016秋季校长开学典礼致辞
2015/11/26 职场文书
《自己去吧》教学反思
2016/02/16 职场文书