Hello and welcome to a video on line graphs. Very often when working with data, we will want to visualize this data
before doing anything further to it. The reasons for this are multitude. Perhaps we want to know if our data has any outliers
in it or with the shape of our distribution is. Whatever our reasons, there are lots of ways to visualize our data.
Let's try a simple line graph today. These are useful when you want to highlight changes over time in your data, such
as sales numbers. Let's pretend we're working for a furniture sales company and they're looking at
last year's numbers. To do this, we'll use map plot lib. We'll generate some sample data.
So we're going to import numpy as np. And this is just to generate the sample data down here. And we're going
to import mapplotlib.pyplot as PLT. And that's to actually create our plot below.
Let's run this code. And this generated our months 1 through 12 January
through December and our sales, which are randomly generated numbers between a certain
value and another certain value. If you're curious about this code, feel free to play with it and run it on your
own. Next, let's make our plot. Notice there are many things that
we can do to a line graph to change the color of the line, label or data, change the titles, et cetera. First of
all, we're going to set our figure size. This isn't really necessary. You can actually take this line out if you want to,
but we want our plot to be nice and big this time. PLT.plot actually creates the
plot and changes and let's it know what we want for our x-value, what we want for our y-value, which in
this case we have to find above. So x is this, why is this?
Our marker style is set here, our line style is set here. We have our color set to blue,
label equals sales trend. We've also titled our plot, given it
x labels, y labels, and given it ticks. The ticks are the things at the bottom
that tell you what your y-ax or your x-axis is.
Then we also have our legend and we'll set our grid to true for this. So I'll run
this plot and you can see now we have monthly sales over a year and
we started pretty poorly in month one and just generally grew all the way up to month 12
which I mean isn't that what you want to see. Our pretend company is doing very well in their first year.
If you wanted to use data from a pandas data frame you would simply change the x and y to the values you want to use.
In data visualization we are primarily concerned with how easy the information is to read. However we may also
have company guidelines to follow. Let's say we work for a company our furniture company who wants to produce
this graph to certain specifications. To improve readability on this plot we've rotated the month labels 45
degrees, changed the color of the line for aesthetic reasons and remove the vertical red lines. We've also changed
the points of the labels because this is the style our pretend company has asked for.
So we've changed a couple of things here, x and y are still the same. We've left the figure size the same but this will make
our points pixels instead of those circles. Our line is still a solid line. We've changed the
color to forest green and down here in our x-ticks we've added
an argument to change the rotation of the ticks by 45 degrees.
And now here under plt.grid we've selected axis = y
so it's only going to use the y-axis. Okay we'll
go ahead and run that and this is the style that our company
has asked for. You can find lists for different
options you have for plots by Googling matplotlib.pyplot options. This will bring up lists of different markers,
colors, et cetera. An excellent resource for matte-plot-lib is their website, https://matplotlib.org/
Feel free to take a look there and see what customization options are available. Let's go there now.
You can see here we've got our documentation.
It'll tell you how to install matte-plot-lib, give you API references,
and then it'll give you tutorials, user guides,
examples, references, all that good stuff, plot types,
so there's a lot to explore here. All right, have fun making your first line graphs.
and we'll see you in the next video.