Python matplotlib shows only horizontal major and vertical minor grid lines

I want to build the main grid lines of the y axis (horizontal grid lines), but I do not want to build the vertical main grid lines (x axis). Instead, I want to build vertical small grid lines.

How can i do this?

The code ax.grid(which='major', linewidth=0)hides both the vertical and horizontal main grid lines ...

Thank!

+4
source share
1 answer

Grid properties can be set independently ax.xaxis.grid()and ax.yaxis.grid().
To activate small grid lines, you first need to specify a locator for them.

import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator

fig, ax = plt.subplots(figsize=(5,3))

ax.yaxis.grid(which="major", color='r', linestyle='-', linewidth=2)

ml = MultipleLocator(0.02)
ax.xaxis.set_minor_locator(ml)
ax.xaxis.grid(which="minor", color='k', linestyle='-.', linewidth=0.7)

plt.show()

enter image description here

+6
source

Source: https://habr.com/ru/post/1665728/


All Articles