How to change matplotlib backends

Six years later and I came across a similar issue, when trying to decide which backend was available to use. Note see Caveats – below This code snippet works well for me: 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 … Read more

How to change backends in matplotlib / Python

Six years later and I came across a similar issue, when trying to decide which backend was available to use. Note see Caveats – below This code snippet works well for me: 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 … Read more

How can I get the output of a matplotlib plot as an SVG?

You will most probably want to fix the image size and get rid of all sorts of backgrounds and axis markers: import matplotlib.pyplot as plt import numpy as np plt.figure(figsize=[6, 6]) x = np.arange(0, 100, 0.00001) y = x*np.sin(2* np.pi * x) plt.plot(y) plt.axis(‘off’) plt.gca().set_position([0, 0, 1, 1]) plt.savefig(“test.svg”) The resulting SVG file contains only … Read more

Convert SVG to PNG in Python

Here is what I did using cairosvg: from cairosvg import svg2png svg_code = “”” <svg xmlns=”http://www.w3.org/2000/svg” width=”24″ height=”24″ viewBox=”0 0 24 24″ fill=”none” stroke=”#000″ stroke-width=”2″ stroke-linecap=”round” stroke-linejoin=”round”> <circle cx=”12″ cy=”12″ r=”10″/> <line x1=”12″ y1=”8″ x2=”12″ y2=”12″/> <line x1=”12″ y1=”16″ x2=”12″ y2=”16″/> </svg> “”” svg2png(bytestring=svg_code,write_to=’output.png’) And it works like a charm! See more: cairosvg document