mwaskom / seaborn

Statistical data visualization in Python
https://seaborn.pydata.org
BSD 3-Clause "New" or "Revised" License
12.62k stars 1.94k forks source link

`lineplot(..., dashes)` argument does not support string descriptors #3681

Closed JeppeKlitgaard closed 7 months ago

JeppeKlitgaard commented 7 months ago

When using lineplot with style set to a categorial, the dashes argument cannot be specified using the conventional matplotlib strings like dashed or -, but must take a list of dash tuples like (1, 0), otherwise it fails with a nasty error.

MWE:

import pandas as pd
import seaborn as sns

d = pd.DataFrame([
    {"a": 1, "b:": 2.0, "c": "A"},
    {"a": 2, "b:": 1.0, "c": "A"},
    {"a": 3, "b:": 3.0, "c": "B"},
    {"a": 4, "b:": 4.0, "c": "B"},
])

sns.lineplot(data=d, x="a", y="b:", style="c", dashes=["-", "-"])

Gives:

TypeError: unsupported operand type(s) for +: 'int' and 'str'

The intended behaviour can be obtained using dashes=[(1, 0), (1, 0)]

This is because Line2D.set_dashes is used instead of Line2D.set_linestyle. Perhaps an additional argument styles= could be added which would use the set_linestyle method instead?

mwaskom commented 7 months ago

Hi, this is documented:

Dashes are specified as in matplotlib: a tuple of (segment, gap) lengths, or an empty string to draw a solid line.

-1 on having both style and styles that is much too confusing imo

mwaskom commented 7 months ago

Going to close as this is intentional and documented — the dash patterns are a superset of the linestyle codes and they ultimately set the same underlying matplotlib attributes so you could never use them at the same time.

JeppeKlitgaard commented 7 months ago

I completely understand you want to avoid the confusion of having style and styles. Would you consider adding a check on the form of the dashes argument to give a more intuitive error?