Python 从matplotlib中的选定列打印条形图
如果我从我的Python 从matplotlib中的选定列打印条形图,python,matplotlib,dataframe,histogram,bar-chart,Python,Matplotlib,Dataframe,Histogram,Bar Chart,如果我从我的DataFrame中有一个变量hotel\u type,并且我计算每个不同hotel\u type 以下命令: `df.hotel_type.value_counts()` 给出了结果: Apartment 3157 Hotel 115 Villa 54 Bed and Breakfast 48 Holiday home 21 问题:如何使用matplotlib绘
DataFrame
中有一个变量hotel\u type
,并且我计算每个不同hotel\u type
以下命令:
`df.hotel_type.value_counts()`
给出了结果:
Apartment 3157
Hotel 115
Villa 54
Bed and Breakfast 48
Holiday home 21
问题:如何使用matplotlib
绘制带有x轴:酒店类型和y轴:每个酒店类型的数量的条形图和柱状图?尝试:
更新:
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()
谢谢您知道如何在条形图中添加值吗?以及如何使用
matplotlib
?我看到了。非常感谢。
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()