Barplot返回不受支持的操作数类型(S) - :“STR”和“浮动”
问题描述:
我尝试绘制pivot_table的柱状图用下面的代码:Barplot返回不受支持的操作数类型(S) - :“STR”和“浮动”
fig=plt.figure(figsize=(10,10))
ax1=fig.add_subplot(111)
ax1.bar(Products.index,Products["Amount"].values)
枢轴表是这样的:
Amount
Material
454T006S R 167 134:11 0.000000
3457T006S R IE216 167 246:1 4.500000
67T009S R 1510K304:1 36.000000
7676009S R IE216 1510K304:1 30.500000
676T012S 167 246:1 1.000000
2324T012S R 1510 111 6.000000
1516T012S R 1510 151 50.500000
1516T012S R 1510 20:1 26.500000
但它返回:
不受支持的操作数类型(一个或多个),用于: - 'STR' 和 '浮动'
我在做什么错了?
答
问题是您的索引值是strings
。
我觉得simpliest是使用pandas.DataFrame.plot.bar
:
Products["Amount"].plot.bar()
可以字符串的值映射到range
,情节范围和最后添加xticks
:
fig=plt.figure(figsize=(10,10))
ax1=fig.add_subplot(111)
idx=Products.index.tolist()
x = range(len(idx))
plt.bar(x, Products["Amount"].values)
plt.xticks(x, idx, rotation=90)
plt.show()
类似的解决方案是reset index
和情节默认index
:
fig=plt.figure(figsize=(10,10))
ax1=fig.add_subplot(111)
Products = Products.reset_index()
plt.bar(Products.index, Products["Amount"].values)
plt.xticks(Products.index, Products['Material'], rotation=90)
plt.show()