From 51d4959345c0a92785c74e4e96f0812740c91fce Mon Sep 17 00:00:00 2001 From: William Qian Date: Thu, 30 Jun 2022 16:45:25 -0400 Subject: [PATCH 1/4] Replace hardcoded parameter names when creating error bars Use `inspect.signature` to automagically check whether a `kwarg` is a line argument, rather than relying on a hardcoded list. --- lib/matplotlib/axes/_axes.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 0c80d87d7c22..97aa7c1750e6 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -1,4 +1,5 @@ import functools +import inspect import itertools import logging import math @@ -3453,11 +3454,7 @@ def _upcast_err(err): # Eject any line-specific information from format string, as it's not # needed for bars or caps. - for key in ['marker', 'markersize', 'markerfacecolor', - 'markeredgewidth', 'markeredgecolor', 'markevery', - 'linestyle', 'fillstyle', 'drawstyle', 'dash_capstyle', - 'dash_joinstyle', 'solid_capstyle', 'solid_joinstyle', - 'dashes']: + for key in inspect.signature(mlines.Line2D).parameters: base_style.pop(key, None) # Make the style dict for the line collections (the bars). From 9958432696aab224649ee1c662eaade8ed5633aa Mon Sep 17 00:00:00 2001 From: William Qian Date: Thu, 30 Jun 2022 16:56:28 -0400 Subject: [PATCH 2/4] Document API changes --- doc/api/next_api_changes/behavior/23376-WLQ.rst | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 doc/api/next_api_changes/behavior/23376-WLQ.rst diff --git a/doc/api/next_api_changes/behavior/23376-WLQ.rst b/doc/api/next_api_changes/behavior/23376-WLQ.rst new file mode 100644 index 000000000000..cf43fe800c71 --- /dev/null +++ b/doc/api/next_api_changes/behavior/23376-WLQ.rst @@ -0,0 +1,4 @@ +All `kwargs` parameters to Line2D are now appropriately supported by axes.errorbar +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Keys are now detected through the signature of Line2D, rather than through a +hardcoded list. From 68d858ceb14ae9bd9fd028594282b6841b77e083 Mon Sep 17 00:00:00 2001 From: William Qian Date: Mon, 4 Jul 2022 23:15:12 -0400 Subject: [PATCH 3/4] Improve parameter passing detection using LineCollection parameters Since all parameters will be passed to `LineCollection.set()`, we can just use that as the primary filter. If there are other keys that should be excluded, they should be handled separately, such as by overwriting the value, as is done for some keys. Notably, `set()` is a kwarg-based key-value updater that uses the `LineCollection`'s `kwargs` specification to perform the appropriate updates. This change allows for all of the gatekeeping to be centralized. --- lib/matplotlib/axes/_axes.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 97aa7c1750e6..ec71c1f5a4e5 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -3452,10 +3452,11 @@ def _upcast_err(err): if ecolor is None: ecolor = base_style['color'] - # Eject any line-specific information from format string, as it's not - # needed for bars or caps. - for key in inspect.signature(mlines.Line2D).parameters: - base_style.pop(key, None) + # Eject anything that's not acceptable by LineCollection.set() + lc_keys = inspect.signature(mcoll.LineCollection.set).parameters + for key in tuple(base_style.keys()): # Because we're resizing base_style + if key not in lc_keys: + base_style.pop(key, None) # Make the style dict for the line collections (the bars). eb_lines_style = {**base_style, 'color': ecolor} From 47205bc3da81a4312aa87ba7028df16866a3b15e Mon Sep 17 00:00:00 2001 From: William Qian Date: Mon, 4 Jul 2022 23:51:26 -0400 Subject: [PATCH 4/4] Also include previously-rejected keys --- lib/matplotlib/axes/_axes.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index ec71c1f5a4e5..6cd53553b92d 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -3454,8 +3454,14 @@ def _upcast_err(err): # Eject anything that's not acceptable by LineCollection.set() lc_keys = inspect.signature(mcoll.LineCollection.set).parameters - for key in tuple(base_style.keys()): # Because we're resizing base_style - if key not in lc_keys: + rej_keys = ['marker', 'markersize', 'markerfacecolor', + 'markeredgewidth', 'markeredgecolor', 'markevery', + 'linestyle', 'fillstyle', 'drawstyle', 'dash_capstyle', + 'dash_joinstyle', 'solid_capstyle', 'solid_joinstyle', + 'dashes'] + # Because we're resizing base_style + for key in tuple(base_style.keys()): + if key not in lc_keys or key in rej_keys: base_style.pop(key, None) # Make the style dict for the line collections (the bars).