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

相关文章

mybatis和mybatis-plus设置值为null不起作用问题及解决

《mybatis和mybatis-plus设置值为null不起作用问题及解决》Mybatis-Plus的FieldStrategy主要用于控制新增、更新和查询时对空值的处理策略,通过配置不同的策略类型... 目录MyBATis-plusFieldStrategy作用FieldStrategy类型每种策略的作

linux下多个硬盘划分到同一挂载点问题

《linux下多个硬盘划分到同一挂载点问题》在Linux系统中,将多个硬盘划分到同一挂载点需要通过逻辑卷管理(LVM)来实现,首先,需要将物理存储设备(如硬盘分区)创建为物理卷,然后,将这些物理卷组成... 目录linux下多个硬盘划分到同一挂载点需要明确的几个概念硬盘插上默认的是非lvm总结Linux下多

Python Jupyter Notebook导包报错问题及解决

《PythonJupyterNotebook导包报错问题及解决》在conda环境中安装包后,JupyterNotebook导入时出现ImportError,可能是由于包版本不对应或版本太高,解决方... 目录问题解决方法重新安装Jupyter NoteBook 更改Kernel总结问题在conda上安装了

pip install jupyterlab失败的原因问题及探索

《pipinstalljupyterlab失败的原因问题及探索》在学习Yolo模型时,尝试安装JupyterLab但遇到错误,错误提示缺少Rust和Cargo编译环境,因为pywinpty包需要它... 目录背景问题解决方案总结背景最近在学习Yolo模型,然后其中要下载jupyter(有点LSVmu像一个

解决jupyterLab打开后出现Config option `template_path`not recognized by `ExporterCollapsibleHeadings`问题

《解决jupyterLab打开后出现Configoption`template_path`notrecognizedby`ExporterCollapsibleHeadings`问题》在Ju... 目录jupyterLab打开后出现“templandroidate_path”相关问题这是 tensorflo

如何解决Pycharm编辑内容时有光标的问题

《如何解决Pycharm编辑内容时有光标的问题》文章介绍了如何在PyCharm中配置VimEmulator插件,包括检查插件是否已安装、下载插件以及安装IdeaVim插件的步骤... 目录Pycharm编辑内容时有光标1.如果Vim Emulator前面有对勾2.www.chinasem.cn如果tools工

最长公共子序列问题的深度分析与Java实现方式

《最长公共子序列问题的深度分析与Java实现方式》本文详细介绍了最长公共子序列(LCS)问题,包括其概念、暴力解法、动态规划解法,并提供了Java代码实现,暴力解法虽然简单,但在大数据处理中效率较低,... 目录最长公共子序列问题概述问题理解与示例分析暴力解法思路与示例代码动态规划解法DP 表的构建与意义动

Java多线程父线程向子线程传值问题及解决

《Java多线程父线程向子线程传值问题及解决》文章总结了5种解决父子之间数据传递困扰的解决方案,包括ThreadLocal+TaskDecorator、UserUtils、CustomTaskDeco... 目录1 背景2 ThreadLocal+TaskDecorator3 RequestContextH

关于Spring @Bean 相同加载顺序不同结果不同的问题记录

《关于Spring@Bean相同加载顺序不同结果不同的问题记录》本文主要探讨了在Spring5.1.3.RELEASE版本下,当有两个全注解类定义相同类型的Bean时,由于加载顺序不同,最终生成的... 目录问题说明测试输出1测试输出2@Bean注解的BeanDefiChina编程nition加入时机总结问题说明

关于最长递增子序列问题概述

《关于最长递增子序列问题概述》本文详细介绍了最长递增子序列问题的定义及两种优化解法:贪心+二分查找和动态规划+状态压缩,贪心+二分查找时间复杂度为O(nlogn),通过维护一个有序的“尾巴”数组来高效... 一、最长递增子序列问题概述1. 问题定义给定一个整数序列,例如 nums = [10, 9, 2