Python Matplotlib Adding Grids & Labels

In this Python Matplotlib article we are going to learn about Matplotlib Adding Grids & Labels, also you can read my previous articles in Python Matplotlib in the below links.

 

Adding Grid

there are situations where having a  reference system would improve the comprehension of the plot—for example with multiline plots. we can add a grid to the plot by calling the grid() function, it takes  one parameter, a Boolean value, to enable (if True) or disable (if False) the grid.

 

 

 

So this is the code for creating grid in Matplotlib, basically we are going to create Grid in a line plot.

 

 

 

 

If you run the code, this will be the result

Python Matplotlib Adding Grids & Labels
Python Matplotlib Adding Grids & Labels

 

 

 

 

Adding Labels

Now we are going to learn how to add labels in Matplotlib. you can see that in the following code we have created two labels for x and y axes.

 

 

 

 

Now if you run the code this will be the result.

Matplotlib Adding Labels
Matplotlib Adding Labels

 

 

 

Title and Legends 

So now we are going to learn how you can create Title and Legends for your matplotlib. you can use plt.title() for giving title.

 

 

 

If you run the code this will be the result, and you can see that we have a title at the top.

Matplotlib Title
Matplotlib Title

 

 

 

 

So now let’s create the legend, Legends are used to explain what each line means in the current figure. Let’s take the multi line plot example .

 

We added an extra keyword argument, label, to the plot() call. This keyword argument provides the information required to compose the text of the legend. Since keyword arguments must come after non-keyword arguments, we have to plot each line in separate plot() calls that include the label argument. Now, calling the legend() function with no argument will show that information  in the legend box.

 

Now if you run the code, you can see the legends in the plot.

Python Matplotlib Adding Grids & Labels
Python Matplotlib Adding Grids & Labels

 

 

 

Matplotlib Tutorials For Beginners

1: Matplotlib Introduction & Installation 

2: Matplotlib Plotting Multiple Curves

3: Matplotlib Plotting Scatter (Points)

4: Matplotlib Plotting BarChart

5: Matplotlib Plotting PieChart

6: Matplotlib Plotting Histogram 

7: Matplotlib Plotting BoxPlot

Subscribe and Get Free Video Courses & Articles in your Email

 

Codeloop
Share via
Copy link
Powered by Social Snap
×