pygal rendering png/svg black pictures pygal rendering png/svg black pictures python python

pygal rendering png/svg black pictures


You need to install lxml as well. So assuming you are in a virtualenv run the following command on your bash/zsh prompt:

pip install lxml

If you only have the other 3 libraries, i.e. cssselect, pycairo, tinycss. Then you will be able to properly render an SVG but the PNG render function will produce a solid black image file (without lxml installed)

The gist below shows all the steps:

[FIRST: install the required libraries]

  • pip install lxml
  • pip install cairosvg
  • pip install tinycss
  • pip install cssselect

[SECOND: create the file]

"""pygal_render_png""""import pygalbar_chart = pygal.Bar()bar_chart.add('Fibonacci', [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55])bar_chart.render_to_file('bar_chart.svg')bar_chart.render_to_png(filename='bar_chart.png')

If you get black svg images in Image Viewer (Ubuntu) or Gimp, try opening the image in Chrome.


Just in case anyone else encounters something similar, my problem was that the SVG looked fine in a browser, but no in Inkscape. I was using custom css, and setting fill: transparent on some elements. It should be fill: none.


1) Install the dependencies as documented (http://pygal.org/en/stable/installing.html)

pip install lxmlpip install cairosvgpip install tinycsspip install cssselect

2) Create chart and render to file

line_chart.render_to_file(file_svg_name)

3) Create svg again (using the same file), but using cairo lib

import cairosvgcairosvg.svg2svg(url=file_svg_name, write_to=file_svg_name)

It worked for me.