Skip to content

Commit 193a86b

Browse files
committed
Also export minor grid.
This will allow to render both major and minor gridlines in mpld3, which fixes the following issue: mpld3/mpld3#527 As per usual, disclaimer that I co-developed this with gpt-5.1-codex, having it figure out the issues and give implementation recommendations, with me testing, verifying, and tidying up the code. Here's what it has to say, especially wrt the change in API call: - include minor tick values/length and minor grid style in axis props so minor ticks/grids render in mpld3 - read grid color/linewidth/linestyle from tick kwargs (and rcParams fallback) instead of inspecting gridlines[0], avoiding the get_gridlines(which=…) API that isn’t available on matplotlib 3.10” Rationale for the kw/rc approach: `Axis.get_gridlines()` doesn’t accept `which` on matplotlib 3.10, so probing `gridlines[0]` for minor/major fails. Pulling style from the tick keyword dict (which matplotlib populates with `grid_*` fields when you call `ax.grid(...)`) plus `rcParams` defaults gives the same style without needing `get_gridlines(which=…)`, keeping compatibility and matching user-set grid styles. (I verified, indeed get_gridlines does not allow specifying which ones - seems like an omission in matplotlib API to me)
1 parent 30242fb commit 193a86b

1 file changed

Lines changed: 21 additions & 13 deletions

File tree

mplexporter/utils.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,10 @@ def get_axis_properties(axis):
212212
else:
213213
props['tickvalues'] = None
214214

215+
minor_locator = axis.get_minor_locator()
216+
props['minor_tickvalues'] = list(axis.get_minorticklocs()) if minor_locator else None
217+
props['minorticklength'] = axis._minor_tick_kw.get('size', None)
218+
215219
# Find tick formats
216220
props['tickformat_formatter'] = ""
217221
formatter = axis.get_major_formatter()
@@ -255,28 +259,32 @@ def get_axis_properties(axis):
255259

256260
# Get associated grid
257261
props['grid'] = get_grid_style(axis)
262+
props['minor_grid'] = get_grid_style(axis, which='minor')
258263

259264
# get axis visibility
260265
props['visible'] = axis.get_visible()
261266

262267
return props
263268

264269

265-
def get_grid_style(axis):
266-
gridlines = axis.get_gridlines()
267-
if axis._major_tick_kw['gridOn'] and len(gridlines) > 0:
268-
color = export_color(gridlines[0].get_color())
269-
alpha = gridlines[0].get_alpha()
270-
dasharray = get_dasharray(gridlines[0])
271-
linewidth = gridlines[0].get_linewidth()
272-
return dict(gridOn=True,
273-
color=color,
274-
dasharray=dasharray,
275-
linewidth=linewidth,
276-
alpha=alpha)
277-
else:
270+
def get_grid_style(axis, which='major'):
271+
tick_kw = axis._minor_tick_kw if which == 'minor' else axis._major_tick_kw
272+
273+
if not tick_kw.get('gridOn'):
278274
return {"gridOn": False}
279275

276+
rc = matplotlib.rcParams
277+
color = export_color(tick_kw.get('grid_color', tick_kw.get('grid_c', rc['grid.color'])))
278+
alpha = tick_kw.get('grid_alpha', rc['grid.alpha'])
279+
dasharray = _dasharray_from_linestyle(tick_kw.get('grid_linestyle', tick_kw.get('grid_ls', rc['grid.linestyle'])))
280+
linewidth = tick_kw.get('grid_linewidth', tick_kw.get('grid_lw', rc['grid.linewidth']))
281+
282+
return dict(gridOn=True,
283+
color=color,
284+
dasharray=dasharray,
285+
linewidth=linewidth,
286+
alpha=alpha)
287+
280288

281289
def get_figure_properties(fig):
282290
return {'figwidth': fig.get_figwidth(),

0 commit comments

Comments
 (0)