Python Pandas - 创建一个水平条形图


要绘制水平条形图,请使用 pandas.DataFrame.plot.barh。条形图显示离散类别之间的比较。

首先,导入必要的库 -

import pandas as pd import matplotlib.pyplot as plt

使用 4 列创建 Pandas DataFrame -

dataFrame = pd.DataFrame({"Car": ['Bentley', 'Lexus', 'BMW', 'Mustang', 'Mercedes', 'Jaguar'],"Cubic_Capacity": [2000, 1800, 1500, 2500, 2200, 3000],"Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000],"Units_Sold": [ 100, 110, 150, 80, 200, 90] })

使用 plot.barh() 绘制水平条形图 -

dataFrame.plot.barh(x='Car', y='Cubic_Capacity', title='Car Specifications', color='blue')

示例

以下为完整代码 -

import pandas as pd import matplotlib.pyplot as plt # creating dataframe dataFrame = pd.DataFrame({"Car": ['Bentley', 'Lexus', 'BMW', 'Mustang', 'Mercedes', 'Jaguar'],"Cubic_Capacity": [2000, 1800, 1500, 2500, 2200, 3000],"Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000],"Units_Sold": [ 100, 110, 150, 80, 200, 90] }) # plotting Horizontal Bar Chart dataFrame.plot.barh(x='Car', y='Cubic_Capacity', title='Car Specifications', color='blue') # set the label plt.xlabel("CC (Cubic Capacity)" ) # display the plotted Horizontal Bar Chart plt.show()

输出

将生成以下输出 -

上次更新:01-Oct-2021

794 次浏览

开启你的 职业生涯

完成课程即获得认证

开始行动
广告