From 1bb41ea071beefbf8a7d701cf56e58ba372f78a0 Mon Sep 17 00:00:00 2001 From: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Date: Fri, 12 Apr 2024 10:44:38 +0200 Subject: [PATCH] DOC: Improve inverted axis example Closes #28050. --- .../subplots_axes_and_figures/invert_axes.py | 39 ++++++++++++------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/galleries/examples/subplots_axes_and_figures/invert_axes.py b/galleries/examples/subplots_axes_and_figures/invert_axes.py index 15ec55d430bd..9779213cf3b6 100644 --- a/galleries/examples/subplots_axes_and_figures/invert_axes.py +++ b/galleries/examples/subplots_axes_and_figures/invert_axes.py @@ -1,25 +1,36 @@ """ -=========== -Invert Axes -=========== +============= +Inverted axis +============= -You can use decreasing axes by flipping the normal order of the axis -limits +This example demonstrates two ways to invert the direction of an axis: + +- If you want to set *explicit axis limits* anyway, e.g. via `~.Axes.set_xlim`, you + can swap the limit values: ``set_xlim(4, 0)`` instead of ``set_xlim(0, 4)``. +- Use `.Axis.set_inverted` if you only want to invert the axis *without modifying + the limits*, i.e. keep existing limits or existing autoscaling behavior. """ import matplotlib.pyplot as plt import numpy as np -t = np.arange(0.01, 5.0, 0.01) -s = np.exp(-t) +x = np.arange(0.01, 4.0, 0.01) +y = np.exp(-x) + +fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(6.4, 4), layout="constrained") +fig.suptitle('Inverted axis with ...') -fig, ax = plt.subplots() +ax1.plot(x, y) +ax1.set_xlim(4, 0) # inverted fixed limits +ax1.set_title('fixed limits: set_xlim(4, 0)') +ax1.set_xlabel('decreasing x ⟶') +ax1.grid(True) -ax.plot(t, s) -ax.set_xlim(5, 0) # decreasing time -ax.set_xlabel('decreasing time (s)') -ax.set_ylabel('voltage (mV)') -ax.set_title('Should be growing...') -ax.grid(True) +ax2.plot(x, y) +ax2.xaxis.set_inverted(True) # inverted axis with autoscaling +ax2.set_title('autoscaling: set_inverted(True)') +ax2.set_xlabel('decreasing x ⟶') +ax2.tick_params(axis='x', colors='darkorange') +ax2.grid(True) plt.show()