【python】plt的figure最大化

matlab转python,很happy,但是很多等价的功能还在摸索。比如figure最大化

主要代码

网上实现plt.figure()最大化,主要以下几种命令

  • 能放大,但图像是歪的,不是最大化
plt.get_current_fig_manager().window.state('zoomed')
  • 能最大化,是我要的效果:Qt5Agg/QtAgg
plt.get_current_fig_manager().window.showMaximized()
  • 能放大,但图像是歪的,不是最大化
manager = plt.get_current_fig_manager()
# matplotlib3.2.1//2.2.3 work
manager.resize(*manager.window.maxsize())
  • 报错:没有frame属性
plt.get_current_fig_manager().frame.Maximize(True)
  • 全屏,不是最大化,都不知道咋退出很尴尬
plt.get_current_fig_manager().full_screen_toggle()

参考链接:

https://blog.csdn.net/weixin_39585934/article/details/93774445
https://blog.csdn.net/hehedadaq/article/details/115459790

查看后端

不同后端(backend)对应的window的属性不同,所以有的代码能执行,有的代码则会报错,比如:

AttributeError: '\_tkinter.tkapp' object has no attribute 'showMaximized'

可以help查看函数是否有对应接口

  • 查看后端:plt.get_backend()

切换后端

  • 切换backend:调用 pyplot 之前切换后端
    • 报错:ImportError: Failed to import any of the following Qt binding modules: PyQt5, PySide2:解决:
      • conda install conda-forge::pyside2 anaconda::pyqt仍报错,尬住:但是呢,我import PyQt5, PySide2又没问题
      • 先装matplotlib再装pyqt依赖的,把matplotlib卸载(conda remove)重装(conda install)才可识别,虽然matplotlib版本没变。尴尬的是,matplotlib.use('Qt5Agg')不报错了,后面还是报错

配置Qt后端

其实就是重装,conda想装啥装啥,我摆了

  • backend配置:环境重装
conda create -n bin numpy scipy matplotlib tqdm
conda activate bin
python -m pip install --upgrade pip # update pip
pip install opencv-python

测试:重装以后最大化也可以实现plt.tight_layout()了,emmm

import matplotlib 
matplotlib.use('QtAgg')
import matplotlib.pyplot as plt
plt.figure()
plt.plot([1,2], [3,4])
plt.get_current_fig_manager().window.showMaximized()
plt.tight_layout()
plt.show()

其他

  • 先最大化,再加其他的title、label
  • 最大化无法plt.tight_layout() 重装解决
  • plt.savefig无法保存全屏状态的figure,尝试过bbox_inches='tight', dpi=300均无效:已放弃,查看归查看,保存归保存。难道需要设置figure尺寸才能实现效果?
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值