Skip to content

Commit 65d0839

Browse files
Export polar grid colors in mplexporter
1 parent b0cead1 commit 65d0839

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
@@ -129,18 +129,9 @@ def _open_polar_axes(self, ax, props):
129129
)
130130
theta_offset = ax.get_theta_offset()
131131
theta_direction = ax.get_theta_direction()
132-
angular_gridlines = ax.xaxis.get_gridlines()
133-
radial_gridlines = ax.yaxis.get_gridlines()
134-
angular_grid = (
135-
(angular_gridlines[0].get_color(), angular_gridlines[0].get_visible())
136-
if len(angular_gridlines)
137-
else ("#b0b0b0", True)
138-
)
139-
radial_grid = (
140-
(radial_gridlines[0].get_color(), radial_gridlines[0].get_visible())
141-
if len(radial_gridlines)
142-
else ("#b0b0b0", True)
143-
)
132+
axes = props.get("axes", [])
133+
angular_grid = axes[0].get("grid", {}) if len(axes) > 0 else {}
134+
radial_grid = axes[1].get("grid", {}) if len(axes) > 1 else {}
144135
frame = props.get("polar_frame")
145136
self.plotly_fig["layout"][self.current_polar_subplot] = go.layout.Polar(
146137
bgcolor=_export_color(props["axesbg"]),
@@ -149,8 +140,8 @@ def _open_polar_axes(self, ax, props):
149140
direction=("counterclockwise" if theta_direction >= 0 else "clockwise"),
150141
tickvals=[float(t) for t in np.degrees(ax.xaxis.get_majorticklocs())],
151142
ticktext=[t.get_text() for t in ax.xaxis.get_majorticklabels()],
152-
showgrid=angular_grid[1],
153-
gridcolor=_export_color(angular_grid[0]),
143+
showgrid=angular_grid.get("gridOn", True),
144+
gridcolor=_export_color(angular_grid.get("color", "#B0B0B0")),
154145
showline=frame["visible"] if frame else True,
155146
linecolor=(_export_color(frame["color"]) if frame else "black"),
156147
linewidth=frame["linewidth"] if frame else 1,
@@ -159,8 +150,8 @@ def _open_polar_axes(self, ax, props):
159150
range=[float(v) for v in ax.get_ylim()],
160151
tickvals=[float(t) for t in ax.yaxis.get_majorticklocs()],
161152
ticktext=[t.get_text() for t in ax.yaxis.get_majorticklabels()],
162-
showgrid=radial_grid[1],
163-
gridcolor=_export_color(radial_grid[0]),
153+
showgrid=radial_grid.get("gridOn", True),
154+
gridcolor=_export_color(radial_grid.get("color", "#B0B0B0")),
164155
showline=False,
165156
),
166157
)

‎plotly/matplotlylib/tests/test_renderer.py‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,13 +1155,34 @@ def test_polar_plot_converts():
11551155
assert polar.angularaxis.ticktext[0] == "0°"
11561156
assert polar.radialaxis.range == tuple(float(v) for v in ax.get_ylim())
11571157
assert polar.bgcolor == "#FFFFFF"
1158-
assert polar.angularaxis.gridcolor == "#b0b0b0"
1159-
assert polar.radialaxis.gridcolor == "#b0b0b0"
1158+
assert polar.angularaxis.gridcolor == "#B0B0B0"
1159+
assert polar.radialaxis.gridcolor == "#B0B0B0"
11601160
assert polar.angularaxis.linecolor == "#000000"
11611161
assert polar.angularaxis.linewidth == 0.8
11621162
assert polar.radialaxis.showline is False
11631163

11641164

1165+
def test_polar_grid_with_tuple_color():
1166+
"""Polar axes with tuple grid colors convert successfully."""
1167+
fig, ax = plt.subplots(subplot_kw={"projection": "polar"})
1168+
ax.grid(color=(0.5, 0.5, 0.5))
1169+
plotly_fig = tls.mpl_to_plotly(fig)
1170+
1171+
assert plotly_fig.layout.polar.angularaxis.gridcolor == "#7F7F7F"
1172+
assert plotly_fig.layout.polar.radialaxis.gridcolor == "#7F7F7F"
1173+
1174+
1175+
def test_cartesian_grid_with_tuple_color():
1176+
"""Cartesian axes with tuple grid colors convert successfully."""
1177+
fig, ax = plt.subplots()
1178+
ax.grid(color=(0.5, 0.5, 0.5))
1179+
ax.plot([0, 1], [0, 1])
1180+
plotly_fig = tls.mpl_to_plotly(fig)
1181+
1182+
assert plotly_fig.layout.xaxis.showgrid is True
1183+
assert plotly_fig.layout.yaxis.showgrid is True
1184+
1185+
11651186
def test_custom_date_xtickvals_given_as_numbers_are_converted():
11661187
"""Custom date ticks given as matplotlib date numbers must be converted
11671188
to date strings."""

0 commit comments

Comments
 (0)