Muhammad
Muhammad

Reputation: 410

Grid in matplotlib

In matplotlib, how can I make the plot to show more gird lines. It is very wide. enter image description here

Upvotes: 0

Views: 138

Answers (1)

You can turn on minor ticks and then specify a "minor" grid for these ticks:

from matplotlib import pyplot
import matplotlib
import numpy as np

t = np.arange(0.0, 2.0, 0.01)
s = np.sin(2 * np.pi * t)

fig, ax = pyplot.subplots()
ax.plot(t, s)

# Major ticks -- these are the ones that will show up on the plot
ax.set_xticks([0.25, 0.75, 1.25, 1.75])
ax.grid()

# Turn on minor ticks, this turns on minor grid
ax.minorticks_on()

# Change style of major and minor grids
# Major grid style
ax.grid(which='major', linestyle='-', linewidth='1.', color='red')

# Minor grid style
ax.grid(which='minor', linestyle=':', linewidth='0.5', color='black')

pyplot.show()

Result:

enter image description here

Upvotes: 1

Related Questions