首页 > 解决方案 > 如何在条形图中启用条形图上的数字

问题描述

如何在条形图中的每个条形上启用数字。

我无法找到我做错的地方。

注意:使用的 sample-superstore 数据集。

我尝试使用以下代码出现错误,例如“TypeError: unsupported operand type(s) for -: 'str' and 'float'”

` Grouped_data= df.groupby(['Region'],as_index=False).count()

import matplotlib.pyplot as plt
import numpy as np

barWidth = 0.9
x=np.arange(len(Grouped_data))
y=list(Grouped_data['Row ID'])
xx=list(Grouped_data['Region'])
plt.legend()
plt.bar(x,y,width = barWidth,color=['lime','r','k','tan'])
plt.xticks(x,xx,rotation=90)
label=y
for i in range(len(xx)):
    plt.text(x = xx[i]-0.5 , y = y[i]+0.1, s = label[i],size = 6)
plt.subplots_adjust(bottom= 0.2, top = 0.98)
plt.show()

我想查看图表中每个条形的值。

标签: python-3.xmatplotlib

解决方案


我改变了我的状况,然后我得到了想要的结果。

for i in range(len(x)):
plt.text(x = x[i]-0.2 , y = y[i]+0.1, s = label[i],size = 10)

但是需要将值放置在适当的位置


推荐阅读