在Mac OS X上以编程方式为Matplotlib选择正确的后端 [英] Programmatically choose correct backend for Matplotlib on Mac OS X

查看:124
本文介绍了在Mac OS X上以编程方式为Matplotlib选择正确的后端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序,该程序集成了tkinter GUI和matplotlib图(使用pyplot).让该程序在各种Mac平台上正常运行时,我遇到了无尽的麻烦.主要的问题似乎是后端的适当选择.

I have a program which integrates a tkinter GUI as well as a matplotlib plot (using pyplot). I'm running into endless troubles having this program work correctly across a variety of Mac platforms. The main problem seems to be the appropriate selection of the backend.

在某些情况下,程序运行正常没有问题.在其他情况下,我遇到了此问题中记录的类似问题.实施此处概述的解决方案可以解决该问题,但是其他系统会弹出其他错误.解决这些其他错误的方法似乎是使用Qt4Agg后端.

In some cases, the program runs fine no problem. In other cases, I run into a similar issue documented in this question. Implementing the solution outlined there solves that problem, but then other errors pop up for other systems. The solution to these other errors appears to be to use the Qt4Agg backend.

必须有一些同时使用tkinter和matplotlib来获取程序的标准方法,以便与Mac兼容.如何以编程方式确保使用了正确的后端,以使该程序对于Mac用户不会崩溃?

There has to be some standard way of getting a program using tkinter and matplotlib at the same time to play nice with Macs. How can I programmatically make sure the correct backend is being used such that the program won't crash for a Mac user?

很抱歉,如果它含糊不清,但这是一个非常广泛的问题.

Sorry if this is vague but it is a very broad problem.

推荐答案

查看此答案:本质上,如果您不知道哪个backend可用,则以下代码应加载当前计算机上可用的第一个backend. (我只包含4个后端,还有很多其他后端.)

In essence, if you do not know which backend is available, the following code should load up the first backend that is available on the current machine. (I have only included 4 backends, there are quite a few others).

import matplotlib
gui_env = ['TKAgg','GTKAgg','Qt4Agg','WXAgg']
for gui in gui_env:
    try:
        print("testing", gui)
        matplotlib.use(gui,warn=False, force=True)
        from matplotlib import pyplot as plt
        break
    except:
        continue
print("Using:",matplotlib.get_backend())

Using: GTKAgg

更新:我被认为是OSX的一个后端,称为MacOSX,尽管我自己无法对其进行测试,但可以将其添加到该列表中.

Update: I am lead to believe that there is a backend for OSX called MacOSX which could be added to that list, although I have no way of testing it myself.

这篇关于在Mac OS X上以编程方式为Matplotlib选择正确的后端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆