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

2024-03-15 22:58

本文主要是介绍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.

 

这篇关于matplotlib中plot.show()不显示图片的问题:如何把backend=Agg配置为TkAgg的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/813531

相关文章

linux生产者,消费者问题

pthread_cond_wait() :用于阻塞当前线程,等待别的线程使用pthread_cond_signal()或pthread_cond_broadcast来唤醒它。 pthread_cond_wait() 必须与pthread_mutex 配套使用。pthread_cond_wait()函数一进入wait状态就会自动release mutex。当其他线程通过pthread

问题:第一次世界大战的起止时间是 #其他#学习方法#微信

问题:第一次世界大战的起止时间是 A.1913 ~1918 年 B.1913 ~1918 年 C.1914 ~1918 年 D.1914 ~1919 年 参考答案如图所示

Linux 安装、配置Tomcat 的HTTPS

Linux 安装 、配置Tomcat的HTTPS 安装Tomcat 这里选择的是 tomcat 10.X ,需要Java 11及更高版本 Binary Distributions ->Core->选择 tar.gz包 下载、上传到内网服务器 /opt 目录tar -xzf 解压将解压的根目录改名为 tomat-10 并移动到 /opt 下, 形成个人习惯的路径 /opt/tomcat-10

uniapp接入微信小程序原生代码配置方案(优化版)

uniapp项目需要把微信小程序原生语法的功能代码嵌套过来,无需把原生代码转换为uniapp,可以配置拷贝的方式集成过来 1、拷贝代码包到src目录 2、vue.config.js中配置原生代码包直接拷贝到编译目录中 3、pages.json中配置分包目录,原生入口组件的路径 4、manifest.json中配置分包,使用原生组件 5、需要把原生代码包里的页面修改成组件的方

2024.6.24 IDEA中文乱码问题(服务器 控制台 TOMcat)实测已解决

1.问题产生原因: 1.文件编码不一致:如果文件的编码方式与IDEA设置的编码方式不一致,就会产生乱码。确保文件和IDEA使用相同的编码,通常是UTF-8。2.IDEA设置问题:检查IDEA的全局编码设置和项目编码设置是否正确。3.终端或控制台编码问题:如果你在终端或控制台看到乱码,可能是终端的编码设置问题。确保终端使用的是支持你的文件的编码方式。 2.解决方案: 1.File -> S

JAVA读取MongoDB中的二进制图片并显示在页面上

1:Jsp页面: <td><img src="${ctx}/mongoImg/show"></td> 2:xml配置: <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001

vcpkg安装opencv中的特殊问题记录(无法找到opencv_corexd.dll)

我是按照网上的vcpkg安装opencv方法进行的(比如这篇:从0开始在visual studio上安装opencv(超详细,针对小白)),但是中间出现了一些别人没有遇到的问题,虽然原因没有找到,但是本人给出一些暂时的解决办法: 问题1: 我在安装库命令行使用的是 .\vcpkg.exe install opencv 我的电脑是x64,vcpkg在这条命令后默认下载的也是opencv2:x6

问题-windows-VPN不正确关闭导致网页打不开

为什么会发生这类事情呢? 主要原因是关机之前vpn没有关掉导致的。 至于为什么没关掉vpn会导致网页打不开,我猜测是因为vpn建立的链接没被更改。 正确关掉vpn的时候,会把ip链接断掉,如果你不正确关掉,ip链接没有断掉,此时你vpn又是没启动的,没有域名解析,所以就打不开网站。 你可以在打不开网页的时候,把vpn打开,你会发现网络又可以登录了。 方法一 注意:方法一虽然方便,但是可能会有

IDEA配置Tomcat远程调试

因为不想把本地的Tomcat配置改乱或者多人开发项目想测试,本文主要是记录一下,IDEA使用Tomcat远程调试的配置过程,免得一段时间不去配置到时候忘记(毕竟这次是因为忘了,所以才打算记录的…) 首先在catalina.sh添加以下内容 JAVA_OPTS="-Dcom.sun.management.jmxremote=-Dcom.sun.management.jmxremote.port

据阿谱尔APO Research调研显示,2023年全球髓内钉市场销售额约为4.7亿美元

根据阿谱尔 (APO Research)的统计及预测,2023年全球髓内钉市场销售额约为4.7亿美元,预计在2024-2030年预测期内将以超过3.82%的CAGR(年复合增长率)增长。 髓内钉市场是指涉及髓内钉制造、分销和销售的行业。髓内钉是一种用于整形外科手术的医疗器械,用于稳定长骨骨折,特别是股骨、胫骨和肱骨。髓内钉通常由不銹钢或钛等材料制成,并插入骨的髓管中,以在愈合过程中提供结构支