首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用value_counts构建图表

使用value_counts构建图表
EN

Stack Overflow用户
提问于 2020-05-20 20:25:58
回答 1查看 70关注 0票数 0

我有以下数据帧:

代码语言:javascript
复制
       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

我想画一张已经使用过的零件的概率图。因此,我做了以下工作:

代码语言:javascript
复制
       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%,并且概率显示在条形图中。谢谢你的倾听。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-20 20:35:27

您可以使用barh代替bar作为水平条形图,然后提取要使用ax.text注记的面片

代码语言:javascript
复制
# 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')

输出:

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61913104

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档