Skip to content

Latest commit

 

History

History
78 lines (46 loc) · 2.68 KB

first_steps.rst

File metadata and controls

78 lines (46 loc) · 2.68 KB

First steps

Caution

First you need to install pygal, see installing.

When it's done, you are ready to make your first chart:

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

Now you should have a svg file called bar_chart.svg in your current directory.

You can open it with various programs such as your web browser, inkscape or any svg compatible viewer.

The resulting chart will be tho following:

bar_chart = pygal.Bar() bar_chart.add('Fibonacci', [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55])

Caution

pygal relies on svg css styling. This is sadly not fully supported by gnome librsvg and therefore can lead to black svg being displayed. This is not a bug in pygal. See this bugzilla search

To make a multiple series graph just add another one:

bar_chart = pygal.Bar() bar_chart.add('Fibonacci', [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]) bar_chart.add('Padovan', [1, 1, 1, 2, 2, 3, 4, 5, 7, 9, 12])

If you want to stack them, use StackedBar instead of `Bar`:

bar_chart = pygal.StackedBar() bar_chart.add('Fibonacci', [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]) bar_chart.add('Padovan', [1, 1, 1, 2, 2, 3, 4, 5, 7, 9, 12])

You can also make it horizontal with `HorizontalStackedBar`:

bar_chart = pygal.HorizontalStackedBar() bar_chart.add('Fibonacci', [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]) bar_chart.add('Padovan', [1, 1, 1, 2, 2, 3, 4, 5, 7, 9, 12])

And finally add a title and some labels:

bar_chart = pygal.HorizontalStackedBar() bar_chart.title = "Remarquable sequences" bar_chart.x_labels = map(str, range(11)) bar_chart.add('Fibonacci', [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]) bar_chart.add('Padovan', [1, 1, 1, 2, 2, 3, 4, 5, 7, 9, 12])

The public API is chainable and can be simplified as call arguments, the last chart can be also written:

bar_chart = pygal.HorizontalStackedBar(
  title="Remarquable sequences", x_labels=map(str, range(11))(
  0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, title='Fibonacci')(
  1, 1, 1, 2, 2, 3, 4, 5, 7, 9, 12, title='Padovan')