I want some space before and after the last tick location on an axis. I would just use axis.set_xlim() for example but this interferes with my (custom) locator and reruns the tick generation. I found and overwritten the view_limits() method of the locator-classes but they don't seem to be called automatically and when called manually they don't have any impact on the resulting plot. I searched the docs and the source but haven't come up with a solution. Am I missing something?
For the greater picture I want to have a locator which gives me some space before and after the ticks and chooses tick points which are a multiple of 'base' like the MultipleLocator but scale the base automatically if the number of ticks exceeds a specified value. If there is another way to achieve this without subclassing a locator I am all ears :).
Here is my example code for the subclassed locator with overwritten view_limits-method:
import matplotlib.pyplot as plt import numpy as np from matplotlib.ticker import MaxNLocator class MyLocator(MaxNLocator): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) def view_limits(self, dmin, dmax): bins = self.bin_boundaries(dmin, dmax) step = bins[1] - bins[0] result = np.array([bins[0] - step, bins[-1] + step]) print(result) return result a = 10.0 b = 99.0 t = np.arange(a, b, 0.1) s = np.sin(0.1*np.pi*t)*np.exp(-t*0.01) loc = MyLocator(9) fig, ax = plt.subplots() plt.plot(t, s) ax.xaxis.set_major_locator(loc) loc.autoscale() # results in [ 0. 110.] but doesnt change the plot plt.show()