How can I plot only horizontal gridlines in Matplotlib? 2

How can I plot only horizontal gridlines in Matplotlib?

The process of plotting only horizontal gridlines in Matplotlib involves using the “grid” function and setting the “axis” parameter to “y” to show only horizontal gridlines. This can be done by first importing the Matplotlib library and then specifying the desired plot using the “plt.plot()” function. The “grid()” function is then used to display the gridlines, with the “axis” parameter set to “y”. This will result in only horizontal gridlines being shown on the plot. Furthermore, the appearance of the gridlines can be customized by using additional parameters such as “color” and “linestyle”. This method allows for a more focused and clear representation of data on the plot.

Plot Only Horizontal Gridlines in Matplotlib


You can use the following basic syntax to only plot horizontal gridlines in Matplotlib:

ax.grid(axis='y')

The following example shows how to use this syntax in practice.

Example: Plot Only Horizontal Gridlines in Matplotlib

The following code shows how to create a bar plot in Matplotlib with only horizontal gridlines shown in the plot:

import pandas as pd
import matplotlib.pyplot as plt

#create DataFrame
df = pd.DataFrame({'team':['Mavs', 'Nets', 'Spurs', 'Warriors'],
                   'points':[105, 99, 112, 100]})

#define plot
fig, ax = plt.subplots()

#create bar plot
df.plot(kind='bar', ax=ax)

#add horizontal gridlines
ax.grid(axis='y')

#display plot
plt.show()

Feel free to use ax.set_axisbelow(True) to display the horizontal gridlines behind the bars in the plot:

import pandas as pd
import matplotlib.pyplot as plt

#create DataFrame
df = pd.DataFrame({'team':['Mavs', 'Nets', 'Spurs', 'Warriors'],
                   'points':[105, 99, 112, 100]})

#define plot
fig, ax = plt.subplots()

#create bar plot
df.plot(kind='bar', ax=ax)

#add horizontal gridlines behind bars in the plot
ax.set_axisbelow(True)
ax.grid(axis='y')

#display plot
plt.show()

Matplotlib horizontal gridlines

And feel free to use the color, linestyle, and linewidth arguments within the grid() function to customize the appearance of the gridlines:

import pandas as pd
import matplotlib.pyplot as plt

#create DataFrame
df = pd.DataFrame({'team':['Mavs', 'Nets', 'Spurs', 'Warriors'],
                   'points':[105, 99, 112, 100]})

#define plot
fig, ax = plt.subplots()

#create bar plot
df.plot(kind='bar', ax=ax)

#add horizontal gridlines with custom appearance
ax.set_axisbelow(True)
ax.grid(axis='y', color='red', linestyle='dashed', linewidth=3)

#display plot
plt.show()

You can find a complete list of ways to customize the gridlines in the Matplotlib documentation.

The following tutorials explain how to perform other common tasks in Matplotlib:

How to Remove Ticks from Matplotlib Plots
How to Change Font Sizes on a Matplotlib Plot

Cite this article

stats writer (2024). How can I plot only horizontal gridlines in Matplotlib?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-plot-only-horizontal-gridlines-in-matplotlib/

stats writer. "How can I plot only horizontal gridlines in Matplotlib?." PSYCHOLOGICAL SCALES, 27 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-plot-only-horizontal-gridlines-in-matplotlib/.

stats writer. "How can I plot only horizontal gridlines in Matplotlib?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-plot-only-horizontal-gridlines-in-matplotlib/.

stats writer (2024) 'How can I plot only horizontal gridlines in Matplotlib?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-plot-only-horizontal-gridlines-in-matplotlib/.

[1] stats writer, "How can I plot only horizontal gridlines in Matplotlib?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I plot only horizontal gridlines in Matplotlib?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top