0:26

I'm going to show you two functions here.

The first one is going to allow us to draw a line plot.

And I'm going to take 3 arguments.

The title that I'd like to appear in the plot and then a sequence of x values and

a sequence of y values.

And the first thing we need to do is create the line plot on which we're going

to put our data.

And you do that by calling pygal.Line.

And you'd see here I passed the height value, you need not to do that.

But it's going to make it easier to display the results in a way that you'll

be able to see in this video.

0:57

Then I can set the title to be whatever I want, so

I say lineplot.title equals some string that will become the title of the plot.

Then I want to use the x values to label the x axis of the plot.

So I set lineplot.x_labels to be xvals, so that sequence now

becomes the x values that are labeling the x axis of my plot.

Then I use lineplot.add and .add in pygal is the way to

add new sequences of data that you want to actually plot.

And you first give it the name of that data so that it'll appear in the legend.

I'm just going to call it "Data" and then you give it the actual data,

which in this case is just a sequence of y values.

And then finally, I have to render the plot somehow and I'm just going to show

them the browser, so I'm going to use lineplot.render_in_browser().

It took a little set up to get that working.

There are other rendering options that may work a little bit easier for you.

1:53

Now I have two sequences of data,

both have ten numbers in them called xvals here and yvals.

And then I'm going to call draw_line with the name "My Line Plot" and

the sequences of xvals and yvals.

So let's see what happens here.

Well, it rendered a line plot, and we can see the title, My line plot, and

then we can see my data.

Now something to notice here, notice what happened on the x-axis.

The numbers that I had in my xvals are evenly spaced, so

it doesn't interpret what's going on at all.

When I do a line plot in pygal,

all of the successive data items get plotted, evenly space.

And so you can see we have 0, 1, 3, 5, 6, and so on along the x-axis.

They're evenly spaced, there's a point at each location with a corresponding yval.

Now let's go back to the program, take a look at the program here.

I also have a function that I am calling draw_xy.

In the draw_xy function also takes a title, takes a sequence of x values and

the sequence of y values, and now I'm going to draw an xy scatter plot.

The difference between an xy plot is that I provide both the x value and

the y value of the coordinate as a tuple so

that pygal knows exactly where you want that point placed.

It does not assume that all of the values in the data should be

evenly spaced consecutively.

So at first I have to take that sequence of x values and y values, and

turn them into a sequence of tuples.

And to do this I'm going to use a list comprehension,

that list comprehension creates a tuple, xval, yval for

all the xval and yval values in, now what do we have here?

zip(xvals, yvals).

While I encourage you to read the documentation on zip, but effectively what

zip does is it takes multiple sequences and it zips them together, like a zipper.

So it takes the first element off of each of the sequences and

puts them in a tuple and returns that.

And then it takes the next element off each of the sequences,

puts them into tuple and returns that is the next value of the iteration.

So if I do that, I'm going to get a sequence of tuples where all the x

values and y values are put together and correlated.

So, you can see here, the xvals start with 0, 1, 3.

The yvals start with 4, 3, 1.

This will create tuple of 0, 4 then tuple 1, 3 then tuple 3, 1 and so on.

4:21

That creates an XY plot that we can add data to.

We set the title in exactly the same way, xyplot.title = title.

Now we don't have to tell it what the x labels are,

because it's going to figure it out based on the data that we pass it.

So then we add our data in.

We can call it data for the legend, but we're passing now a sequence of

coordinates that have xy values in them and then rendering in the browser.

So let's see what happens here.

All right, as you can see our plot looks a little bit different.

There is now not a point at every tick.

You can see that we have a point at the x value of 0, point at the x value of 1.

We don't have any point at the x value 2.

And then we have one at 3, nothing at 4, and 5 and so on.

So now because we passed xy tuples,

pygal places the points at exactly the xy coordinates we specified.

Instead of just using the x values as the labels for the x axis and

the y values as the height for each particular label.

This is more realistic if we were actually trying to

plot data that did not have sequential x values.

If the data did have sequential x values though,

you've probably realized that we'd end up with

basically exactly the same plots whether we do a line plot or an XY scatter plot.

5:37

Now you should have a feel for how to create plots using pygal.

It's reasonably straightforward.

You simply have to create a plot of the appropriate type and

then you add data to it.

You can also set the title for the plot, you can set titles for the axis as well.

I encourage you to read the documentation to find out more about the capabilities

of pygal.

But this should be enough to get you started.

So go ahead and start planning your favorite data.