Matplotlib – Add Grid Lines

In this lesson, we will learn how to add grid lines to a line graph. For that, use the grid() function in Matplotlib.
Before moving further, we’ve prepared a video tutorial to learn to add grid lines to a figure in Matplotlib:

Let us see an example of adding grid lines to a line graph:

import pandas as pd
import matplotlib.pyplot as plt

# DataFrame with 3 columns
dataFrame = pd.DataFrame(
   {
      "Cricket_Bat": ['SG', 'BDM', 'SS', 'GM', 'Kookaburra', 'Spartan'],
      "MRP": [2000, 2200, 2400, 2700, 2800, 3000],
      "Weight_Grams": [1100, 1200, 1250, 1330, 1480, 1600]
   }
)

# Plot a line graph using the pyplot.plot() method
# The x and y coordinates are the columns of the DataFrame
plt.plot(dataFrame["MRP"], dataFrame["Weight_Grams"])

# Adding grid lines
plt.grid()

# Display the figure
plt.show()

Output

Add Grid Lines in a graph with Matplotlib


If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.


For Videos, Join Our YouTube Channel: Join Now


Read More:

Matplotlib - Plotting
Matplotlib - Add Labels to a Plot
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment