Matplotlib / Matlab label tick at min / max value
For numerical plots, it can be important to label the ticks of axes extrema (minimum or maximum). For example, to clearly show the edges of simulated data axes. This can be easily done in Python Matplotlib or in Matlab. This assumes the typical case that the axes values are numeric.
In this example, the y-axis ticks show the endpoints of the y-data range: -3.375 and 27.0.
The data are in general non-monotonic, so we sort ticks before resetting them.
Python: Matplotlib does not require sorting the ticks.
from matplotlib.pyplot import figure, show
import numpy as np
# synthetic data
x = np.arange(-1.5, 3.25, 0.25)
y = x**3
fg = figure()
ax = fg.gca()
ax.plot(x, y)
# label min and max ticks
yticks = np.append(ax.get_yticks(), [y.min(), y.max()])
ax.set_yticks(yticks)
show()
Matlab: requires sorting the ticks before resetting them.
% synthetic data
x = -1.5:0.25:3.0;
y = x.^3;
fg = figure;
ax = axes(fg);
plot(ax, x, y)
% label min and max ticks
yticks = sort([ax.YTick, min(y), max(y)]);
ax.YTick = yticks;