lgienapp / aquarel

Styling matplotlib made easy
MIT License
725 stars 20 forks source link

set_tick_labels location parameter produces a value error #25

Closed hasan-alper closed 7 months ago

hasan-alper commented 2 years ago
theme = load_theme("arctic_dark").set_tick_labels(location="top")
theme.apply()

produces

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In [2], line 2
      1 theme = load_theme("arctic_dark").set_tick_labels(location="top")
----> 2 theme.apply()

File ~/Documents/aquarel/aquarel/theme.py:325, in Theme.apply(self)
    323         if mapped_key == "axes.prop_cycle":
    324             value = cycler("color", value)
--> 325         mpl.rcParams.update({sub_key: value})
    326 else:
    327     # Special treatment for color palette, as this is otherwise not JSON serializable
    328     if mapped_key == "axes.prop_cycle":

File /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/_collections_abc.py:940, in MutableMapping.update(self, other, **kwds)
    938 if isinstance(other, Mapping):
    939     for key in other:
--> 940         self[key] = other[key]
    941 elif hasattr(other, "keys"):
    942     for key in other.keys():

File ~/Documents/aquarel/venv/lib/python3.9/site-packages/matplotlib/__init__.py:648, in RcParams.__setitem__(self, key, val)
    646         cval = self.validate[key](val)
    647     except ValueError as ve:
--> 648         raise ValueError(f"Key {key}: {ve}") from None
    649     dict.__setitem__(self, key, cval)
    650 except KeyError as err:

ValueError: Key xaxis.labellocation: 'top' is not a valid value for xaxis.labellocation; supported values are ['left', 'center', 'right']

If location is left

ValueError: Key yaxis.labellocation: 'left' is not a valid value for yaxis.labellocation; supported values are ['bottom', 'center', 'top']

In the docs, valid options are given as this:


location (Optional[str]) – location of the tick labels, can be {“left”, “right”, “bottom”, “top”, “center”}, default: center
lucianosrp commented 7 months ago

I think the problem is that aquarel is attempting to set the same label position to both axes. The XAxis has only "left","center" and "right" as valid positions, while YAxis has "bottom", "center" and "top".

When you set the location as "top" the script is:

  1. setting the XAxis label position to "top" which raises the ValueError

When you set the location as "left" the script is:

  1. setting the XAxis label position to "left" (which is valid)
  2. attempts to set the YAxis label position to "left" which raises the ValueError

    No errors are being raise if you set the location as "center", which is the only position which is accepted by both Axes.

    I think aquarel should handle this scenarios better - @lgienapp I can have a look on a solution!

lgienapp commented 7 months ago

Sure, go ahead, a fix for this would be very welcome!