How to Graph Data in Python Using the Matplotlib Module!

LeMaster Tech
182 followers
18 Video Views·Apr 21, 2025
The matplotlib module along with NumPy are two of python's most frequently used modules for data analysis and data science!
In this video we cover how to use three basic matplotlib graphs, the bar chart, the standard plot, and the scatter plot, to visually represent data sets.
Be sure to let me know what you want to see next on the channel in the comments below as well as any questions you had here, and don't forget to leave a like on the video and subscribe to the channel!
Consider becoming a super supporter of the channel below! https://www.patreon.com/LeMasterTech
what's up you guys welcome back to the
channel in today's video I'm gonna show
you how to create three of the most
common most basic types of plot using
the matplot lib module in Python we're
going to cover how to do a basic plot a
scatter plot and a bar graph using
matplotlib and some numpy
[Music]
alright so let's jump right into it um
python is an extremely useful
programming tool for data analysis
you've almost certainly heard that if
you're a beginner or intermediate python
programmer and two of the most common
tools that you will use in Python for
data analysis are numpy which is
essentially a python numerical
computation module that a lot of the
other modules build off of including
matplotlib which is a really useful
mathematical tool for plotting uh
different data sets so these are both
really useful to know if if you want to
get into python for data analysis so
let's just start by importing matplotlib
dot Pi plot which is going to be the
piece that we are actually going to use
as our plot for all three of the types
of graphs that we're going to cover in
here today and we also want to import
numpy and you'll most commonly see this
abbreviated just as NP it's a really
easy way to use the numpy module without
typing out numpy but you absolutely
could type out numpy if you wanted okay
and so real quick you don't have to
worry too much about what this means but
we are going to use a specific style of
plot just a underscore MPL Dash Gallery
okay so uh this is kind of one of just a
basic command if you really want to look
into the different styles that you can
use matplotlibs documentation is pretty
good online so feel free to look it up
if you want to use other styles and then
the actual plot has this thing called
subplots that has uh its figure size and
definition in this first variable and
then it stores its axes in a second
variable and so again I I'm gonna try to
get through three different types of
plot and I can maybe do a longer video
on a deep dive into matplotlib in the
future
but basically we want to get this plot
dot subplots right here in the beginning
because we're going to use this to uh
tell our plot later on in our program
what the different axes are going to
look like so what our X and Y data are
going to be made up of and to do that we
want to break plot dot subplots into
this fig and ax sub parameter okay so
these variables and now let's start our
basic plot section okay so the code we
need just to make our basic plot and I
will also throw in here that at the very
end you don't want to forget this step
or you'll think that your code is not
working you always want to make sure you
have plot.show at the end because that
is what is actually going to display
your graph onto the screen
but now I don't have specific data
prepared for this for this tutorial so
let's go ahead and make a set of data
that are just going to be a sine wave
all right and to to do that I'm going to
use the
numpy linear spacing so Lin space to
basically give me data from 0 to 10 and
then I'm going to say how many points I
want in between those okay so this is
essentially
saying give me 100 evenly spaced points
between 0 to 10. all right so that is
what my X data is going to be and now
I'm going to use uh that same set of
data to give me a sine wave by just
saying y equals and then NP DOT sign and
then times x this would sort of be how I
would get a generic sine wave now let's
go ahead and increase its frequency and
amplitude just so it's a little bit more
interesting in the span we just defined
and so I'm just going to add in some
variables like four plus so you should
know a normal sine wave will go from 1
to negative one and now we're just
increasing the speed which it oscillates
here and then we are going to say that
actually ours is going to go from seven
to one so it's gonna four is going to
replace 0 as kind of our uh y you know
zero point and then we can go up three
or down three so it's just a more
interesting
um uh set of data so that you can see
this but so basically this is y sine
wave from one to seven
um and then that'll be our data and so
what we want to do to use this data is
we say that X that axes variable that we
already made Dot Plot and then we give
it our X set of data our y set of data
and then an optional variable because by
default it'll give you just one but an
optional variable is also to increase
the line width so I'm going to go with a
line width of 2. and that's actually all
the setup that we have to do now we just
have to essentially tell the axes set
the axes with how to display and then
it'll be ready to go so we do that in x
dot set and then we are going to say
what we want the X limits of our uh
graph to be and I want mine to be
between 0 and 8. so
um I just basically know that I'm going
from 0 to 10 and so I'm going to say the
range that I want to display is from 0
to 8. these are the X Min and Max limits
and then uh just another again this
one's optional but the number of ticks
that you want to have show up are going
to be a range between one and eight okay
so uh I'm gonna copy this whole ax dot
set in innards so this chunk and I'm
just gonna copy and then put on a second
row the same thing but for the Y limit
and the Y ticks okay so basically I'm
making a square here that's going to
show me zero to eight in the X and the y
direction and give me a tick on my axes
every one so one two three four five six
seven eight okay and this is actually it
this like 18 rungs in most of it is
spaces this is pretty much all we need
for a basic plot in numpy so let's go
ahead and run this
and you can see that we get a sine wave
and if you hover your mouse over it you
can see that the low Point here for the
Y's is one and the high point is seven
and that the right edge of the graph
goes up to eight so you could say you
could set your X limit a little bit
higher like we could set it to 10 and
rerun it and what you'll see now is that
you go all the way up to 10. so feel
free to read the documentation on
matplotlib and start looking at all of
the optional parameters four basic plots
and every other type of plot we cover
here
but for now I'm going to comment out the
stuff that is just for the basic plot
and we're going to move on to a scatter
plot I don't want this uh lesson to get
super duper long
um just in case what you guys are really
looking for is a crash course in the
three most common in my opinion most
common types of plots with matplotlib so
let's take a look at a basic scatter
plot okay and so to do this you may have
noticed that I I commented out the
section that was specific to basic plots
but I'm actually leaving these last two
commands the ax.set plot dot show and my
initial setup in here and that's because
the code that I didn't just comment out
is much more generic and you can use it
for all these different styles of plot
it's this section in here where you're
telling it what kind of plot to use so
let's go ahead and take a look at what
needs to change for a scatter plot
and so let's start by getting some new
data
um I think first let's get some
randomized X and Y data that's kind of
the most interesting scatter plot
anyways so we'll use a normal random
distribution
um for our uh X data and we'll just say
um that we want it to be
centered on zero and then we give it a
scale and size of 2 and then 24. and
then we'll go ahead and get Y randomized
data as well that will be uh and I say
centered on zero because we're adding 4
to it so really the values that you'll
see are centered on 4 4 but our random
data that we're grabbing is centered
around zero plus or minus two so
now we're going to do the same thing for
y random normal and then zero two and
then here uh one thing that's nice to
make sure you get two lists of the same
length is that you can use length of X
instead of just 24 and this is just a
smarter way of doing it because if you
were to ever decide you wanted a larger
or smaller sample size now they would
grow together or Shrink together so that
is a good idea to do
and I'll just throw in here that the
three variables we're giving it are the
location which is also known as Center
and then uh scale and then size and
again numpy super common and well
documented so I'm trying to cover a lot
of content this video in a pretty short
time frame if you have any specific
questions feel free to drop them in the
comments below now two things that we'll
do to our scatter plot that make it a
little bit more interesting is we'll do
sizes and colors uh randomized as well
so for our sizes list we'll go ahead and
do NP dot random and then this one will
do uniform instead
um again feel free look up the
difference but basically we're going to
give it a low point a high point and
then a total size and since we want uh
all of our lists to have the same number
of points we're going to use length of X
again for sizes and we'll do something
very similar for our colors list NP dot
random dot uniform and we'll go from 15
to 80 and we'll make it length of X okay
so I think I have an extra set of
parameters around that length of X I did
yeah but now I don't
um and so again just to reiterate the
arguments that we're giving it here are
low
High
and then size okay those are the three
things that we put in those uh
NP dot random dot uniform now let's
actually create the plot and for this we
need to give it the X and Y arrays of
data and then the sizes and colors are
optional but we made the list so we may
as well use them and then the data range
for color map is going to be something
that we pass in as well because we are
generating a color mapping with that
colors list so for this it's x dot
scatter instead of X Dot Plot and then
it is going to be X data Y data and then
you need to use S equals sizes and C
equals colors and then the color mapping
minimum and maximum so that is vmin zero
in Vmax uh to a hundred so that's where
this 15 to 80 comes into play we're
saying all right our lightest color
would be zero our darkest color would be
a hundred
so let's make all of our data somewhere
in between like 15 and 80. so they'll
all be pretty easy to uh see but also
distributed all over the map okay so
I know this has gotten a little bit
trickier to read especially because I've
commented out plot code there in the
middle but hopefully you're taking away
that this stuff here is scatter plot
specific creating our X set of data our
y set of data the data sizes and colors
and then this x dot scatter plugging the
information in there we can leave our x
dot set as is and our plot.show as is
let's run it now and take a look at what
we get okay and so hopefully you can see
we get this interesting uh distribution
of points it actually looks very spread
out when I make it full screen
um but you can see on here they're
varying in colors and sizes because of
that randomizer function that we set up
um
but they're all over the map and uh if
you don't believe that it's random you
can just run it again and you'll get a
totally different graph the second time
okay so that obviously looks a lot
different than the second one
um now that is all I'm gonna say about
scattered plots for now again leave
comments uh below if you have them I'm
sure there are things that you might
wonder about that we haven't covered in
here but let's take a look at the third
and final for this tutorial which we're
gonna do is the bar graph so start the
bar graph section okay and so first
let's go ahead and make the data again
and I'm just gonna say uh let's make
this X from a half and then plus numpy
dot arrange uh range a range of eight
okay so it's just gonna give us a nice
ordered list going up from 0.5
now let's make our Y data random and
then we'll use that uniform again
excuse me
and we'll do two seven and length of X
just again to make sure that we are
getting
um two lists of the same size so uh
let's just make it clear here that what
we did in here was
um low high and then total size so our
values should be between two and seven
we're going to have a bar for each of
them of uh of uh we're gonna have eight
total bars is what I was trying to say
poorly there and now let's go ahead and
take a look at plot the bar graph so
that's all we're going to do for the bar
graph in terms of data is just to
arrange our X data nice and orderly and
then get some random y values um
obviously I'm not pulling from real data
sets here because I'm just showing you
how to make the graphs if you have any
questions how to apply your data set
over top of it throw a comment down
below and I'll get back to you as soon
as I can
all right but for a bar graph all we
need are the X and Y arrays of data and
then we'll establish an edge an edge
color and a line width just to show you
some of the stylistic things that you
can do okay and so instead of x dot
scatter or X Dot Plot we're going to go
with x dot bar and then X and Y and then
width of we'll just say one feel free to
play around with that if you want the
edge color I will make white and you'll
see that when we boot it up and then the
line width uh will be equal to one so
width is how wide you actually want the
bars to be and then line width is going
to be how many pixels wide you want the
edges around your bars to be okay so
those are really the only things that
you need to know I will say that Lim is
the axis range to show and that ticks is
where to start
and how many to show I have that comment
in my notes uh to put it down here but I
guess I never threw it down so basically
limit X Y Lim and xlim and ylim and I do
think I talked about it just didn't
leave a good comment is saying the range
our axis is going to show and then ticks
is uh is the range of
of yeah ticks that we're going to show
on The X and Y axis okay so let's boot
this up and take a look at what we just
created with a bar graph all right and
so uh hopefully what you can see is we
have eight total bars and the reason I
guess I didn't touch on this but the
reason we used X at 0.5 is because that
is the center point of each bar so if we
make them one wide centered on a half
and then we do eight of them it's going
to be centered on 0.5 1.5 2.5 3.5 but
it's going to go half of a bar in each
Direction so and then you can see our y
values are random
between uh two and seven so that seemed
to work pretty good hopefully you
understand how to basically replace my X
and Y lists of just sort of quickly
thrown together data with more useful
data that applies to your situation
um but so that is all I want to say
about this super quick crash course in
some of the most basic matplot lib
graphs
of course if you have any questions
about what you saw in this video or
would like to see some specific stuff on
the channel in the future just let me
know about in the comments below as
always thank you guys so much for
watching and until next time bye
