Share and unshare axes after creation. by lkjell · Pull Request #9923 · matplotlib/matplotlib · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions doc/api/next_api_changes/2017-12-06-KL.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Remove share through `matplotlib.Axes.get_shared_{x,y,z}_axes`
--------------------------------------------------------------

Previously when different axes are created with different parent/master axes,
the share would still be symmetric and transitive if an unconventional
method through `matplotlib.Axes.get_shared_x_axes`
is used to share the axes after creation. With the new sharing mechanism
this is no longer possible.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Share and unshare `axes` after creation
---------------------------------------

`matplotlib.axes.Axes` have `matplotlib.axes.Axes.unshare_x_axes`,
`matplotlib.axes.Axes.unshare_y_axes`, `matplotlib.axes.Axes.unshare_z_axes`
and `matplotlib.axes.Axes.unshare_axes` methods to unshare axes.
Similiar there are `matplotlib.axes.Axes.share_x_axes`,
`matplotlib.axes.Axes.share_y_axes`, `matplotlib.axes.Axes.share_z_axes` and
`matplotlib.axes.Axes.share_axes` methods to share axes.

Unshare an axis will decouple the viewlimits for further changes.
Share an axis will couple the viewlimits.
39 changes: 39 additions & 0 deletions examples/mplot3d/share_unshare_3d_axes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""
============================================
Parametric Curve with Share and Unshare Axes
============================================

This example demonstrates plotting a parametric curve in 3D,
and how to share and unshare 3D plot axes.
"""
import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt

mpl.rcParams['legend.fontsize'] = 10

# Prepare arrays x, y, z
theta = np.linspace(-4 * np.pi, 4 * np.pi, 100)
z = np.linspace(-2, 2, 100)
r = z ** 2 + 1
x = r * np.sin(theta)
y = r * np.cos(theta)

fig = plt.figure()
ax = fig.add_subplot(311, projection='3d')

ax.plot(x, y, z, label='parametric curve')
ax.legend()

ax1 = fig.add_subplot(312)
ax1.plot(range(10))
ax1.share_axes(ax)

ax2 = fig.add_subplot(313, projection='3d', sharex=ax)
ax2.plot(x, y, z)

ax2.unshare_x_axes()
ax2.share_z_axes(ax)

plt.show()
36 changes: 36 additions & 0 deletions examples/subplots_axes_and_figures/unshare_axis_demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""
======================
Unshare and share axis
======================

The example shows how to share and unshare axes after they are created.
"""

import matplotlib.pyplot as plt
import numpy as np

t = np.arange(0.01, 5.0, 0.01)
s1 = np.sin(2 * np.pi * t)
s2 = np.exp(-t)
s3 = np.sin(4 * np.pi * t)

ax1 = plt.subplot(311)
plt.plot(t, s1)

ax2 = plt.subplot(312)
plt.plot(t, s2)

ax3 = plt.subplot(313)
plt.plot(t, s3)

ax1.share_x_axes(ax2)
ax1.share_y_axes(ax2)

# Share both axes.
ax3.share_axes(ax1)
plt.xlim(0.01, 5.0)

ax3.unshare_y_axes()
ax2.unshare_x_axes()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't try to run this, but its hard to imagine this actually demos anything in a non-interactive session.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You do not create share as usual by setting a share parent axes. Sharing is set after the creation of the axes.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think demos should be readily readable by users since they are one of the primary ways we document Matplotlib. I find this demo is very cryptic. Why are you un-sharing? Just to show that these functions exist? What effect does this have on the example? The description of the demo should state the usual way of sharing, and this alternate way.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm... no comment.


plt.show()
145 changes: 119 additions & 26 deletions lib/matplotlib/axes/_base.py
Loading