Seaborn列向stripplot报错ValueError:min()参数为空序列
解决Seaborn stripplot绘制DataFrame时的ValueError问题
问题重现
尝试用Seaborn绘制DataFrame列数据,代码如下:
#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")
处理后的DataFrame:
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.27650659776 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
运行后触发错误:
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
问题原因
错误根源是参数使用混淆:Set2是Seaborn的调色板(palette)名称,用于给不同类别分配一组颜色,但你把它传给了color参数——这个参数是用来指定单个统一颜色的,Seaborn无法将调色板名称解析为有效颜色,导致生成的颜色列表为空,进而触发min() arg is an empty sequence错误。
解决方案
把color="Set2"替换为palette="Set2",让Seaborn正确识别调色板:
# 修正后的绘图代码 sns.stripplot(data=df_plot_intra.drop("Patient_ID", axis=1), palette="Set2")
如果想给所有数据点设置同一个颜色,才使用color参数,比如:
# 所有点用蓝色 sns.stripplot(data=df_plot_intra.drop("Patient_ID", axis=1), color="blue")
内容的提问来源于stack exchange,提问作者gmut
相关产品推荐
相关产品推荐

