["python","numpy","matplotlib"]
Does matplotlib have a function for drawing diagonal lines in axis coordinates?

Matplotlib Axes have the functions axhline and axvline for drawing horizontal or vertical lines at a given y or x coordinate (respectively) independently of the data scale on an Axes. Is there a similar function for plotting a constant diagonal? For example, if I have a scatterplot of variables with a similar domain, it is often useful to know whether they fall above or below the line of y = x: mean, cov = [0, 0], [(1, .6), (.6, 1)] x, y = np.random.multivariate_normal(mean, cov, 100).T y += x + 1 f, ax = plt.subplots(figsize=(6, 6)) ax.scatter(x, y, c=".3") ax.plot([-3, 3], [-3, 3], ls="--", c=".3") ax.set(xlim=(-3, 3), ylim=(-3, 3)) This can of course be done programmatically by grabbing the axis limits, (ax.get_xlim(), etc.), but that a) takes a few extra steps and b) is brittle in cases where more data might end up on the plot and shift the limits. (Actually in some cases just adding the constant line itself stretches the axes). It would be preferable to just do, e.g., ax.axdline(ls="--", c=".3"), but it's not clear if something like this exists in the matplotlib codebase. All you would need to do would be modify the axhline code to plot from [0, 1] in axes coordinates for both x and y, I think.
Starting from matplotlib 3.3.0, it will: https://matplotlib.org/3.3.0/api/_as_gen/matplotlib.axes.Axes.axline.html Axes.axline(self, xy1, xy2=None, *, slope=None, **kwargs) Add an infinitely long straight line. The line can be defined either by two points xy1 and xy2, or by one point xy1 and a slope. This draws a straight line "on the screen", regardless of the x and y scales, and is thus also suitable for drawing exponential decays in semilog plots, power laws in loglog plots, etc. However, slope should only be used with linear scales; It has no clear meaning for all other scales, and thus the behavior is undefined. Please specify the line using the points xy1, xy2 for non-linear scales.