Skip to content

[Bug]: rcdefaults and rcParams.clear() don't work?  #25855

Open
@chahak13

Description

@chahak13

Bug summary

This is the rcdefaults() function

def rcdefaults():
"""
Restore the `.rcParams` from Matplotlib's internal default style.
Style-blacklisted `.rcParams` (defined in
``matplotlib.style.core.STYLE_BLACKLIST``) are not updated.
See Also
--------
matplotlib.rc_file_defaults
Restore the `.rcParams` from the rc file originally loaded by
Matplotlib.
matplotlib.style.use
Use a specific style file. Call ``style.use('default')`` to restore
the default style.
"""
# Deprecation warnings were already handled when creating rcParamsDefault,
# no need to reemit them here.
with _api.suppress_matplotlib_deprecation_warning():
from .style.core import STYLE_BLACKLIST
rcParams.clear()
rcParams.update({k: v for k, v in rcParamsDefault.items()
if k not in STYLE_BLACKLIST})

What exactly are we trying to do here? According to the docstring, if we're trying to reset the rcParams dictionary and only reset the keys that are not in STYLE_BLACKLIST then there are 2 problems I think:

  1. rcParams.clear() doesn't work. It doesn't clear out the entire dictionary.
  2. After the update line, rcParams will still have keys from STYLE_BLACKLIST which, if I were to perform the same operations on a dictionary, shouldn't be the case.
  3. While it does make sense to still have the keys from STYLE_BLACKLIST but then I'm not sure why clear() is used and not just directly update the keys.

Also, sidenote, popitem() errors out without a good error message (which is also why clear doesn't actually empty the dict?)

Code for reproduction

In [3]: mpl.rcParams["webagg.port"]
Out[3]: 8988

In [4]: mpl.rcParams["webagg.port"] = 9000

In [5]: mpl.rcParams["webagg.port"]
Out[5]: 9000

In [6]: mpl.rcdefaults()

In [7]: mpl.rcParams["webagg.port"]
Out[7]: 9000

In [8]: mpl.rcParams.clear()

In [9]: mpl.rcParams["webagg.port"]
Out[9]: 9000

Actual outcome

clear() doesn't empty the rcParams dictionary.

Expected outcome

clear() should empty the dictionary and after rcdefaults(), keys from STYLE_BLACKLIST shouldn't be there.

Additional information

I'm not sure if this is a documentation issue and the behaviour is intended or the behaviour isn't intended.

Operating system

No response

Matplotlib Version

Current main

Matplotlib Backend

No response

Python version

No response

Jupyter version

No response

Installation

None

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions