Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2a62854

Browse files
committed
Basic Usage tutorial: add additional axes section
Secondary and twin axes are rather important (although not necessarily very basic) so they should be shortly mentioned with links to the API docs where more detailed examples are linked (unfortunately there's no tutorial on secondary and twin axes to refer to).
1 parent ad2c869 commit 2a62854

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

tutorials/introductory/usage.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,8 @@ def my_plotter(ax, data1, data2, param_dict):
367367
#
368368
# Each Axes has two (or three) `~.axis.Axis` objects representing the x- and
369369
# y-axis. These control the *scale* of the Axis, the tick *locators* and the
370-
# tick *formatters*.
370+
# tick *formatters*. Additional Axes can be attached to display further Axis
371+
# objects.
371372
#
372373
# Scales
373374
# ------
@@ -449,6 +450,32 @@ def my_plotter(ax, data1, data2, param_dict):
449450
# numbers or dates. If you pass 1000 strings, Matplotlib will think you
450451
# meant 1000 categories and will add 1000 ticks to your plot!
451452
#
453+
#
454+
# Additional Axis objects
455+
# ------------------------
456+
#
457+
# Plotting data of different magnitude in one chart may require
458+
# an additional y-axis. Such an Axis can be created by using
459+
# `~.Axes.twinx` to add a new Axes with an invisible x-axis and a y-axis
460+
# positioned at the right (analogously for `~.Axes.twiny`).
461+
#
462+
# Similarly, you can add a `~.Axes.secondary_xaxis` or
463+
# `~.Axes.secondary_yaxis` having a different scale than the main Axis to
464+
# represent the data in different scales or units.
465+
466+
467+
fig, (ax1, ax3) = plt.subplots(1, 2, figsize=(8, 2.7), constrained_layout=True)
468+
l1, = ax1.plot(t, s)
469+
ax2 = ax1.twinx()
470+
l2, = ax2.plot(t, range(len(t)), 'C1')
471+
ax2.legend([l1, l2], ['Sine (left)', 'Straight (right)'])
472+
473+
ax3.plot(t, s)
474+
ax3.set_xlabel('Angle [°]')
475+
ax4 = ax3.secondary_xaxis('top', functions=(np.rad2deg, np.deg2rad))
476+
ax4.set_xlabel('Angle [rad]')
477+
478+
##############################################################################
452479
# Color mapped data
453480
# =================
454481
#

0 commit comments

Comments
 (0)