-
-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Description
Bug summary
I use major ticks and minor ticks and adopt them after zooming with "ax.callbacks.connect('xlim_changed', self._ax_scaled)". When zooming in very deep and then zooming out in one step with the "home" button of toolbar I got the message
"Locator attempting to generate 50003 ticks ([-0.2, ..., 10000.2]), which exceeds Locator.MAXTICKS (1000)."
and the computer need about 1 minute to create ticks.
To see the problem please zoom the xaxis with tool bar until you have reached of length of about 20. Then zoom out in one step with home button.
Code for reproduction
''' Show a problem in minorticks when zooming out with toolbar.
The problem apears when setting major ticks after a large zoomout with toolbar, but it is caused by minor ticks.
The problem is hard to catch if you don't now this.'''
# "Locator attempting to generate 50003 ticks ([-0.2, ..., 10000.2]), which exceeds Locator.MAXTICKS (1000)."
import matplotlib.pyplot as plt
from matplotlib import ticker
from matplotlib.ticker import MultipleLocator
from time import time
class Layout:
def __init__(self):
''' Usage of toolbar homebutton may cause the message
"Locator attempting to generate 50003 ticks ([-0.2, ..., 10000.2]), which exceeds Locator.MAXTICKS (1000)." and
generate thousand of objects (I assume minor ticks), needing a lot of time.
'''
fig, ax1 = plt.subplots()
ax1.set_title('Zoom in with Toolbar to about 4990...5010. Then use home-button.', color='red')
ax1.set( xlabel='Pointnr.', ylabel='Measure(x)', xlim=(0,10000), ylim=(-1,1) ) # No data today. Normally 30000 points with need of zooming
ax1.grid( axis='both')
self.fontsize = 12
ax1.callbacks.connect('xlim_changed', self._ax_scaled)
self._ax_scaled(ax1)
def _ax_scaled(self, ax):
''' Eventhandler to adopt ticks when zooming in and out.'''
start=time()
# ax.xaxis.set_minor_locator(MultipleLocator(10000)) # workaround with long minorticks. Use later when you have seen the problem.
xs, xe = ax.get_xlim() # Lenght of xaxis
majortick = int((xe-xs)/10)
print(f'\nSetting majortick fontsize: {majortick=} {self.fontsize=}')
if self.fontsize==8 and majortick > 995:
print('\nAt this point, what is the need to generate the minorticks for the whole xaxis ?')
print('Why can´t we generate the minorticks later ?')
self.fontsize = 12 if majortick > 7 else 8
for ticklbl in ax.get_xticklabels(): # set fontsize of major tick labels according to zoom
ticklbl.set(fontsize = self.fontsize)
print('Fontsize finished\n')
ax.xaxis.set_minor_locator(MultipleLocator(majortick/10)) # set minor ticks to tenth of major ticks
print(f'{time()-start=:4.1f}') # Homebutton zoom out may need several minutes, be patient
f = Layout()
plt.show()Actual outcome
"Locator attempting to generate 50003 ticks ([-0.2, ..., 10000.2]), which exceeds Locator.MAXTICKS (1000)."
and about one minute to wait until Python is back.
Expected outcome
No message, no wait time.
Additional information
Just start the programm
Operating system
windows 11
Matplotlib Version
3.10.1
Matplotlib Backend
tkagg
Python version
Python 3.13.7 (tags/v3.13.7:bcee1c3, Aug 14 2025, 14:15:11) [MSC v.1944 64 bit (AMD64)] on win32
Jupyter version
Installation
pip