Matplotlib computes on first creation of a plot an optimal number of ticks and labels, so that the label strings do not overdraw another tick label. Is it possible to optimally modify this number of ticks on a resize event? If the figure size is increased then there may be an opportunity to have more ticks and labels. If the figure size is decreased then the number of ticks should be decreased whenever they overdraw other labels.
1 Answer
You could use the resize_event to change the number of ticks. This example uses MaxNLocator but you can use a different Locator. It makes the number of ticks grow as the width of the window grows:
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(np.random.rand(100), 'o')
def onresize(event):
bbox = fig.get_window_extent().transformed(fig.dpi_scale_trans.inverted())
width, height = bbox.width * fig.dpi, bbox.height * fig.dpi
tick_step = 100
n = width / tick_step
ax.xaxis.set_major_locator(ticker.MaxNLocator(n))
fig.canvas.mpl_connect('resize_event', onresize)
plt.show()