Python Pandas - 绘制包含所有列的分组水平条形图


对于包含所有列的分组水平条形图,使用 barh() 创建条形图,并且不要设置 a 和 y 值。

首先,导入所需的库 -

import pandas as pd
import matplotlib.pyplot as plt

使用 3 列创建 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],
})

绘制包含所有列的分组水平条形图 -

dataFrame.plot.barh(title='Car Specifications', color=("blue", "orange"))

示例

以下是完整代码 -

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],
})

# plotting grouped Horizontal Bar Chart with all the columns
dataFrame.plot.barh(title='Car Specifications', color=("blue", "orange"))

# display the plotted Horizontal Bar Chart
plt.show()

输出

将生成以下输出 -

更新时间:2021 年 9 月 28 日

超过 1K 次浏览

开启您的 职业生涯

完成课程即可获得认证

开始学习
广告
© . All rights reserved.