Gauge charts display data in a graph style that is similar to speedometers in automobiles. They also work well with multiple datasets but not one with a single dataset. In the following code, we have an example of a gauge chart, which is a very simple pygal chart. This time, we will use some new data; in this case, we will use a dataset that represents the space shuttle speed from the time of launch to 20 minutes.
Let's look at our sample code and our chart in the following code snippet. Copy the code into your editor of choice, and ensure that you save the file to gauge_chart.svg
:
# -*- coding: utf-8 -*- import pygal gauge_chart = pygal.Gauge() gauge_chart.title = 'Speed of space shuttle during takeoff' gauge_chart.x_labels = ['Pre-takeoff', '5 min', ' 10 min', '15 min', '20 min'] gauge_chart.add('Pre-takeoff', 0) gauge_chart.add('5 min', 96) gauge_chart.add('10 min', 167) gauge_chart.add('15 min', 249) gauge_chart.add('20 min', 339) gauge_chart.render_to_file('gauge_chart.svg')
Open the gauge_chart.svg
file, and the result will be as shown in the following screenshot:
Looking at gauge_chart.svg
in our browser, we can see a simple gauge chart. However, notice that the speed values are floats. If we are working with complex floats with multiple decimal points, we can simplify this chart and trim those floats off using human_readable=True
in our Gauge()
function, as shown in the following code:
# -*- coding: utf-8 -*- import pygal gauge_chart = pygal.Gauge(human_readable=True) gauge_chart.title = 'Speed of space shuttle during takeoff' gauge_chart.x_labels = ['Pre-takeoff', '5 min', ' 10 min', '15 min', '20 min'] gauge_chart.add('Pre-takeoff', 0) gauge_chart.add('5 min', 96) gauge_chart.add('10 min', 167) gauge_chart.add('15 min', 249) gauge_chart.add('20 min', 339) gauge_chart.render_to_file('gauge_chart.svg')
Now, let's update our chart and look at the results.
This looks good; using human_readable=True
will help trim long values in our charts and help prevent overlapping. Notice the values in the preceding screenshot; any decimal values are now trimmed for our chart's labels.
18.119.28.108