Stücke
Stücke

Reputation: 1043

Grid lines in between data points

Is there a way to move the grid lines in between the data points instead of on the data points?

import matplotlib.pyplot as plt
import numpy as np

# Fixing random state for reproducibility
np.random.seed(19680801)


plt.rcdefaults()
fig, ax = plt.subplots()

# Example data
people = ('Tom', 'Dick', 'Harry', 'Slim', 'Jim')
y_pos = np.arange(len(people))
performance = 3 + 10 * np.random.rand(len(people))

ax.barh(y_pos, performance, align='center')
ax.set_yticks(y_pos)
ax.set_yticklabels(people)
ax.invert_yaxis()  # labels read top-to-bottom

plt.grid(color='black')

plt.show()

enter image description here

Upvotes: 0

Views: 264

Answers (1)

JohanC
JohanC

Reputation: 80289

You could use the minor ticks for the grid. Here is some example code to get you started:

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

# Fixing random state for reproducibility
np.random.seed(19680801)

plt.rcdefaults()
fig, ax = plt.subplots()

# Example data
people = ('Tom', 'Dick', 'Harry', 'Slim', 'Jim')
y_pos = np.arange(len(people))
performance = 3 + 10 * np.random.rand(len(people))

ax.barh(y_pos, performance, align='center')
ax.set_yticks(y_pos)
ax.set_yticklabels(people)
ax.yaxis.set_minor_locator(MultipleLocator(0.5))
ax.set_ylim(-0.5, len(y_pos) - 0.5) # default there is too much padding which doesn't look nice
ax.invert_yaxis()  # labels read top-to-bottom

ax.grid(color='black', axis='x', which='major')
ax.grid(color='black', axis='y', which='minor')

plt.show()

example plot

Upvotes: 1

Related Questions