20

I am using python (with a virtual env in LinuxMint), I installed pygal.

Everything works fine (rendering to html) but not rendering to svg or png . The result : Nothing but a black background.

I installed cssselect and tinycss like mentioned here .

It works for the first time, but when retrying, I had the same issue .

(I don't know if this is related or not, but this happens to me when exporting a photo using darktable last week)

I use the example from the website of pygal:

import pygal                                                       # First import pygal
bar_chart = pygal.Bar()                                            # Then create a bar graph object
bar_chart.add('Fibonacci', [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55])  # Add some values
bar_chart.render_to_file('bar_chart.svg')                          # Save the svg to a file

EDIT:

bar_chart.render_to_png('bar_chart.png')

is working now .

But not:

bar_chart.render_to_file('bar_chart.svg')
4m1nh4j1
  • 4,289
  • 16
  • 62
  • 104
  • 1
    I was getting black svg images in when I was viewing them in Gimp or the default Image Viewer on Ubuntu. The images show up fine when I open them in Chrome. I don't know why this is happening though. – Aneesh Dogra Aug 24 '15 at 12:56

4 Answers4

32

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]

#Create a PNG file with Pygal
import pygal

bar_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.

emk
  • 580
  • 1
  • 6
  • 12
  • 1
    Actually this is the answer -- I got the same issue as question author and was able to solve it with help of @emk 's comment. Thank you! – Illarion Kovalchuk Mar 24 '15 at 08:59
  • If you have to open it in a particular browswer for it not to look like crap (I do), then it just ain't working. I'll stick with matplotlib until they work this out. – eric Jul 28 '17 at 00:40
  • Yep, on Ubuntu, after installing requirements the image was still black. Opening in Chrome worked though. – Joshua Patterson Apr 28 '20 at 23:48
1

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.

Peter
  • 13,733
  • 11
  • 75
  • 122
1

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

pip install lxml
pip install cairosvg
pip install tinycss
pip 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 cairosvg
cairosvg.svg2svg(url=file_svg_name, write_to=file_svg_name)

It worked for me.

0

I had this problem when trying to convert pygal-made svgs with Inkscape. Firefox and chrome would display them just fine, but Inkscape would draw a black block.

I found out that colors specified as rgba(0, 0, 0, 0.9) do not work with inkscape, but #f8fD00 do work.

I selected SolidColorStyle in pygal which happens to use only the #-form.

Sairam
  • 375
  • 1
  • 5
  • 28
H.N.
  • 1