Skip to content

Commit ca6badf

Browse files
Support overlaying and visibility for twinned and overlaid subplots
1 parent 3cc8a40 commit ca6badf

2 files changed

Lines changed: 41 additions & 2 deletions

File tree

‎plotly/matplotlylib/renderer.py‎

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ def __init__(self):
6767
self.msg = "Initialized PlotlyRenderer\n"
6868
self._processing_legend = False
6969
self._legend_visible = False
70+
self.axes_list = []
7071

7172
def _convert_x_dates(self, x):
7273
"""Convert x values to date strings when the x-axis is a date axis."""
@@ -167,8 +168,8 @@ def open_axes(self, ax, props):
167168
]
168169
self.current_bars = []
169170
self.axis_ct += 1
170-
# update plot background with the axes background from mpl
171-
self.plotly_fig["layout"].plot_bgcolor = _export_color(props["axesbg"])
171+
if props.get("patch_visible", True):
172+
self.plotly_fig["layout"].plot_bgcolor = _export_color(props["axesbg"])
172173
# set defaults in axes
173174
xaxis = go.layout.XAxis(
174175
anchor="y{0}".format(self.axis_ct), zeroline=False, ticks="inside"
@@ -223,6 +224,36 @@ def open_axes(self, ax, props):
223224
if not y_main_ticks:
224225
yaxis["ticks"] = ""
225226

227+
overlay_ax_ct = None
228+
for prev_ax, prev_ct in self.axes_list:
229+
is_twinned = (
230+
(
231+
hasattr(ax, "_twinned_axes")
232+
and ax in ax._twinned_axes.get_siblings(prev_ax)
233+
)
234+
or getattr(ax, "_sharex", None) == prev_ax
235+
or getattr(ax, "_sharey", None) == prev_ax
236+
or getattr(prev_ax, "_sharex", None) == ax
237+
or getattr(prev_ax, "_sharey", None) == ax
238+
or (ax.get_position().bounds == prev_ax.get_position().bounds)
239+
)
240+
if is_twinned:
241+
overlay_ax_ct = prev_ct
242+
break
243+
244+
if overlay_ax_ct is not None:
245+
overlay_x = "x" if overlay_ax_ct == 1 else "x{0}".format(overlay_ax_ct)
246+
overlay_y = "y" if overlay_ax_ct == 1 else "y{0}".format(overlay_ax_ct)
247+
xaxis["overlaying"] = overlay_x
248+
yaxis["overlaying"] = overlay_y
249+
250+
if not props["axes"][0]["visible"]:
251+
xaxis["visible"] = False
252+
if not props["axes"][1]["visible"]:
253+
yaxis["visible"] = False
254+
255+
self.axes_list.append((ax, self.axis_ct))
256+
226257
# put axes in our figure
227258
self.plotly_fig["layout"]["xaxis{0}".format(self.axis_ct)] = xaxis
228259
self.plotly_fig["layout"]["yaxis{0}".format(self.axis_ct)] = yaxis

‎plotly/matplotlylib/tests/test_renderer.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,10 @@ def test_twinx_axis_position_and_ticks():
224224
assert plotly_fig.layout.yaxis2.side == "right"
225225
assert plotly_fig.layout.yaxis2.title.text == "right axis"
226226
assert plotly_fig.layout.yaxis2.ticks == "inside"
227+
assert plotly_fig.layout.yaxis2.overlaying == "y"
228+
assert plotly_fig.layout.xaxis2.overlaying == "x"
229+
assert plotly_fig.layout.xaxis2.visible is False
230+
assert len(plotly_fig.data) == 2
227231

228232

229233
def test_twiny_axis_position_and_ticks():
@@ -244,6 +248,10 @@ def test_twiny_axis_position_and_ticks():
244248
assert plotly_fig.layout.xaxis2.side == "top"
245249
assert plotly_fig.layout.xaxis2.title.text == "top axis"
246250
assert plotly_fig.layout.xaxis2.ticks == "inside"
251+
assert plotly_fig.layout.xaxis2.overlaying == "x"
252+
assert plotly_fig.layout.yaxis2.overlaying == "y"
253+
assert plotly_fig.layout.yaxis2.visible is False
254+
assert len(plotly_fig.data) == 2
247255

248256

249257
def test_right_axis_ticks_hidden_when_mpl_right_ticks_hidden():

0 commit comments

Comments
 (0)