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

Azure环境Jupyter Notebook用Seaborn绘图报TypeError错误

TypeError: unsupported operand type(s) for /: 'str' and 'int' when plotting horizontal bar chart with Seaborn

问题场景

你在Azure的Jupyter Notebook里结合App Insights做数据分析,拿到了这样的数据集:

axes = {'xaxis': ['2018-04-15T03:00:00Z', '2018-04-15T07:00:00Z'], 'yaxis': [62, 26]}

然后你用Seaborn绘制水平柱状图,代码如下:

import seaborn as sns #modern visualization and plotting
sns.barplot(y = axes['yaxis'], x = axes['xaxis'], orient='h');

结果抛出了错误:TypeError: unsupported operand type(s) for /: 'str' and 'int'

问题原因

这个错误的核心是参数顺序搞反了!当你设置orient='h'(水平方向)时,Seaborn的barplot有明确的参数要求:

  • x需要传入数值型数据(用来控制柱状条的长度)
  • y需要传入分类/字符串型数据(用来作为纵轴的分组标签)

你之前把字符串类型的时间数据传给了x,数值数据传给了y,Seaborn在计算柱状条布局时,会尝试对数值做比例运算,但字符串无法参与这类数值操作,自然就抛出了类型不兼容的错误。

解决方案

只需要把x和y的参数值对调即可,修正后的代码如下:

import seaborn as sns
sns.barplot(x = axes['yaxis'], y = axes['xaxis'], orient='h');

这样设置后,数值型的yaxis会作为水平柱状条的长度(对应x轴),字符串型的时间会作为纵轴的分组标签,Seaborn就能正确渲染出你想要的水平柱状图了。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 03:57:48