Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从matplotlib中的选定列打印条形图_Python_Matplotlib_Dataframe_Histogram_Bar Chart - Fatal编程技术网

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()