user1551817
user1551817

Reputation: 7451

remove x ticks but keep grid lines

I have a Pyplot plot, which I want to add gridlines to. I did this using:

plt.grid(True)

I then removed my x ticks using:

ax1.xaxis.set_visible(False)

My x ticks were removed, but so were the x grid lines. I would like them to stay.

Is there a way I can do this please?

Upvotes: 13

Views: 8342

Answers (2)

tacaswell
tacaswell

Reputation: 87376

from matplotlib.ticker import NullFormatter
ax.xaxis.set_major_formatter(NullFormatter())

Upvotes: 8

Lucas Ribeiro
Lucas Ribeiro

Reputation: 6282

Try this:

plt.grid(True)
ax.xaxis.set_ticklabels([])

It should work. The grid will be intact, but there won't be any tick labels. If you don't want the ticks too, add:

ax.xaxis.set_ticks_position('none')

Upvotes: 20

Related Questions