[英]plot a bar chart from a selected column in matplotlib
If I have a variable hotel_type
from my DataFrame
and I count for the number of each different hotel_type
如果我有一个变量
hotel_type
从我的DataFrame
和我算每个不同的数量hotel_type
The following command: 以下命令:
`df.hotel_type.value_counts()`
gives the result: 给出结果:
Apartment 3157
Hotel 115
Villa 54
Bed and Breakfast 48
Holiday home 21
Question: How can I plot a bar chart and histogram with x-axis : hotel type and y-axis : the number of each hotel type by using matplotlib
? 问题:如何通过使用
matplotlib
绘制带有x轴:酒店类型和y轴:每种酒店类型的数量的条形图和直方图?
Try with Series.plot.barh : 尝试使用Series.plot.barh :
df.hotel_type.value_counts().plot.barh()
Update: 更新:
import numpy as np
import matplotlib.pyplot as plt
a = df.hotel_type.value_counts()
x = list(a.index)
y = list(a)
fig, ax = plt.subplots()
width = 0.75 # the width of the bars
ind = np.arange(len(y)) # the x locations for the groups
ax.barh(ind, y, width, color="blue")
ax.set_yticks(ind+width/2)
ax.set_yticklabels(x, minor=False)
for i, v in enumerate(y):
ax.text(v + .25, i + .25, str(v), color='blue', fontweight='bold') #add value labels into bar
plt.title('title')
plt.xlabel('x')
plt.ylabel('y')
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.