Python Pandas - 绘制水平堆叠条形图


对于堆叠水平条形图,使用 barh() 创建条形图,并将参数“stacked”设置为 True

Stacked = True

首先,导入所需库 −

import pandas as pd
import matplotlib.pyplot as plt

创建一个包含 3 列的数据框 −

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(stacked=True, title='Car Specifications', color=("orange", "cyan"))

示例

以下是完整代码 −

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 stacked Horizontal Bar Chart with all the columns
dataFrame.plot.barh(stacked=True, title='Car Specifications', color=("orange", "cyan"))

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

输出

这将产生以下输出 −

更新日期: 01-Oct-2021

2K+ 次浏览

开始您的职业生涯

完成课程并获得认证

开始
广告
© . All rights reserved.