Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用值_计数构建图表_Python_Pandas_Matplotlib_Graph - Fatal编程技术网

Python 使用值_计数构建图表

Python 使用值_计数构建图表,python,pandas,matplotlib,graph,Python,Pandas,Matplotlib,Graph,我有以下数据帧: df_Mechanics = pd.DataFrame({'Car Plate End': [749, 749, 749,749, 749, 749, 749, 749, 749, 49], 'Car Model': ['Mustang', 'Mustang', 'Mustang', 'Mustang', 'Mus

我有以下数据帧:

       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()
图表如预期的那样。但我想完善你的设计师

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

我很难理解

我想让我的图表水平,x轴高达100%,概率出现在条形图中。感谢收听。

您可以使用barh而不是bar作为水平条,然后提取修补程序以使用ax.text进行注释:

输出:


非常感谢。您知道如何将x轴变换到100%吗?请参阅中的说明。我可以按如下方式更改x轴比例:ax=prob*100.plotkind='barh'但是,条形图标题不见了。你知道如何解决这个问题吗?prob.plotkind='barh',figzise=10,6?。
# 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')