Skip to content

Commit c98af04

Browse files
Export polar grid colors in mplexporter
1 parent fc628e1 commit c98af04

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
@@ -278,13 +278,14 @@ def get_axis_properties(axis):
278278

279279
def get_grid_style(axis):
280280
gridlines = axis.get_gridlines()
281-
if axis.get_tick_params()["gridOn"] and len(gridlines) > 0:
282-
color = export_color(gridlines[0].get_color())
283-
alpha = gridlines[0].get_alpha()
284-
dasharray = get_dasharray(gridlines[0])
285-
return dict(gridOn=True, color=color, dasharray=dasharray, alpha=alpha)
286-
else:
281+
if len(gridlines) == 0:
287282
return {"gridOn": False}
283+
return dict(
284+
gridOn=axis.get_tick_params().get("gridOn", False),
285+
color=export_color(gridlines[0].get_color()),
286+
dasharray=get_dasharray(gridlines[0]),
287+
alpha=gridlines[0].get_alpha(),
288+
)
288289

289290

290291
def get_figure_properties(fig):

‎plotly/matplotlylib/renderer.py‎

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -124,18 +124,9 @@ def _open_polar_axes(self, ax, props):
124124
)
125125
theta_offset = ax.get_theta_offset()
126126
theta_direction = ax.get_theta_direction()
127-
angular_gridlines = ax.xaxis.get_gridlines()
128-
radial_gridlines = ax.yaxis.get_gridlines()
129-
angular_grid = (
130-
(angular_gridlines[0].get_color(), angular_gridlines[0].get_visible())
131-
if len(angular_gridlines)
132-
else ("#b0b0b0", True)
133-
)
134-
radial_grid = (
135-
(radial_gridlines[0].get_color(), radial_gridlines[0].get_visible())
136-
if len(radial_gridlines)
137-
else ("#b0b0b0", True)
138-
)
127+
axes = props.get("axes", [])
128+
angular_grid = axes[0].get("grid", {}) if len(axes) > 0 else {}
129+
radial_grid = axes[1].get("grid", {}) if len(axes) > 1 else {}
139130
frame = props.get("polar_frame")
140131
self.plotly_fig["layout"][self.current_polar_subplot] = go.layout.Polar(
141132
bgcolor=_export_color(props["axesbg"]),
@@ -144,8 +135,8 @@ def _open_polar_axes(self, ax, props):
144135
direction=("counterclockwise" if theta_direction >= 0 else "clockwise"),
145136
tickvals=[float(t) for t in np.degrees(ax.xaxis.get_majorticklocs())],
146137
ticktext=[t.get_text() for t in ax.xaxis.get_majorticklabels()],
147-
showgrid=angular_grid[1],
148-
gridcolor=_export_color(angular_grid[0]),
138+
showgrid=angular_grid.get("gridOn", True),
139+
gridcolor=_export_color(angular_grid.get("color", "#B0B0B0")),
149140
showline=frame["visible"] if frame else True,
150141
linecolor=(_export_color(frame["color"]) if frame else "black"),
151142
linewidth=frame["linewidth"] if frame else 1,
@@ -154,8 +145,8 @@ def _open_polar_axes(self, ax, props):
154145
range=[float(v) for v in ax.get_ylim()],
155146
tickvals=[float(t) for t in ax.yaxis.get_majorticklocs()],
156147
ticktext=[t.get_text() for t in ax.yaxis.get_majorticklabels()],
157-
showgrid=radial_grid[1],
158-
gridcolor=_export_color(radial_grid[0]),
148+
showgrid=radial_grid.get("gridOn", True),
149+
gridcolor=_export_color(radial_grid.get("color", "#B0B0B0")),
159150
showline=False,
160151
),
161152
)

‎plotly/matplotlylib/tests/test_renderer.py‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -873,13 +873,34 @@ def test_polar_plot_converts():
873873
assert polar.angularaxis.ticktext[0] == "0°"
874874
assert polar.radialaxis.range == tuple(float(v) for v in ax.get_ylim())
875875
assert polar.bgcolor == "#FFFFFF"
876-
assert polar.angularaxis.gridcolor == "#b0b0b0"
877-
assert polar.radialaxis.gridcolor == "#b0b0b0"
876+
assert polar.angularaxis.gridcolor == "#B0B0B0"
877+
assert polar.radialaxis.gridcolor == "#B0B0B0"
878878
assert polar.angularaxis.linecolor == "#000000"
879879
assert polar.angularaxis.linewidth == 0.8
880880
assert polar.radialaxis.showline is False
881881

882882

883+
def test_polar_grid_with_tuple_color():
884+
"""Polar axes with tuple grid colors convert successfully."""
885+
fig, ax = plt.subplots(subplot_kw={"projection": "polar"})
886+
ax.grid(color=(0.5, 0.5, 0.5))
887+
plotly_fig = tls.mpl_to_plotly(fig)
888+
889+
assert plotly_fig.layout.polar.angularaxis.gridcolor == "#7F7F7F"
890+
assert plotly_fig.layout.polar.radialaxis.gridcolor == "#7F7F7F"
891+
892+
893+
def test_cartesian_grid_with_tuple_color():
894+
"""Cartesian axes with tuple grid colors convert successfully."""
895+
fig, ax = plt.subplots()
896+
ax.grid(color=(0.5, 0.5, 0.5))
897+
ax.plot([0, 1], [0, 1])
898+
plotly_fig = tls.mpl_to_plotly(fig)
899+
900+
assert plotly_fig.layout.xaxis.showgrid is True
901+
assert plotly_fig.layout.yaxis.showgrid is True
902+
903+
883904
def test_custom_date_xtickvals_given_as_numbers_are_converted():
884905
"""Custom date ticks given as matplotlib date numbers must be converted
885906
to date strings."""

0 commit comments

Comments
 (0)