gmut
gmut

Reputation: 7

Seaborn columnwise stripplot gives ValueError

I'm simply trying to plot the columns of my dataframe with seaborn:

#Plot CoV Vals per patient
#make df
data = {'Patient_ID': patient_list,
        'Manual Reader 1': intraCoV_kate_dict.values(),
        'Manual Reader 2': intraCoV_selene_dict.values(),
        'Automatic bg selection': intraCoV_homog_dict.values(),
       }
  
# Create DataFrame
df_plot_intra = pd.DataFrame(data)
print(df_plot_intra)
print(df_plot_intra.drop("Patient_ID", axis=1))

#plot with seaborn
sns.stripplot(data=df_plot_intra.drop("Patient_ID", axis=1), color = "Set2")

My Dataframe (with the indicated column dropped) looks like this:

       Manual Reader 1     Manual Reader 2 Automatic bg selection
0    512121.7703226448   41915.74655761683      18103.86823163034
1    62971.19555431088  30371.248398078853     293949.27579836844
2    186444.3934911384  124438.71031050631     237108.97954526436
3    20464.24914545304  10433.799042052842      8920.798190557956
4    270549.4280501692    488004.964203974     114649.76948070318
5    553194.5961811391   752083.7511653332     46893.265540308035
6   1135634.3045041235   78546.71945962348      79877.26708738977
7    81995.44945779811    71576.4734166673     28025.684730116525
8    58135.01545385555   82652.93203094913     10399.192011960262
9    44931.50850748612  41390.559493600194     119254.46795220574
10  469287.15919308254   29877.27650659776      758667.5483106133
11  35321.755595259376   4413.871028266582      2630.377217394228
12   82658.52800267116   36445.10419234513     1266.6083717539414
13  117421.76949639994  514727.90414553485      71753.88248988726
14    651960.843691586  516501.42951649544      8733.196282802484

I get the error:

ValueError                                Traceback (most recent call last)
Input In [102], in <cell line: 14>()
     12 print(df_plot_intra.drop("Patient_ID", axis=1))
     13 #plot with seaborn
---> 14 sns.stripplot(data=df_plot_intra.drop("Patient_ID", axis=1), color = "Set2")

File ~\anaconda3\envs\hids3\lib\site-packages\seaborn\_decorators.py:46, in _deprecate_positional_args.<locals>.inner_f(*args, **kwargs)
     36     warnings.warn(
     37         "Pass the following variable{} as {}keyword arg{}: {}. "
     38         "From version 0.12, the only valid positional argument "
   (...)
     43         FutureWarning
     44     )
     45 kwargs.update({k: arg for k, arg in zip(sig.parameters, args)})
---> 46 return f(**kwargs)

File ~\anaconda3\envs\hids3\lib\site-packages\seaborn\categorical.py:2807, in stripplot(x, y, hue, data, order, hue_order, jitter, dodge, orient, color, palette, size, edgecolor, linewidth, ax, **kwargs)
   2804     msg = "The `split` parameter has been renamed to `dodge`."
   2805     warnings.warn(msg, UserWarning)
-> 2807 plotter = _StripPlotter(x, y, hue, data, order, hue_order,
   2808                         jitter, dodge, orient, color, palette)
   2809 if ax is None:
   2810     ax = plt.gca()

File ~\anaconda3\envs\hids3\lib\site-packages\seaborn\categorical.py:1100, in _StripPlotter.__init__(self, x, y, hue, data, order, hue_order, jitter, dodge, orient, color, palette)
   1098 """Initialize the plotter."""
   1099 self.establish_variables(x, y, hue, data, orient, order, hue_order)
-> 1100 self.establish_colors(color, palette, 1)
   1102 # Set object attributes
   1103 self.dodge = dodge

File ~\anaconda3\envs\hids3\lib\site-packages\seaborn\categorical.py:319, in _CategoricalPlotter.establish_colors(self, color, palette, saturation)
    317 # Determine the gray color to use for the lines framing the plot
    318 light_vals = [colorsys.rgb_to_hls(*c)[1] for c in rgb_colors]
--> 319 lum = min(light_vals) * .6
    320 gray = mpl.colors.rgb2hex((lum, lum, lum))
    322 # Assign object attributes

ValueError: min() arg is an empty sequence

First it seemed like this had something to do with specifying the color, but this didn't help. I'm trying to plot the columns of the dataframe with stripplot following this example: Seaborn stripplot based on columns

I've also checked for Nan's in the dataframe, but there are none.

Does anyone have a clue of what might be going wrong?

Upvotes: 0

Views: 904

Answers (1)

furas
furas

Reputation: 142631

I get this error when I have strings like "1.2" instead of numbers 1.2.

It needs to convert all strings to float using .astype(float)

sns.stripplot(data=df.drop("Patient_ID", axis=1).astype(float), color="Set2")

OR you should convert them at start

df[['Manual Reader 1', 'Manual Reader 2', 'Automatic bg selection']] = df[['Manual Reader 1', 'Manual Reader 2', 'Automatic bg selection']].astype(floata)

Minimal working example

import pandas as pd
import seaborn as sns

data = {
    'Patient_ID': ['a', 'b', 'c'],
    'A': [ '1.2',  '3.4',  '5.6'],  # strings
    'B': ['11.2', '13.4', '15.6'],  # strings
}

df = pd.DataFrame(data)
print(df)

df[['A','B']] = df[['A','B']].astype(float)
print(df)

print(df.drop("Patient_ID", axis=1))

sns.stripplot(data=df.drop("Patient_ID", axis=1)) #, color="Set2")

Upvotes: 0

Related Questions