Thanks to visit codestin.com
Credit goes to github.com

Skip to content

KeyError: 'gridOn' in axis.py when axis.tick_params() is used with reset = True #20149

@Klemet

Description

@Klemet

Bug report

Bug summary

Using matplotlib 3.4.1 on a fresh miniconda installation, trying to add a grid to an axis can return an error.
It seems to be linked with the "reset" option of the tick_param function of an axis, if the function has been used just before adding the grid. Activating the option seems to cause the issue; removing the option makes everything work.

Code for reproduction

fig, axes = plt.subplots(1, 4, sharey= True)

# This doesn't work
for axis in axes:
    axis.tick_params(reset = True, axis='both', which='major', labelsize= 10, right = False, top = False)
for axis in axes:
    axis.grid(color = "gainsboro", linestyle='-', linewidth=0.5) 
    axis.set_facecolor('#FCFCFC')

# This works
for axis in axes:
    axis.tick_params(reset = False, axis='both', which='major', labelsize= 10, right = False, top = False)
for axis in axes:
    axis.grid(color = "gainsboro", linestyle='-', linewidth=0.5) 
    axis.set_facecolor('#FCFCFC')

Actual outcome

  File "C:\ProgramData\Miniconda3\lib\site-packages\matplotlib\axis.py", line 1414, in grid
    gridkw['gridOn'] = not self._major_tick_kw['gridOn']

KeyError: 'gridOn'

Expected outcome

The grid is expected to be added to the figure without any issue, resulting in the following appearance :

image

Matplotlib version

  • Operating system: Windows 10
  • Matplotlib version (import matplotlib; print(matplotlib.__version__)): 3.4.1
  • Matplotlib backend (print(matplotlib.get_backend())): Qt5Agg
  • Python version: 3.9.1

Matplotlib was installed via the anaconda command prompt, using conda install -c conda-forge matplotlib.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions