使用value_counts建立图表 [英] Building charts with value_counts

查看:59
本文介绍了使用value_counts建立图表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据框:

  df_Mechanics = pd.DataFrame({'Car Plate End':[749,749,749,749,749,749,749,749,749,49],汽车型号":['Mustang','Mustang','Mustang','Mustang','Mustang','Mustang','Mustang','Mustang','Mustang','Mustang'],替换零件":[刹车片",车轮",发动机",发动机",发动机",车轮",引擎",引擎",引擎",引擎"]})打印(df_Mechanics)车板端汽车型号替换零件749野马刹车片749野马轮749野马发动机749野马发动机749野马发动机749野马轮749野马发动机749野马发动机749野马发动机749野马发动机 

我想绘制一个已经使用过的零件的概率图.因此,我做了以下事情:

 概率= df_Mechanics ['替换零件'] .value_counts(normalize = True)阈值= 0.05遮罩=概率>临界点tail_prob = prob.loc [〜mask] .sum()概率= prob.loc [掩码]prob.plot(种类='bar')plt.xticks(旋转= 25)plt.show() 

该图形符合预期.但我想完善您的设计师.

我在此链接上找到了一个很好的例子:

I have the following dataframe:

       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

I would like to plot a probability plot of parts that have been used. So, I did the following:

       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()

The graph is as expected. But I would like to perfect your designer.

I found a very good example at this link: https://matplotlib.org/3.2.1/gallery/statistics/barchart_demo.html

I'm having trouble understanding.

I would like to make my graph horizontal, that the x-axis is up to 100% and that the probabilities appear in the bars. Thanks for listening.

解决方案

You can use barh instead of bar for horizontal bar, then extract the patches to annotate with 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')

Output:

这篇关于使用value_counts建立图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆