Skip to content

Commit

Permalink
DOC: Improve inverted axis example
Browse files Browse the repository at this point in the history
  • Loading branch information
timhoffm committed Apr 12, 2024
1 parent d45dfbb commit 1bb41ea
Showing 1 changed file with 25 additions and 14 deletions.
39 changes: 25 additions & 14 deletions galleries/examples/subplots_axes_and_figures/invert_axes.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 1bb41ea

Please sign in to comment.