XY charts

XY charts are typically used in scientific data to show multiple values at various points. They can display negative values as well. They also overlay multiple sets of values for easy readability. Let's build a simple XY chart with two points. Copy the following code into your Python file and run the application, and save your SVG file output as xy_chart.svg:

# -*- coding: utf-8 -*-
import pygal

xy_chart = pygal.XY()
xy_chart.add('Value 1', [(-50, -30), (100, 45)])

xy_chart.render_to_file("xy_chart.svg")

Open the xy_chart.svg file; the result is shown in the following screenshot:

XY charts

Note how pygal highlights the 0 lines on both the x and y coordinates; again, this is free styling provided by the pygal library to indicate negative values. Also, take note of the add() function, of how each value is noted as an (x, y) coordinate, grouped together in an array. Let's build another chart, this time with two plots; in this case, we build with Value 1 and Value 2. Copy the following code and run it:

# -*- coding: utf-8 -*-
import pygal

xy_chart = pygal.XY()
xy_chart.add('Value 1',  [(-50, -30), (100, 45)])
xy_chart.add('Value 2',  [(-2, -14), (370, 444)])
xy_chart.render_to_file("xy_chart.svg")

Open the xy_chart.svg file; note that two line plots are present, as shown in the following screenshot:

XY charts

We will now see how to make a basic line plot in an XY chart, but what if we have multiple values on one line? Let's write one more XY chart with three values and six points per value. Let's look at the following code for this and run it:

# -*- coding: utf-8 -*-
import pygal

xy_chart = pygal.XY()
xy_chart.add('Value 1',  [(-50, -30), (100, 45), (120, 56), (168, 102), (211, 192), (279, 211)])
xy_chart.add('Value 2',  [(-2, -14), (370, 444), (391, 464), (399, 512), (412, 569), (789, 896)])
xy_chart.add('Value 3',  [(2, 10), (142, 164), (184, 216), (203, 243), (208, 335), (243, 201)])
xy_chart.render_to_file("xy_chart.svg")

When it is finished, open the xy_chart.svg file; it should look like what is shown in the following screenshot:

XY charts

Note how we can easily read each dataset. We can discern that Value 2 has the most values that are on the higher side, and also that Value 3 reached a higher point than Value 1 but dropped down quickly, which makes XY charts great for scientific data. Now, let's take a look at a variation of XY charts called scatter plots.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.31.22