首页 > 解决方案 > Seaborn Catplot 误差与分类预测变量

问题描述

我正在使用一个数据集,该数据集具有一个分类预测变量(值为东海岸或西海岸)和一个因变量(分钟)。这是 CSV 文件: https ://drive.google.com/file/d/15wlmm1gN4dPUiZCwALXVP3YpE31A6EqI/view?usp=sharing

我试图将 Coast 设置为 X 轴上的自变量,将 Minutes 设置为 Y 轴上的因变量:

import pandas as pd
import seaborn as sns

data = {'Minutes': [83, 85, 87, 84, 81, 74, 84, 87, 75, 72, 79, 86, 73, 70, 83, 83, 85, 85, 75, 90, 87, 81, 85, 81, 80, 86, 71, 78, 85, 73, 82, 81, 71, 84, 80, 80, 82, 89, 77, 88, 73, 86, 85, 80, 77, 81, 88, 81, 90, 77, 90, 71, 87, 81, 79, 72, 87, 72, 79, 88, 73, 86, 83, 82, 81, 83, 82, 90, 78, 72, 76, 74, 78, 70, 83, 84, 75, 84, 87, 72, 73, 80, 75, 80, 72, 76, 82, 76, 76, 70, 79, 80, 87, 90, 81, 83, 82, 87, 90, 77, 66, 69, 72, 63, 66, 61, 71, 64, 76, 67, 71, 65, 72, 61, 74, 64, 75, 80, 78, 78, 69, 77, 65, 66, 72, 72, 66, 69, 62, 63, 62, 68, 63, 63, 78, 61, 69, 72, 63, 61, 66, 60, 62, 78, 63, 76, 75, 64, 80, 69, 75, 64, 65, 68, 67, 74, 77, 80, 64, 61, 68, 72, 63, 70, 64, 75, 74, 73, 79, 63, 61, 68, 80, 79, 67, 67, 67, 61, 66, 60, 64, 64, 61, 76, 64, 76, 70, 68, 69, 77, 69, 65, 65, 72, 76, 72, 78, 64, 70, 64], 'Coast': ['East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'East_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast', 'West_Coast']}
iphone_data_recoded = pd.DataFrame(data)

sns.catplot(x=iphone_data_recoded['Coast'], y=iphone_data_recoded['Minutes'], data=iphone_data_recoded)

我收到以下错误:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

完整的错误日志在此 RTF 中: https ://drive.google.com/file/d/1KjzP1_6BYgnDhjU-Ug4EHjelZcbNY3dB/view?usp=sharing

---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-194-d0546cb13868> in <module>
----> 1 sns.catplot(x=iphone_data_recoded['Coast'],
y=iphone_data_recoded['Minutes'], data=iphone_data_recoded)
~/opt/anaconda3/lib/python3.7/site-packages/seaborn/categorical.py in catplot(x,
y, hue, data, row, col, col_wrap, estimator, ci, n_boot, units, order, hue_order,
row_order, col_order, kind, height, aspect, orient, color, palette, legend,
legend_out, sharex, sharey, margin_titles, facet_kws, **kwargs)
3753
3754 # Draw the plot onto the facets
-> 3755 g.map_dataframe(plot_func, x, y, hue, **plot_kws)
3756
3757 # Special case axis labels for a count type plot
~/opt/anaconda3/lib/python3.7/site-packages/seaborn/axisgrid.py in
map_dataframe(self, func, *args, **kwargs)
821
822 # Finalize the annotations and layout
--> 823 self._finalize_grid(args[:2])
824
825 return self
~/opt/anaconda3/lib/python3.7/site-packages/seaborn/axisgrid.py in
_finalize_grid(self, axlabels)
844 def _finalize_grid(self, axlabels):
845 """Finalize the annotations and layout."""
--> 846 self.set_axis_labels(*axlabels)
847 self.set_titles()
848 self.fig.tight_layout()
~/opt/anaconda3/lib/python3.7/site-packages/seaborn/axisgrid.py in
set_axis_labels(self, x_var, y_var)
870 if x_var is not None:
871 self._x_var = x_var
--> 872 self.set_xlabels(x_var)
873 if y_var is not None:
874 self._y_var = y_var
~/opt/anaconda3/lib/python3.7/site-packages/seaborn/axisgrid.py in
set_xlabels(self, label, **kwargs)
881 label = self._x_var
882 for ax in self._bottom_axes:
--> 883 ax.set_xlabel(label, **kwargs)
884 return self

885
~/opt/anaconda3/lib/python3.7/site-packages/matplotlib/axes/_axes.py in
set_xlabel(self, xlabel, fontdict, labelpad, **kwargs)
245 if labelpad is not None:
246 self.xaxis.labelpad = labelpad
--> 247 return self.xaxis.set_label_text(xlabel, fontdict, **kwargs)
248
249 def get_ylabel(self):
~/opt/anaconda3/lib/python3.7/site-packages/matplotlib/axis.py in
set_label_text(self, label, fontdict, **kwargs)
1594 """
1595 self.isDefault_label = False
-> 1596 self.label.set_text(label)
1597 if fontdict is not None:
1598 self.label.update(fontdict)
~/opt/anaconda3/lib/python3.7/site-packages/matplotlib/text.py in set_text(self, s)
1148 if s is None:
1149 s = ''
-> 1150 if s != self._text:
1151 self._text = str(s)
1152 self.stale = True
~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/generic.py in
__nonzero__(self)
1553 "The truth value of a {0} is ambiguous. "
1554 "Use a.empty, a.bool(), a.item(), a.any() or a.all().".format(
-> 1555 self.__class__.__name__
1556 )
1557 )
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(),
a.item(), a.any() or a.all().

标签: pythonstatisticsseabornvisualizationcategorical-data

解决方案


对于 seaborn,您可以执行以下操作:

sns.catplot(x='Coast', y='Minutes', data=iphone_data_recoded)

在此处输入图像描述


推荐阅读