如何使用matplotlib.pyplot在Python中绘制条形图?参数高度必须为< xxxx'或标量

时间:2017-08-20 18:10:50

标签: python matplotlib plot bar-chart data-science

我有一个名为' train'的数据框。它有多个变量。其中一个变量是“行业”。专栏'行业的前10个要素'如下:

train['industry'][:10]
0    Office supplies    
1    Unknown            
2    Misc services      
3    Social services    
4    Unknown            
5    Manufacturing      
6    Social services    
7    Office supplies    
8    Entertainment      
9    Construction       
Name: industry, dtype: object

我正在尝试使用matplotlib.pyplot作为plt库绘制条形图,其中x轴为行业类型,y轴为频率。我不确定应该是什么应该是'身高'争论?

plt.bar(train['industry'], height = )

1 个答案:

答案 0 :(得分:0)

身高是你的频率。这是example

  1. 制作长度为len(train.index)

  2. 的数组
  3. 制作没有行业名称的条形图 - plt.bar(array, frequency)。频率/高度不能是字符串。它需要是数字的。如果不是,请使用pd.to_numeric

  4. 进行转换
  5. 将行业名称放入数组

  6. 使用set_xticklabels(industries)更改标签

相关问题