Python: OSError: cannot load library libcairo.so.2
On Mac OS X using homebrew: brew install cairo brew install pango
On Mac OS X using homebrew: brew install cairo brew install pango
the option -T svg worked for me
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
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
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
Had the same problem and @Epistemex’s link helped me troubleshoot it. … You need to install libcairo2-dev, libjpeg-dev and libgif-dev packages … sudo apt-get install libcairo2-dev libjpeg-dev libgif-dev
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