I have too many ticks on my graph and they are running into each other.
How can I reduce the number of ticks?
For example, I have ticks:
1E-6, 1E-5, 1E-4, ... 1E6, 1E7
And I only want:
1E-5, 1E-3, ... 1E5, 1E7
I’ve tried playing with the LogLocator, but I haven’t been able to figure this out.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Alternatively, if you want to simply set the number of ticks while allowing matplotlib to position them (currently only with MaxNLocator), there is pyplot.locator_params,
pyplot.locator_params(nbins=4)
You can specify specific axis in this method as mentioned below, default is both:
# To specify the number of ticks on both or any single axes pyplot.locator_params(axis='y', nbins=6) pyplot.locator_params(axis='x', nbins=10)
Method 2
If somebody still gets this page in search results:
fig, ax = plt.subplots()
plt.plot(...)
every_nth = 4
for n, label in enumerate(ax.xaxis.get_ticklabels()):
if n % every_nth != 0:
label.set_visible(False)
Method 3
To solve the issue of customisation and appearance of the ticks, see the Tick Locators guide on the matplotlib website
ax.xaxis.set_major_locator(plt.MaxNLocator(3))
would set the total number of ticks in the x-axis to 3, and evenly distribute them across the axis.
There is also a nice tutorial about this
Method 4
There’s a set_ticks() function for axis objects.
Method 5
in case somebody still needs it, and since nothing
here really worked for me, i came up with a very
simple way that keeps the appearance of the
generated plot “as is” while fixing the number
of ticks to exactly N:
import numpy as np import matplotlib.pyplot as plt f, ax = plt.subplots() ax.plot(range(100)) ymin, ymax = ax.get_ylim() ax.set_yticks(np.round(np.linspace(ymin, ymax, N), 2))
Method 6
The solution @raphael gave is straightforward and quite helpful.
Still, the displayed tick labels will not be values sampled from the original distribution but from the indexes of the array returned by np.linspace(ymin, ymax, N).
To display N values evenly spaced from your original tick labels, use the set_yticklabels() method. Here is a snippet for the y axis, with integer labels:
import numpy as np import matplotlib.pyplot as plt ax = plt.gca() ymin, ymax = ax.get_ylim() custom_ticks = np.linspace(ymin, ymax, N, dtype=int) ax.set_yticks(custom_ticks) ax.set_yticklabels(custom_ticks)
Method 7
If you need one tick every N=3 ticks :
N = 3 # 1 tick every 3 xticks_pos, xticks_labels = plt.xticks() # get all axis ticks myticks = [j for i,j in enumerate(xticks_pos) if not i%N] # index of selected ticks newlabels = <span class="su-label su-label-type-default"></span>
or with fig,ax = plt.subplots() :
N = 3 # 1 tick every 3 xticks_pos = ax.get_xticks() xticks_labels = ax.get_xticklabels() myticks = [j for i,j in enumerate(xticks_pos) if not i%N] # index of selected ticks newlabels = <span class="su-label su-label-type-default"></span>
(obviously you can adjust the offset with (i+offset)%N).
Note that you can get uneven ticks if you wish, e.g. myticks = [1, 3, 8].
Then you can use
plt.gca().set_xticks(myticks) # set new X axis ticks
or if you want to replace labels as well
plt.xticks(myticks, newlabels) # set new X axis ticks and labels
Beware that axis limits must be set after the axis ticks.
Finally, you may wish to draw only an arbitrary set of ticks :
mylabels = ['03/2018', '09/2019', '10/2020'] plt.draw() # needed to populate xticks with actual labels xticks_pos, xticks_labels = plt.xticks() # get all axis ticks myticks = [i for i,j in enumerate(b) if j.get_text() in mylabels] plt.xticks(myticks, mylabels)
(assuming mylabels is ordered ; if it is not, then sort myticks and reorder it).
Method 8
xticks function auto iterates with range function
start_number = 0
end_number = len(data you have)
step_number = how many skips to make from strat to end
rotation = 90 degrees tilt will help with long ticks
plt.xticks(range(start_number,end_number,step_number),rotation=90)
Method 9
When a log scale is used the number of major ticks can be fixed with the following command
import matplotlib.pyplot as plt .... plt.locator_params(numticks=12) plt.show()
The value set to numticks determines the number of axis ticks to be displayed.
Credits to @bgamari’s post for introducing the locator_params() function, but the nticks parameter throws an error when a log scale is used.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0