Hi,
I am struggling with the following issue. I need to generate reports that consists of a collection of charts. All these charts, except one, are made using Matplotlib default backend (TkAgg). One chart needs to be made using the Cairo backend, the reason is that I am plotting an igraph graph and that can only be plotted using Cairo.
The issue is that I cannot change backends on the fly, for example the following does not work:
matplotlib.pyplot.switch_backend('cairo.png')
(I know that the switch_backend functionality is experimental)
and I have also tried matplotlib.use("cairo.png")
but this leads to import problems as the matplotlib.use("cairo.png")
statement should come before importing matplotlib.pyplot
.
but I need two different backends over the course of the life of the script.
So my question is does someone have a code snippet that shows how to switch the backend in Matplotlib?
Thanks so much!
UPDATE: I have written a snippet that loads matplotlib, shows the default backend, unloads matplotlib, reloads it and changes the backend:
import matplotlib
import matplotlib.pyplot as plt
import sys
print matplotlib.pyplot.get_backend()
modules = []
for module in sys.modules:
if module.startswith('matplotlib'):
modules.append(module)
for module in modules:
sys.modules.pop(module)
import matplotlib
matplotlib.use("cairo.png")
import matplotlib.pyplot as plt
print matplotlib.pyplot.get_backend()
but is this really the way to do it?
UPDATE 2: I had some serious brain freeze yesterday... The simple and most obvious solution is to use the Cairo backend for all charts and not to switch the backend at all :)
UPDATE 3: Actually, it's still an issue so anybody who knows how to dynamically switch matplotlib backends....please post your answer.