Matplotlib is one of the most important plotting libraries in python.
In the modern days,
there are many plotting tools, but Matplotlib pyplot is one of the most useful tools. And this
plotting library itself, the whole plotting module itself, is inspired by plotting tools that were
available in Matlab. And the reason for this might surprise you, that lot of machine learning
and data science and AI scientists actually come from other areas, like physics, like
astronomy, like applied mathematics and statistics, like applied math and statistics, and also
a lot of engineers from electronics. All these areas, of course, there are, of course, cs folks
out there, but many of these people in sciences and in electrical engineering and applied
math are used to Matlab. Matlab is one of the most popular scientific computing toolboxes
out there, especially for scientific computing. So when people started building a python
specific plotting library for machine learning, data science and AI, they said, okay, we are all
used to Matlab syntax. We all are very used to how to plot things in Matlab. So why don't we
build a plotting library just like the one that we are used to in Matlab? And that's the
inspiration for Matplotlib pyplot. So, having known this, now let's go and look at some very
simple examples. I'll explain you some basics of plotting. For more details about plotting, we
have a chapter called exploratory data analysis, where we look at multiple types of plots.
And in exploratory analysis, in addition to pyplot, we also use another library called cborn,
which has extremely beautiful and nice plotting tools. We will learn about seaborn in the
exploratory data analysis chapter. Okay, now let's import it. Okay, so you can import
matplotlib pyplot as PLT. So this is the alias that I'm using because I don't want to keep
typing Matplotlib pipelot every time. Now let's look at it. Suppose if I want to plot these
values, okay, so what I have here is these are my y values, right? Two, four, six, and four,
right? And if you look at this plot here itself, if you look at this plot here, what were my
numbers? Two, four, six, and let me just look it up, okay, six and four, right? So these are the
y values, right? These are the y values. There were no x values specifically mentioned for my
plot. If the x values are not mentioned, then the indices like 0123 become my x axis. Because
when I'm plotting, I didn't mention anything about my, anything about my x axis, right? So if
no x axis values are given your indices, because this was at index zero in the array, this was
at index one, this was at index two and index three, the same value. So if you notice here at
zero, you have value two. At one, you have value of three, roughly here, sorry, one, you have
value of four. Sorry, my mistake. At two, you have a value of six, and at three, again, you
have a value of four. Now, these four dots are connected now. So your pipelot, lib. Now
pipelot, your matplotlib. Sorry, not pipelot lib, your matplotlib. If you just say plt plot. And I
just gave, and I just gave a list, right? I just gave one list. So it will plot these list elements as
elements of the y axis with the indices this being at zero, th index, first index, second index,
third index as the corresponding x axis. Now, in this code, one thing you'll notice is I have
set something called y label as numbers, x label as indices, and title as my plot, and I've set
plot show. These are the four lines of code that I have. Let me explain each of these pieces.
When I say plot, title, myplot, what you see here is called the title of the plot, okay? What
you see here is called the y axis label, or the Y label because this is the y axis, right? So if you
look at this plot, this is the x axis, and this is the y axis, right? And we had these four points.
And we had these four points, right? So your x axis points were indices. So, 0123, your y axis
points were two, four, six, and four, exactly those points. And we said the title. This is called
the title, and this is called the x axis label. And this is called the y axis label. For a given plot,
these three things are important. This says what the plot is all about. This says what is the x
axis? Here I've said indices, right? Because those are the indices or the indexes of each of
these elements. And hence I call it indices. And the y axis is the number that I'm plotting.
Now, to do that, it's very, very simple. You just have to say X label and Y label. If you say plot
x label, it sets that as x label. If you say Y label as numbers, it sets that as numbers and title,
right? So there is one problem with this plot. If you have noticed one problem with this plot
is that I don't see a grid. See, one of the other useful things in a plot is a grid like structure
like this. Right? A grid helps you read these values much more easily. That grid is not
present. We'll see future code where we'll see how to get that grid. Right. Now look at this.
Now, in this case, instead of giving one array of numbers, I've given two lists. When I give
two lists, this becomes my x axis and this becomes my y axis values. One thing you'll quickly
notice is if my x axis is one, my y axis is one. If my x axis is two, its corresponding value is
four, and four is nothing but two squared. Similarly, three squared is nine, four squared is
60. And what is my x label? My x label is numbers. My y label is squares. I have not set a title
here, but here, there is one interesting line here. Here I'm saying plot grid. The moment you
say plot grid, you get this grid. This is what I mean by a grid, right? So let's go down and see
this plot. Right? For one, you get a value of one. For two, you get a value of four. For three,
you get a value of nine. For four, you get a value of 16. And you can much more easily read
these numbers because of this grid that you have here. This is called the grid. This is called
the grid. And how do you set the grid to be visible? For example, in the previous plot, we did
not have the grid. And you can just set the grid by saying plt grid, right? So then the grid is
visible, and you know how to set the x label and the y label. I haven't set a title here, but you
can set it if you choose to. So those are very, very simple commands which will help you
plot. Oh, again here. Remember, instead of giving one list here, I've given two lists. Hence
this becomes my x axis values and this becomes my corresponding y axis values. Now let's
go down a little. Now, instead of a line plot, if I want to get a slightly different plot, let's look
at this plot here. Here. What I'm saying here is plt plot. These are my x axis values. These are
my y axis values. Again, squares of each of them. But here I'm saying ro. So what ro means is
r here means the color of each of these dots is red. That's what the r here means. And o
basically means that. Put dots here, put o shaped dots here instead of connecting them with
a line. That's what it means. We'll see more examples as we go forward here. Also, I'm
setting the grid to be present. And I'm saying plot show. I don't have any x label, y label or
title here. Right. Now let's go down and look at slightly more interesting things, right? So
here, look at this, look at this plot here. What am I saying? First on the same plot if I want
multiple things to be plotted, right? So let's go and look at this code first, and then we'll go to
the plot. So here I'm saying that t is basically a numpy array with values between zero and
five and with intervals of zero, two, right? That's what I'm creating, right? So I have values
like 0.2.4, so on, so forth up to 4.8. Right? Now comes the interesting part. Now what we see
here is I'm saying plot, sorry, plt, plot T and T square. And here I'm saying that I want b
what b hyphen. Hyphen basically means is I basically want blue dotted line, or I should say
dashed line, sorry, it's not called dotted line, it's called a dashed line. Similarly, I am plotting
t and t power 2.2 here, star, star basically means exponent, right? So each element in t I am
exponenting with 2.2. Here, I want red, and s stands for squares, okay? And I'm also giving a
label here. I'm saying this line is square power two. This is just any string you can give the
label for this plot is power 2.2. Similarly, I am plotting T versus T power 2.5 and I'm saying
green. And when I say a cap, when I say this, what it means is triangle. That's what it means.
That means a triangle. And this symbol is literally above your. If you look at your keyboard
on top of six, you have this sign. It's often called as a caps lock or it's also called as exponent
key. Right? It's right above the key six. On most laptops and desktops, on most keyboards.
Here also I'm giving a label saying this plot corresponds to exponent 2.5. And I'm saying
grid. And here there is another interesting line called Legend. I'll explain you what legend is.
And I'm saying show. Now let's go down to the plot. As you have seen, this is what is called a
legend in a plot. This is called a legend in a plot. A legend. When you have especially
multiple lines, a legend explains you understand, it helps you understand which line
corresponds to what. And in legend, in Python, legends are filled with. So here I've given a
power two, power 2.2, power 2.5. Those are nothing but the values that I have in the labels.
The values that I have in the labels come up in the legend. So you can give any value, any
value here, and the same values will come up in the legend here. Legends make it extremely
readable. Legends make a plot extremely readable. Now, by looking at this, I know that,
okay, this blue line is basically power two. This red line is Power 2.2. This green is power
2.5. And as you know, if you go back and see, the blue dashed line is squared, the red
squares and the green triangles. So you can plot multiple lines in the same plot by just
repeating PLT plot. And when you have multiple lines, it's extremely important to have a
legend. Otherwise you don't know what is what. You'll simply be lost. Right, having lent this,
now let's go a little more. Let's understand some more properties. Suppose if I want the line
width to be more here, I can say plt plot x comma y. I have my x and y lists here, which I'm
using here. And I have a parameter called line width. And if I give a line width of very large
value, I get a line here which is very wide, right? And I'm just saying plot show. There are
many, many other, very many other parameters that you can look up in the documentation
for this function. In Matplotlib pyplot for the plot function, there are many, many
parameters that you can use. One such parameter, I'm just showing you here, is called line
width. Now there is another very interesting thing called set properties. So let me explain
you what it is, okay? Imagine I have x one, y one as squares, x two, y two as double twice of
these numbers. So these are four sets of lists that I have in a single line. You can plot both
these plots, both these lines, you can say plt plot x one, y one and x two, y two. So now what
it does is it plots x one versus y one. So this is your x axis and y axis. It plots x one versus y
one. It also plots x two versus y two. Now the fun part is this. What PLT plot returns is
basically another variable called lines. And here it's plotting two lines, right? So I have
actually lines zero and lines one. And now I can change the properties of these lines as
follows. Let's look it up. So I can say PLT set properties for line zero. I can say I want the
color to be r. What is line zero? Line zero corresponds to the line between x one and y one.
Okay, this is line zero, and this will be line one. And I want the line width to be two. This is
one way of writing it. This is using the keyword arguments, right? There is also Matlab style
arguments, which is, I can say plt set properties for line one. If I'd say color and g one after
the other, what this is equivalent to, these two pair of commands are equivalent to color
equals to g. That's what it means. And this is equivalent to exactly this. So this is how we
actually use command line arguments in Matlab. Since, as I told you, matplotlib is very
inspired from Matlab, we can simply take the, this is a syntax. This is a Matlab syntax. You
can do this way or this way. If you're more of an original Python guy, you might want to
prefer this, because this is very native to how we think in Python. Right. These are your
keyword arguments. If you come from other branches where you have used Matlab
extensively, you might find more home, you might find this more similar to what you have
used in the past. But in both these cases, see, I've changed my first line to color red, my
second line to color green, and that's what you see here, right? So you have both these two
lines, and you see the colors having changed. Now, what if I want multiple plots? Okay, so
we've seen how to play with one plot. Now here is an interesting part. So look at like this,
right? I have two plots here. I have t one, t two, and I have a function f, which uses some
exponent followed by cosine and things like that. Let's not worry what it does internally. I
have a function. Now, when I say plot figure one, it creates a figure for me in this figure. So it
literally creates a figure for you, right? In this figure, I can create subplots. Okay? So when I
say plT, subplot two, one, one, what it means is I want two subplots. What this means is I
want two rows and one column. So what it's saying here is I want subplots with two rows,
row zero and row one and one column. Okay? It's breaking up this whole figure into sub
blocks. And it's saying this is the number of rows, right? This is the number of columns, and
this is the figure number. And what is the first figure? First figure is here, right? This is the
first figure, and this is the second figure. It's saying plot subplot, two rows, one column, and
the first figure is what I want in the first subplot is what I want to plot in the first subplot.
I'm saying plot grid and plot plot t one f, t one with a blue dashed line. With a blue single
line. If I put double dash, it becomes a dashed line, a blue colored line. So what happens is
I'll get a plot here, I'll get some plot here, I'll get some plot here. Now, I say plt, subplot two,
one, two, which means two rows, one column, and figure two, which means it is this part,
right. Two rows, one column, and figure two. Here it's plotting some other plot. Right. Here
it's plotting something else. Now I'm saying plot show. So let's see the output of this. The
output of this is basically two plots where the first plot is this, where the first plot is the
output of this second plot. This is the output. This is how in a single figure, you can break a
figure. You can break a single figure into parts. So this is your whole big figure, which we
have broken up into two parts. This is row zero, row one, column one. This is your figure
one, and this is your figure two. This is how we broke it up. If you have to plot multiple
figures, there will be cases where you want to plot four figures. One figure here, one figure
here, one figure here, one figure here. All of that is possible using the PLT figure and PLT
subplot. We'll see slightly one more interesting, complicated case here. Okay, so let's look at
this. So here I'm saying figure one. So it creates space for figure one, right? In this, I'm saying
plots two, one, one, which means it's now broken this up into two subplots because it has
two rows, one column, and in the first, this is the first figure. This is the second figure, right?
In the first figure, I'm saying one, two, three. Plot one, two, three. So I get a plot like this. In
the second plot, I'm saying plot four, five, six. I get one more plot here. Now, here I'm saying
plot figure two, right? So figure one is over. Now it creates one more figure right below this.
And in this, I am saying plot plt, plot four, five, six. So it just plots one, one value here.
Because I've not created subplots here in figure two, I have not created subplots. This is my
figure one. This is my figure two. Right? Now, after plotting figure two, after plotting figure
two, if I want to go back to figure one, and in figure one, if I want to go to this subplot and do
something with it, how do I do it? I just now say figure one. As soon as I say figure one, I go
back here. In figure one, which subplot do I want to play with? I want to play with the two
rows, one column, the first plot, which means this is this two rows, first column. And this is
the first subplot for this subplot. Now I want to say plot title. Suppose if I want to write
some title here, I can just say easy as one, two, three, as my title, and just show. Now this is
what should happen. You understood the flow, I hope, right? Now let's see the output of this.
Exactly what we expect. This is your figure one. Figure one has two subfigures, two rows
and one column. My first figure I have easy as one, two, three, which I changed at the end,
right? And when I go down, I have my second plot here. What I would recommend you is
play with this change, add one more figure, play with this change the subplots, play with all
these changes so that you understand how to create multiple figures or multiple plots using
this simple function. All you have to remember is the figure command and the subplot. And
you can go from, you could have a complex structure where you have two figures here and
you have a big figure here, and you have two more figures here. That's very possible, right?
So this thing could be figure three, this thing could be figure two. This thing could be figure
one. And at any time you can go to any plot and change some property of the plot. For
example, you can go here and change. So I would like you to play. So the example that I have
written here is the simplest of all. So there will be cases where you have to make plots like
this. And I recommend you play around with this and understand how to create different
types of plots. And matplotlib is extensively used in machine learning and data science, as
you will see in a lot of our work. I recommend you check out exploratory data analysis
chapter where we use, in addition to Matplotlib, we also use seaborn a lot for various types
of plotting.