You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用seaborn jointplot时报TypeError:无法将序列与float相乘

问题

我有一个包含分类字段inc_open_hours_bucket和数值字段inc_volume的DataFrame,尝试使用seaborn的jointplot(kind='hex')绘制二者关系时,触发错误:TypeError: can't multiply sequence by non-int of type 'float'。原本以为分类字段和整数字段搭配不会有问题,请问是否需要强制转换字段类型才能使用该图表?

相关代码及数据

data = {'inc_open_hours_bucket': ['No Data', '0_15_min', '15_30_min', '30_45_min', '45_60_min',
                                  '1_1.5_hour', '1.5_2_hour', '2_2.5_hour', '2.5_3_hour',
                                  '3_3.5_hour', '3.5_4_hour', '4_4.5_hour', '4.5_5_hour',
                                  '5_5.5_hour', '5.5_6_hour', '> 6_hour'],
        'inc_volume': [153, 99, 50, 46, 53, 73, 50, 44, 37, 34, 29, 23, 13, 15, 18, 417]}

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

输出结果:

inc_open_hours_bucket  inc_volume
0                No Data         153
1               0_15_min          99
2              15_30_min          50
3              30_45_min          46
4              45_60_min          53
5             1_1.5_hour          73
6             1.5_2_hour          50
7             2_2.5_hour          44
8             2.5_3_hour          37
9             3_3.5_hour          34
10            3.5_4_hour          29
11            4_4.5_hour          23
12            4.5_5_hour          13
13            5_5.5_hour          15
14            5.5_6_hour          18
15              > 6_hour         417

df.info()输出:

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 16 entries, 0 to 15
Data columns (total 2 columns):
inc_open_hours_bucket    16 non-null object
inc_volume               16 non-null int64
dtypes: int64(1), object(1)
memory usage: 336.0+ bytes

绘图代码

# Create jointplot
sns.set(font_scale = 0.5)
ax = sns.jointplot(x='inc_open_hours_bucket',y='inc_volume',data=df,kind='hex')
# Add attributes
plt.title("INC by inc_open_hours_bucket")
plt.show()
# Save image
ax_Figure = ax.get_figure()
# Set to high resolution and save
ax_Figure.savefig('12. INC by inc_open_hours_bucket.png', dpi=300) 

解决方案

错误原因

jointplot的kind='hex'(六边形分箱图)要求两个轴的字段都必须是数值类型——它的核心逻辑是对数据进行空间分箱并统计密度,而字符串类型的分类字段无法参与数值化的分箱计算,因此触发类型错误。

处理方法

方法1:改用适配分类-数值关系的图表类型

如果你的核心需求是展示分类字段与数值字段的关联,hex类型的jointplot并不适配,推荐使用更匹配的图表:

  • 箱线图:sns.boxplot(x='inc_open_hours_bucket', y='inc_volume', data=df)
  • 条形图:sns.barplot(x='inc_open_hours_bucket', y='inc_volume', data=df)
  • 散点型jointplot:将kind='hex'改为kind='scatter',分类字段会被自动映射为索引数值,但视觉效果不如专门的分类图表直观。

方法2:将分类字段映射为数值索引(仅需保留hex图时)

若一定要使用hex类型的jointplot,可以手动将分类字段转换为对应的数值索引:

# 为分类字段创建数值映射
df['bucket_idx'] = df['inc_open_hours_bucket'].astype('category').cat.codes

# 使用数值索引绘制hex图
sns.set(font_scale = 0.5)
ax = sns.jointplot(x='bucket_idx', y='inc_volume', data=df, kind='hex')

# 将x轴刻度替换为原分类标签
ax.ax_joint.set_xticks(range(len(df['inc_open_hours_bucket'].unique())))
ax.ax_joint.set_xticklabels(df['inc_open_hours_bucket'].unique(), rotation=45)

plt.title("INC by inc_open_hours_bucket")
plt.show()
ax.get_figure().savefig('12. INC by inc_open_hours_bucket.png', dpi=300)

注:这种方式本质是用分类的索引值替代原分类,hex图展示的是索引与数值的密度分布,仅在需要这种密度统计场景下有意义,否则优先推荐方法1。


内容的提问来源于stack exchange,提问作者Peter Lucas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 20:48:22