matplotlib中plot.show()不显示图片的问题:如何把backend=Agg配置为TkAgg

关于matplotlib不显示的问题,碰到过多次,貌似是默认安装使用anaconda时都会碰到,不知道matplotlib为什么一直不解决这个问题。所以记录一下。

默认情况下,matplotlib的backend使用的是agg,或template,此时是无法显示图片的,agg库不支持。

好奇的可以查一下自己的配置文件,如

>>> import matplotlib
>>> matplotlib.matplotlib_fname()
C:\Users\Administrator\.matplotlib\matplotlibrc
也可以使用下面的命令打印出配置,
print(mpl.get_backend())
# it will display Agg if you have a display problem, or otherwise TkAgg etc.
print(mpl.matplotlib_fname())
# it will display the setting file location, e.g.
# C:\Users\Administrator\.matplotlib\matplotlibrc
# the content is simple: "backend      : Agg", just change it to "backend      :TkAgg"

 

解决办法:

先把自己版本所支持的backends打印出来看一下,

>>>import matplotlib.rcsetup as rcsetup
>>>print(rcsetup.all_backends)
['GTK3Agg', 'GTK3Cairo', 'MacOSX', 'nbAgg', 'Qt4Agg', 'Qt4Cairo', 'Qt5Agg', 'Qt5Cairo', 'TkAgg', 'TkCairo', 'WebAgg', 'WX', 'WXAgg', 'WXCairo', 'agg', 'cairo', 'pdf', 'pgf', 'ps', 'svg', 'template']

例如,把配置文件

C:\Users\Administrator.matplotlib\matplotlibrc

的内容改成正面的情况:

backend      : TkAgg

一般图片就能正常显示了,当然你也可以不停尝试下其他的backends,像有些backends是需要安装 其他支持包的,如cairo。

linux上解决办法也是相同的,只不过配置文件的位置不一样。

 

给几个有用的参考:

https://stackoverflow.com/questions/2130913/no-plot-window-in-matplotlib

https://stackoverflow.com/questions/7534453/matplotlib-does-not-show-my-drawings-although-i-call-pyplot-show

 

附评论里的问题:确定配置为TkAgg,但是在import matplotlib.pyplot as plt之后所显示的终端为agg

这种现象的主要原因,是因为版本冲突。可以这么说,print((matplotlib.matplotlib_fname())打印出来的配置文件,未必是你正在调用的配置文件。

我自己特意在不同环境调试了一下,用一个实际例子来说,在某个非默认的环境,名称为tch37,打印出来的地址为
D:\Anaconda3\envs\tch37\Lib\site-packages\matplotlib\mpl-data\matplotlibrc
通常在窗口命令下用,conda activate tch37之后再用python,是这个环境。

而实际上,我查了一下自己电脑中的版本,
print(matplotlib.__version__)
'3.2.2'
然后我找到这个3.2.2所对应的位置,在这里,
D:\Anaconda3\pkgs\matplotlib-base-3.2.2-py37h64f37c6_0\Lib\site-packages\matplotlib\mpl-data\matplotlibrc
在vscode中,即使我调用tch37这个环境,用的也是3.2.2这个配置。
所以,如果我要在vscode中使用TkAgg,我应该修改的是这个地方的配置。

总结起来就是,出现这种情况,你先找一下,系统里到底有多少个matplotlibrc这样的文件,然后再慢慢查找到底应该修改哪个配置文件。通常默认的配置文件里有这么一句

## You can also deploy your own backend outside of matplotlib by referring to
## the module name (which must be in the PYTHONPATH) as 'module://my_backend'.
#backend : Agg

你把他改成下面这个样子即可,

## You can also deploy your own backend outside of matplotlib by referring to
## the module name (which must be in the PYTHONPATH) as 'module://my_backend'.
backend : TkAgg

至于anaconda何时,在哪里调用哪个地方的配置,这一点估计谁也扯不清楚。

 

临时暴力改变backend

不得不承认,matplotlib(anaconda?)有时无论你怎么配置,就是存在不可思议的一些怪现象。

如果你临时需要转换backend,其实很简单,直接调用
matplotlib.use('Agg')

matplotlib.use('TkAgg')
暴力完成切换即可。

 

官方的说法摘录

参考:https://matplotlib.org/faq/usage_faq.html

There are four ways to configure your backend. If they conflict each other, the method mentioned last in the following list will be used, e.g. calling use() will override the setting in your matplotlibrc.

  1. The backend parameter in your matplotlibrc file (see Customizing matplotlib):

    backend : WXAgg   # use wxpython with antigrain (agg) rendering
    
  2. Setting the MPLBACKEND environment variable, either for your current shell or for a single script:

    > export MPLBACKEND="module://my_backend"
    > python simple_plot.py
    
    > MPLBACKEND="module://my_backend" python simple_plot.py
    

    Setting this environment variable will override the backend parameter in any matplotlibrc, even if there is a matplotlibrc in your current working directory. Therefore setting MPLBACKEND globally, e.g. in your .bashrc or .profile, is discouraged as it might lead to counter-intuitive behavior.

  3. To set the backend for a single script, you can alternatively use the -d command line argument:

    > python script.py -dbackend
    

    This method is deprecated as the -d argument might conflict with scripts which parse command line arguments (see issue #1986). You should use MPLBACKEND instead.

  4. If your script depends on a specific backend you can use the use() function:

    import matplotlib
    matplotlib.use('PS')   # generate postscript output by default
    

    If you use the use() function, this must be done before importing matplotlib.pyplot. Calling use() after pyplot has been imported will have no effect. Using use() will require changes in your code if users want to use a different backend. Therefore, you should avoid explicitly calling use() unless absolutely necessary.

 

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值