How to make a Bar chart with Python Matplotlib
32K views
Mar 28, 2023
Here is how to make a bar chart using matplotlib in Python.
View Video Transcript
0:00
Here is out to make a bar chart in Python
0:02
Firstly, just make sure that Python is installed on your machine and also make sure that
0:07
you have Matt Plotlib installed. Once you have that you can open this code like I have done here
0:12
Then here we're just going to make a new file. Then we'll just call it bar.p.i
0:17
So you can call it whatever you want because we're making a bad chart I'm calling it
0:21
bar. Enter then we have that file. Now inside this file we can light our Python code
0:27
So I said we need to have Matt Plotlib installed because that's what we're going to use to make our bar chart. To make a batch art, we're just going to
0:33
import matplot lib, we'll say import. Then in mat plot lib we have a modicode pi plot. Then we're
0:40
going to import this as PLT. So PLT is more of a standard with this, this community, but you can
0:46
call this whatever you want. If you want, you can just say mat plot or you can just say plot or
0:51
whatever you can call it. I'm just calling it PLT. So we just want to plot a bar chart with
0:56
sales and demands. So these will be the cells we have made in the first four months of the year. On our x-axis, which I'll call x, we'll have an all
1:04
allay that will have our data. So here we'll have the months like January, February, March
1:11
and April. So that's our data for the months. And for the sales, they're going to sit on the
1:16
y-axis. So I'll just call it y. Again, it will be an allay of sales. So we have our data
1:22
This data is going to sit on the x-axis and this data will sit on the y-axis. So once we have
1:27
this, we can now plot our bar. Now we can use this. module that we have here so say p o t and on this module there's a function code bar that
1:35
will help us to make a bar chart so you open the blanket and this bar that we want to plot is x
1:41
i can say x comma y now we can show our bar to see how it looks like you can just say p o t again
1:48
again on this module there a function code show and once you do that you can just save the file then here you see this button If you have configured everything correct you can just press this button and Python will run this file So let do that
2:02
Boom! It didn't do much of the work, but you can see already it's looking okay
2:07
But when you look at this, we don't know what these are. We just see the amans
2:11
We don't know what these values are. So it would be nice if we put a little title here and even there
2:16
and even the entire title for this one. So to be able to do that, we're going to go back
2:21
So again we'll say PLT. So we'll find several functions that will help us do that. So one of them is x label
2:28
which will help us to label the x-axis. So here I'll just say month
2:32
Equally, I'll do the same for the y-axis, y label, and I'll call these cells
2:38
Then finally, we have another one which is called title. So this will be the title for the entire chart bar
2:44
So we're just going to say sales. Once you do that, you can save the file, then we can run this file
2:51
once again click on this one. Boom! We have the title here and also there you can see the label and even the label there
3:00
Now whenever someone looks at this they're able to tell that these are sales and these are the first four months of the year
3:07
Now we can even go further to change these colors here so that January should have its own color all the way up to April
3:14
So what we can do is just here, define another value upon which colors
3:19
So it will be an array of colors like so. Once you have defined your colors, then you can add more parameters to the bat-chat
3:27
which will say color equals colors. So whenever we are looking for the color, we're going to get these colors that we have defined here
3:35
Just set the file, run it again. You have different colors for each of these and there are a lot more customizations that you
3:43
can make to this graph. But for now, just end it here
3:47
Thank you so much for watching. I'll see you in the next one