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

scatter plot with wide data: markers=False gives an error #3652

Open jhncls opened 8 months ago

jhncls commented 8 months ago

Seaborn 0.13 is getting more friendly towards wide form data. With a scatter plot, the index (if available) is used as x=, and the columns are used both as hue= and style=. If the user wants to remove the effects of style, markers=False gives an error : "KeyError: 'marker'".

Code tested with seaborn 0.13.2 and also the latest dev version:

flights_wide = sns.load_dataset('flights').pivot(index='year', columns='month', values='passengers')
sns.scatterplot(data=flights_wide, markers=False)

With markers=['.'], there is a warning about not having enough markers. (The more convoluted markers=['.']*len(flights_wide.columns) works without problem.)

On the other hand, marker='.' is completely ignored.

PS: trace of the error with markers=False (seaborn 0.13.2):

Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm 2023.3.2\plugins\python\helpers\pydev\pydevconsole.py", line 364, in runcode
    coro = func()
           ^^^^^^
  File "<input>", line 1, in <module>
  File "C:\...\venv\Lib\site-packages\seaborn\relational.py", line 636, in scatterplot
    p.plot(ax, kwargs)
  File "C:\...\venv\Lib\site-packages\seaborn\relational.py", line 425, in plot
    example_marker = self._style_map(example_level, "marker")
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\...\venv\Lib\site-packages\seaborn\_base.py", line 85, in __call__
    return self._lookup_single(key, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\...\venv\Lib\site-packages\seaborn\_base.py", line 588, in _lookup_single
    value = self.lookup_table[key][attr]
            ~~~~~~~~~~~~~~~~~~~~~~^^^^^^
KeyError: 'marker'
mwaskom commented 8 months ago

Hm this sounds like a regression, do you happen to know if it works on 0.11/0.12?

jhncls commented 8 months ago

Testing with 0.13.2 / 0.13.0 / 0.12.2 / 0.12.0 / 0.11.2 / 0.11.0 all give the same error at the same line value = self.lookup_table[key][attr].

For a minimal test case: sns.scatterplot(data=[[7]], markers=False)

mwaskom commented 8 months ago

Thanks for checking. Of note, lineplot handles this as expected

sns.lineplot(tips, markers=False, dashes=False)

image