How to switch backends in matplotlib / Python -


I am struggling with the following issue. I need to create reports that contain a collection of charts. All these charts, except one, have been created using Matplotlib default backend (TkAgg). A chart is needed to use the Cairo backend, the reason is that I am investigating an igraph graph and it can only be done using Cairo

The issue is that I can not change the backend on the fly, for example the following does not work:
matplotlib.pyplot.switch_backend ('cairo.png') (I know that switch_backend functionality is experimental) < / P>

and I have tried the matplotlib.use ("cairo.png") but Problems leads to import the matplotlib .use ( "cairo.png") statement should come to matplotlib.pyplot before importing. But I need two different backend during script life.

So my question is, does anyone have a code snippet that shows how to change backend in Madplastbib?

Thanks a lot!

UPDATE: I wrote a snippet that loads the matplotlib, shows the default backend, unloads matplotlib, reloads it, and backend changes:

 < Code> import matplotlib import matplotlib.pyplot such as plt import sys print matplotlib.pyplot.get_backend () module for modules in Sysmodules = []: If module: StarSwitch ('matpllib'): modules for modules in module.Append (Module): import sys.modules.pop (modules) matplotlib matplotlib.use ("cairo.png") Import Plot print matplotlib.pyplot as matplotlib.pyplot.get_backend ()  

But is this really the way to do this? <2 p> 2 Update: I had some serious brain freeze yesterday ... The simple and most obvious solution is to use the Cairo backend for all the charts and do not switch backend at all :)

3 Update: In fact, it is still an issue, that anyone knows how to switch the laptop back dynamically .... Please post your answer.

is an "experimental" feature:

  import matplotlib Pyplot As p pswitch_backend ('newbackend')  

Switch the default backend to the new backend. This feature is experimental , and is expected to only work on switching back to an image backend. For example, if you have a bunch of PostScript scripts that you want to run from the interactive IPitthon session You may want to switch to the PS Backend before running them to avoid having a bunch of GSI window popups. If you try to switch from interactive one GUI backend to another, then you will explode. Calling this command will close all open windows.


Comments