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?
To remove the ticks on the x-axis, tick_params() method accepts an attribute named bottom, and we can set its value to False and pass it as a parameter inside the tick_params() function. It removes the tick on the x-axis.
You can set the ticks using ax. set_xticks() . Here you can slice the x list to set a ticks at every 2nd entry using the slice notation [::2] . Then set the x tick labels using ax.
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With