4

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.

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.

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.

Community
  • 1
  • 1
zephyr
  • 2,182
  • 3
  • 29
  • 51

1 Answers1

6

See this answer: How to switch backends in matplotlib / Python

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

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.

Rolf of Saxony
  • 21,661
  • 5
  • 39
  • 60