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

Skip to content

Commit 1909fef

Browse files
committed
Merge pull request #1972 from mdboom/twin-spine-fix
SubplotBase._make_twin_axes always creates a new subplot instance
2 parents 1cf4f5e + 14d4327 commit 1909fef

3 files changed

Lines changed: 55 additions & 1 deletion

File tree

lib/matplotlib/axes.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9062,7 +9062,14 @@ def _make_twin_axes(self, *kl, **kwargs):
90629062
"""
90639063
make a twinx axes of self. This is used for twinx and twiny.
90649064
"""
9065-
ax2 = self.figure.add_subplot(self.get_subplotspec(), *kl, **kwargs)
9065+
from matplotlib.projections import process_projection_requirements
9066+
kl = (self.get_subplotspec(),) + kl
9067+
projection_class, kwargs, key = process_projection_requirements(
9068+
self.figure, *kl, **kwargs)
9069+
9070+
ax2 = subplot_class_factory(projection_class)(self.figure,
9071+
*kl, **kwargs)
9072+
self.figure.add_subplot(ax2)
90669073
return ax2
90679074

90689075

10.1 KB
Loading

lib/matplotlib/tests/test_axes.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,3 +1108,50 @@ def test_mixed_collection():
11081108

11091109
ax.set_xlim(0, 16)
11101110
ax.set_ylim(0, 16)
1111+
1112+
1113+
@image_comparison(baseline_images=['twin_spines'], remove_text=True,
1114+
extensions=['png'])
1115+
def test_twin_spines():
1116+
1117+
def make_patch_spines_invisible(ax):
1118+
ax.set_frame_on(True)
1119+
ax.patch.set_visible(False)
1120+
for sp in ax.spines.itervalues():
1121+
sp.set_visible(False)
1122+
1123+
fig = plt.figure(figsize=(4, 3))
1124+
fig.subplots_adjust(right=0.75)
1125+
1126+
host = fig.add_subplot(111)
1127+
par1 = host.twinx()
1128+
par2 = host.twinx()
1129+
1130+
# Offset the right spine of par2. The ticks and label have already been
1131+
# placed on the right by twinx above.
1132+
par2.spines["right"].set_position(("axes", 1.2))
1133+
# Having been created by twinx, par2 has its frame off, so the line of its
1134+
# detached spine is invisible. First, activate the frame but make the patch
1135+
# and spines invisible.
1136+
make_patch_spines_invisible(par2)
1137+
# Second, show the right spine.
1138+
par2.spines["right"].set_visible(True)
1139+
1140+
p1, = host.plot([0, 1, 2], [0, 1, 2], "b-")
1141+
p2, = par1.plot([0, 1, 2], [0, 3, 2], "r-")
1142+
p3, = par2.plot([0, 1, 2], [50, 30, 15], "g-")
1143+
1144+
host.set_xlim(0, 2)
1145+
host.set_ylim(0, 2)
1146+
par1.set_ylim(0, 4)
1147+
par2.set_ylim(1, 65)
1148+
1149+
host.yaxis.label.set_color(p1.get_color())
1150+
par1.yaxis.label.set_color(p2.get_color())
1151+
par2.yaxis.label.set_color(p3.get_color())
1152+
1153+
tkw = dict(size=4, width=1.5)
1154+
host.tick_params(axis='y', colors=p1.get_color(), **tkw)
1155+
par1.tick_params(axis='y', colors=p2.get_color(), **tkw)
1156+
par2.tick_params(axis='y', colors=p3.get_color(), **tkw)
1157+
host.tick_params(axis='x', **tkw)

0 commit comments

Comments
 (0)