我想使用 matplotlib 将值添加到堆积条形图。到目前为止,我已经能够创建堆积条形图,但我对如何添加注释感到困惑。
我想要一个类似的输出,而不是整个图表,而只是中间的注释。
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
data = {'Range':['<10','>10', '>= 20', '<10','>10', '>= 20', '<10','>10', '>= 20'],
'Price':[50,25,25,70,20,10,80,10,10]
'Value':[100,50,50,140,40,20,160,20,20]}
df1 = pd.DataFrame(data)
b1 = df1[(df1['Range'] == '<10']['Price']
b2 = df1[df1['Range'] == '>10']['Price']
b3 = df1[df1['Range'] == '>= 20']['Price']
totals = [i+j+k for i,j,k in zip(b1,b2,b3)]
greenBars = [i / j * 100 for i,j in zip(b1, totals)]
orangeBars = [i / j * 100 for i,j in zip(b2, totals)]
blueBars = [i / j * 100 for i,j in zip(b3, totals)]
barWidth = 0.5
names = ('low', 'medium', 'high')
r = [0,1,2]
plt.bar(r, greenBars, color='#b5ffb9', edgecolor='white', width=barWidth, label = '$<10')
plt.bar(r, orangeBars, bottom=greenBars, color='#f9bc86', edgecolor='white', width=barWidth, label = '$>10')
plt.bar(r, blueBars, bottom=[i+j for i,j in zip(greenBars, orangeBars)], color='#a3acff', edgecolor='white', width=barWidth, label = '$>=20')
plt.xticks(r, names)
plt.xlabel("group")
plt.legend(loc='upper left', bbox_to_anchor=(1,1), ncol=1)
plt.show()
添加了上面的代码来创建堆积图。期望的输出:
对于低类别,通过从列中提取值Value(100、50 和 50)在堆栈上添加注释
对于中值,值为 140、40 和 20。
对于高值,值为 160、20 和 20。
DIEA
相关分类