Skip to content

Commit 5fcad01

Browse files
Export polar grid colors in mplexporter
1 parent 857f2cf commit 5fcad01

3 files changed

Lines changed: 37 additions & 24 deletions

File tree

‎plotly/matplotlylib/mplexporter/utils.py‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -257,13 +257,14 @@ def get_axis_properties(axis):
257257

258258
def get_grid_style(axis):
259259
gridlines = axis.get_gridlines()
260-
if axis.get_tick_params()["gridOn"] and len(gridlines) > 0:
261-
color = export_color(gridlines[0].get_color())
262-
alpha = gridlines[0].get_alpha()
263-
dasharray = get_dasharray(gridlines[0])
264-
return dict(gridOn=True, color=color, dasharray=dasharray, alpha=alpha)
265-
else:
260+
if len(gridlines) == 0:
266261
return {"gridOn": False}
262+
return dict(
263+
gridOn=axis.get_tick_params().get("gridOn", False),
264+
color=export_color(gridlines[0].get_color()),
265+
dasharray=get_dasharray(gridlines[0]),
266+
alpha=gridlines[0].get_alpha(),
267+
)
267268

268269

269270
def get_figure_properties(fig):

‎plotly/matplotlylib/renderer.py‎

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -99,18 +99,9 @@ def _open_polar_axes(self, ax, props):
9999
)
100100
theta_offset = ax.get_theta_offset()
101101
theta_direction = ax.get_theta_direction()
102-
angular_gridlines = ax.xaxis.get_gridlines()
103-
radial_gridlines = ax.yaxis.get_gridlines()
104-
angular_grid = (
105-
(angular_gridlines[0].get_color(), angular_gridlines[0].get_visible())
106-
if len(angular_gridlines)
107-
else ("#b0b0b0", True)
108-
)
109-
radial_grid = (
110-
(radial_gridlines[0].get_color(), radial_gridlines[0].get_visible())
111-
if len(radial_gridlines)
112-
else ("#b0b0b0", True)
113-
)
102+
axes = props.get("axes", [])
103+
angular_grid = axes[0].get("grid", {}) if len(axes) > 0 else {}
104+
radial_grid = axes[1].get("grid", {}) if len(axes) > 1 else {}
114105
frame = props.get("polar_frame")
115106
self.plotly_fig["layout"][self.current_polar_subplot] = go.layout.Polar(
116107
bgcolor=_export_color(props["axesbg"]),
@@ -119,8 +110,8 @@ def _open_polar_axes(self, ax, props):
119110
direction=("counterclockwise" if theta_direction >= 0 else "clockwise"),
120111
tickvals=[float(t) for t in np.degrees(ax.xaxis.get_majorticklocs())],
121112
ticktext=[t.get_text() for t in ax.xaxis.get_majorticklabels()],
122-
showgrid=angular_grid[1],
123-
gridcolor=_export_color(angular_grid[0]),
113+
showgrid=angular_grid.get("gridOn", True),
114+
gridcolor=_export_color(angular_grid.get("color", "#B0B0B0")),
124115
showline=frame["visible"] if frame else True,
125116
linecolor=(_export_color(frame["color"]) if frame else "black"),
126117
linewidth=frame["linewidth"] if frame else 1,
@@ -129,8 +120,8 @@ def _open_polar_axes(self, ax, props):
129120
range=[float(v) for v in ax.get_ylim()],
130121
tickvals=[float(t) for t in ax.yaxis.get_majorticklocs()],
131122
ticktext=[t.get_text() for t in ax.yaxis.get_majorticklabels()],
132-
showgrid=radial_grid[1],
133-
gridcolor=_export_color(radial_grid[0]),
123+
showgrid=radial_grid.get("gridOn", True),
124+
gridcolor=_export_color(radial_grid.get("color", "#B0B0B0")),
134125
showline=False,
135126
),
136127
)

‎plotly/matplotlylib/tests/test_renderer.py‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,13 +354,34 @@ def test_polar_plot_converts():
354354
assert polar.angularaxis.ticktext[0] == "0°"
355355
assert polar.radialaxis.range == tuple(float(v) for v in ax.get_ylim())
356356
assert polar.bgcolor == "#FFFFFF"
357-
assert polar.angularaxis.gridcolor == "#b0b0b0"
358-
assert polar.radialaxis.gridcolor == "#b0b0b0"
357+
assert polar.angularaxis.gridcolor == "#B0B0B0"
358+
assert polar.radialaxis.gridcolor == "#B0B0B0"
359359
assert polar.angularaxis.linecolor == "#000000"
360360
assert polar.angularaxis.linewidth == 0.8
361361
assert polar.radialaxis.showline is False
362362

363363

364+
def test_polar_grid_with_tuple_color():
365+
"""Polar axes with tuple grid colors convert successfully."""
366+
fig, ax = plt.subplots(subplot_kw={"projection": "polar"})
367+
ax.grid(color=(0.5, 0.5, 0.5))
368+
plotly_fig = tls.mpl_to_plotly(fig)
369+
370+
assert plotly_fig.layout.polar.angularaxis.gridcolor == "#7F7F7F"
371+
assert plotly_fig.layout.polar.radialaxis.gridcolor == "#7F7F7F"
372+
373+
374+
def test_cartesian_grid_with_tuple_color():
375+
"""Cartesian axes with tuple grid colors convert successfully."""
376+
fig, ax = plt.subplots()
377+
ax.grid(color=(0.5, 0.5, 0.5))
378+
ax.plot([0, 1], [0, 1])
379+
plotly_fig = tls.mpl_to_plotly(fig)
380+
381+
assert plotly_fig.layout.xaxis.showgrid is True
382+
assert plotly_fig.layout.yaxis.showgrid is True
383+
384+
364385
def test_custom_date_xtickvals_given_as_numbers_are_converted():
365386
"""Custom date ticks given as matplotlib date numbers must be converted
366387
to date strings."""

0 commit comments

Comments
 (0)