我有以下数据帧:
df_Mechanics = pd.DataFrame({'Car Plate End': [749, 749, 749,749, 749,
749, 749, 749, 749, 49],
'Car Model': ['Mustang', 'Mustang', 'Mustang', 'Mustang', 'Mustang',
'Mustang', 'Mustang', 'Mustang', 'Mustang', 'Mustang'],
'Replaced part': ['brake pad', 'wheel', 'engine', 'engine', 'engine',
'wheel', 'engine','engine', 'engine', 'engine']
})
print(df_Mechanics)
Car Plate End Car Model Replaced part
749 Mustang brake pad
749 Mustang wheel
749 Mustang engine
749 Mustang engine
749 Mustang engine
749 Mustang wheel
749 Mustang engine
749 Mustang engine
749 Mustang engine
749 Mustang engine我想画一张已经使用过的零件的概率图。因此,我做了以下工作:
prob = df_Mechanics['Replaced part'].value_counts(normalize=True)
threshold = 0.05
mask = prob > threshold
tail_prob = prob.loc[~mask].sum()
prob = prob.loc[mask]
prob.plot(kind='bar')
plt.xticks(rotation=25)
plt.show()该图与预期一致。但我想让你的设计师更完美。
我在这个链接上找到了一个非常好的例子:https://matplotlib.org/3.2.1/gallery/statistics/barchart_demo.html
我听不懂。
我想让我的图形水平,x轴达到100%,并且概率显示在条形图中。谢谢你的倾听。
发布于 2020-05-20 20:35:27
您可以使用barh代替bar作为水平条形图,然后提取要使用ax.text注记的面片
# replace prob.plot(...) with the following
ax = prob.plot(kind='barh')
# loop through the bars
for patch in ax.patches:
# extract bar's information
x = patch.get_width()
bar_width=patch.get_height()
y = patch.get_y()
# annotate the bar
ax.text(x-.05, y+bar_width/2, f'{x:.0%}',
verticalalignment='center',
color='white')输出:

https://stackoverflow.com/questions/61913104
复制相似问题