3

How to plot a chart with minor grid of 1, major grid of 10, and with xticklabels increment 20 units?

Here is my sample code and output with xticklabels increment every 10 units:

plt.figure(figsize=(10, 10))
ax = plt.gca()
major_ticks = np.arange(0, 60, 10)    
minor_ticks = np.arange(0, 60, 1)
ax.set_xticks(major_ticks)
ax.set_xticks(minor_ticks, minor=True)
ax.set_yticks(major_ticks)
ax.set_yticks(minor_ticks, minor=True)
ax.grid(which='major')
ax.grid(which='minor', alpha=0.5)
ax.set_aspect('equal')

enter image description here

But then I wanted to display the xticklabels and yticklabels with the increment of 20 instead of 10, as shown here like this:

enter image description here

Any idea how to accomplish this? Thanks

Scoodood
  • 583
  • 1
  • 5
  • 13
  • [Here](https://stackoverflow.com/questions/24943991/change-grid-interval-and-specify-tick-labels-in-matplotlib/24953575) you will see how to use major and minor ticks – Sheldore Dec 28 '18 at 16:29
  • Hi @Bazingaa, thanks for your reply, but the link that you provide did not solve this particular problem. – Scoodood Dec 28 '18 at 22:38
  • It was not a direct answer but I gave you the links which could guide you in the right direction. Anyway, I answered now. Have a look at it below. – Sheldore Dec 28 '18 at 23:08
  • Thanks @Bazingaa! Great solution. Btw, do you know how to turn of all tick marks except those the location that has the xticklabels turn on? – Scoodood Dec 28 '18 at 23:48
  • Check my edited answer – Sheldore Dec 29 '18 at 00:07
  • hi Bazingaa, all answers given so far are good answers that solve my problem.After reading your message, I now realize that I cannot accept 2 answers. Really, sorry about that. I am going to pick the best answer then. Thanks. – Scoodood Jan 03 '19 at 01:44

2 Answers2

4

Just add the following 4 lines to the end of your code: You just have to hide every second major tick label. That's pretty much it to get what you want. [1::2] indexing means start from the second index and take every second element from there. I have to start from the second index because the tick label at the first index is 0 which you do not want to remove.

EDIT: IF you just want the major ticks at the locations where you have tick labels, you can do the following modifications (marked by an arrow <---). Yo might find the official docs helpful.

Plot without the major ticks but with minor ticks

# Hiding for x-axis
for t in ax.xaxis.get_major_ticks()[1::2]:
    t.label.set_visible(False)
    t.tick1On = False # <----

# Hiding for y-axis
for t in ax.yaxis.get_major_ticks()[1::2]:
    t.label.set_visible(False)    
    t.tick1On = False # <----

enter image description here

Plot without the minor and major ticks

If you also want to hide the minor ticks, you can do the following in addition to the above code

for t in ax.xaxis.get_minor_ticks():
    t.tick1On = False
    # t._apply_params(width=0) # suggested by Jayjayyy

for t in ax.yaxis.get_minor_ticks():
    t.tick1On = False     
    # t._apply_params(width=0) # suggested by Jayjayyy

The direct way avoiding all for loops is as suggested by Jayjayyy

ax.tick_params(axis='both', which='minor', width=0)   

enter image description here

Sheldore
  • 37,862
  • 7
  • 57
  • 71
  • Thanks for the appreciation. I had to look it up in my laptop for my work related notebooks where I used similar things a long time ago ;) But sometimes I just discover new options by using a tab completion after typing `ax.` – Sheldore Dec 28 '18 at 23:40
  • @Jayjayyy: I added your suggestion. Thanks – Sheldore Dec 29 '18 at 00:17
  • I haven’t used it so far. I just added jayjay suggestion – Sheldore Dec 29 '18 at 01:01
1

Update:

That looks maybe a little bit nicer:

ax.set_xticklabels(['' if i % 2 else l for i, l in enumerate(ax.get_xticks())])
ax.set_yticklabels(['' if i % 2 else l for i, l in enumerate(ax.get_yticks())])

I think you'll have to manually set every second label to '':

import matplotlib.pyplot as plt
import numpy as np

plt.figure(figsize=(10, 10))
ax = plt.gca()
major_ticks = np.arange(0, 60, 10)    
minor_ticks = np.arange(0, 60, 1)
ax.set_xticks(major_ticks)
ax.set_xticks(minor_ticks, minor=True)
ax.set_yticks(major_ticks)
ax.set_yticks(minor_ticks, minor=True)
ax.grid(which='major')
ax.grid(which='minor', alpha=0.5)
ax.set_aspect('equal')

xticks = ax.get_xticks().tolist()
for i in range(1, len(xticks), 2):
    xticks[i] = ''
ax.set_xticklabels(xticks)

yticks = ax.get_yticks().tolist()
for i in range(1, len(yticks), 2):
    yticks[i] = ''
ax.set_yticklabels(yticks)

plt.show()

And plots:

plot

finefoot
  • 9,914
  • 7
  • 59
  • 102