From fbe3c0fd1ae03270aab52f880f01a91aa828999d Mon Sep 17 00:00:00 2001 From: Lukas Schrangl Date: Thu, 11 Dec 2025 14:19:26 +0100 Subject: [PATCH] Update `matplotlib.rcParams.validate` instead of overwriting Other packages can update `rcParams.validate` as well. Overwriting can therefore lead to problems. --- matplotlib_scalebar/scalebar.py | 46 ++++++++++++++------------------- 1 file changed, 19 insertions(+), 27 deletions(-) diff --git a/matplotlib_scalebar/scalebar.py b/matplotlib_scalebar/scalebar.py index efd5a5d..7630681 100644 --- a/matplotlib_scalebar/scalebar.py +++ b/matplotlib_scalebar/scalebar.py @@ -96,33 +96,25 @@ def _validate_legend_loc(loc): return loc -defaultParams.update( - { - "scalebar.length_fraction": [0.2, validate_float], - "scalebar.width_fraction": [0.01, validate_float], - "scalebar.location": ["upper right", _validate_legend_loc], - "scalebar.pad": [0.2, validate_float], - "scalebar.border_pad": [0.1, validate_float], - "scalebar.sep": [5, validate_float], - "scalebar.frameon": [True, validate_bool], - "scalebar.color": ["k", validate_color], - "scalebar.box_color": ["w", validate_color], - "scalebar.box_alpha": [1.0, validate_float], - "scalebar.scale_loc": ["bottom", _validate_scale_loc], - "scalebar.label_loc": ["top", _validate_label_loc], - "scalebar.rotation": ["horizontal", _validate_rotation], - } -) - - -_all_deprecated = getattr(matplotlib, "_all_deprecated", {}) - - -# Recreate the validate function -matplotlib.rcParams.validate = dict( - (key, converter) - for key, (default, converter) in defaultParams.items() - if key not in _all_deprecated +scalebar_params = { + "scalebar.length_fraction": [0.2, validate_float], + "scalebar.width_fraction": [0.01, validate_float], + "scalebar.location": ["upper right", _validate_legend_loc], + "scalebar.pad": [0.2, validate_float], + "scalebar.border_pad": [0.1, validate_float], + "scalebar.sep": [5, validate_float], + "scalebar.frameon": [True, validate_bool], + "scalebar.color": ["k", validate_color], + "scalebar.box_color": ["w", validate_color], + "scalebar.box_alpha": [1.0, validate_float], + "scalebar.scale_loc": ["bottom", _validate_scale_loc], + "scalebar.label_loc": ["top", _validate_label_loc], + "scalebar.rotation": ["horizontal", _validate_rotation], +} +defaultParams.update(scalebar_params) +# Update the validate function +matplotlib.rcParams.validate.update( + dict((key, converter) for key, (_, converter) in scalebar_params.items()) ) # Dimension lookup