Mac系统下Python绘图窗口总在最后面


关注九天学者微信公众号(扫码关注)第一时间获取技术贴更新!


在Mac系统下使用python的同学肯定遇到过这个问题:用matplotlib绘图的时候,窗口总是在最后面。比如我用的vscode编辑代码的,成图窗口总是在vscode后面,需要将vscode窗口缩小一点才能看到绘图结果。这显然不是我们想要的!


解决办法

import matplotlib as mpl
mpl.use('TkAgg')
import matplotlib.pyplot as plt

import matplotlib as mpl之后进阶着跟一句mpl.use('TkAgg')即可将绘图窗口调到最前面。但是这句代码必须紧跟mpl且不能放在plt之后,比如:

import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.use('TkAgg')

将会提示错误,而且窗口依然在最后面:

This call to matplotlib.use() has no effect because the backend has already
been chosen; matplotlib.use() must be called before pylab, matplotlib.pyplot,
or matplotlib.backends is imported for the first time.
The backend was originally set to 'MacOSX' by the following code:
File "test_MonokaiPro.py", line 3, in
import matplotlib.pyplot as plt
File "/Users/zguo/.pyenv/versions/anaconda3-5.0.1/lib/python3.6/site-packages/matplotlib/pyplot.py", line 69,in
from matplotlib.backends import pylab_setup
File "/Users/zguo/.pyenv/versions/anaconda3-5.0.1/lib/python3.6/site-packages/matplotlib/backends/init.py", line 14, in
line for line in traceback.format_stack()

如何快速运行脚本

Mac系统下Python绘图窗口总在最后面_第1张图片
code runner

安装runner插件,然后设置你喜欢的快捷键,比如我喜欢用shift+enter,聪明的你一下就想到了,没错,这个就是jupyter-notebook的默认运行快捷键!

Mac系统下Python绘图窗口总在最后面_第2张图片
设置快捷键:双击一下然后键入你喜欢的快捷键

关注九天学者微信公众号(扫码关注)第一时间获取技术贴更新!


你可能感兴趣的:(Mac系统下Python绘图窗口总在最后面)