Matplotlib无法绘制分类值 [英] Matplotlib cannot plot categorical values

查看:101
本文介绍了Matplotlib无法绘制分类值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的示例:

  import matplotlib.pyplot as plt 
test_list = ['a',' b','b','c']
plt.hist(test_list)
plt.show()

它会生成以下错误消息:

  TypeError Traceback(最近一次通话)
< ipython-input-48-228f7f5e9d1e>在< module>()
1 test_list = ['a','b','b','c']
----> 2 plt.hist(test_list)
3 plt.show()

C:\anaconda3\lib\site-packages\matplotlib\pyplot.py in hist(x ,bins,范围,normed,权重,累积值,底部,histt​​ype,align,orientation,rwidth,log,color,label,stacked,hold,data,** kwargs)
2956 histt​​ype = histt​​ype,align = align,方向=方向,
2957 rwidth = rwidth,log = log,颜色=颜色,label =标签,
-> 2958 stacked = stacked,data = data,** kwargs)
2959最终:
2960 ax.hold(washold)

C:\Anaconda3\lib\site -packages\matplotlib\__init __。py在内部(ax,* args,** kwargs)
1809 warnings.warn(msg%(label_namer,func .__ name__),
1810 RuntimeWarning,stacklevel = 2)
-> 1811 return func(ax,* args,** kwargs)
1812 pre_doc =内部.__ doc__
1813如果pre_doc为None:

C:\Anaconda3\lib\ hist中的站点软件包 matplotlib axes _axes.py(自身,x,bin,范围,归一化,权重,累积,底部,直方图,对齐,方向,rwidth,log,颜色,标签,堆叠式, ** kwargs)
5993 xmax = -np.inf
5994 for x in xi:
->如果len(xi)> 5995 0:
5996 xmin = min(xmin,xi.min())
5997 xmax = max(xmax,xi.max())

TypeError:len()of未缩放的对象

我只在Google上进行了短暂搜索,但看起来我无法在其中绘制类别变量的直方图matplotlib。



任何人都可以确认吗?

解决方案

可以创建matplotlib中类别数据的直方图。



由于此直方图,并且它是 用matplotlib创建的,如果说您无法在matplotlib中为分类变量绘制直方图,则肯定是错误的。


Here is my example:

import matplotlib.pyplot as plt
test_list = ['a', 'b', 'b', 'c']
plt.hist(test_list)    
plt.show()

It generates the following error message:

TypeError                                 Traceback (most recent call last)
<ipython-input-48-228f7f5e9d1e> in <module>()
      1 test_list = ['a', 'b', 'b', 'c']
----> 2 plt.hist(test_list)
      3 plt.show()

C:\Anaconda3\lib\site-packages\matplotlib\pyplot.py in hist(x, bins, range, normed, weights, cumulative, bottom, histtype, align, orientation, rwidth, log, color, label, stacked, hold, data, **kwargs)
   2956                       histtype=histtype, align=align, orientation=orientation,
   2957                       rwidth=rwidth, log=log, color=color, label=label,
-> 2958                       stacked=stacked, data=data, **kwargs)
   2959     finally:
   2960         ax.hold(washold)

C:\Anaconda3\lib\site-packages\matplotlib\__init__.py in inner(ax, *args, **kwargs)
   1809                     warnings.warn(msg % (label_namer, func.__name__),
   1810                                   RuntimeWarning, stacklevel=2)
-> 1811             return func(ax, *args, **kwargs)
   1812         pre_doc = inner.__doc__
   1813         if pre_doc is None:

C:\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py in hist(self, x, bins, range, normed, weights, cumulative, bottom, histtype, align, orientation, rwidth, log, color, label, stacked, **kwargs)
   5993             xmax = -np.inf
   5994             for xi in x:
-> 5995                 if len(xi) > 0:
   5996                     xmin = min(xmin, xi.min())
   5997                     xmax = max(xmax, xi.max())

TypeError: len() of unsized object

I only briefly search on google, but it looks like I cannot plot histogram for categorical variables in matplotlib.

Can anybody confirm?

解决方案

Sure it is possible to create a histogram of categorial data in matplotlib. As this link suggests, but also as suggested in the matplotlib demo, simply use a barchart for that purpose.

import matplotlib.pyplot as plt
import numpy as np

test_list = ['a', 'b', 'b', 'c', "d", "b"]
histdic = {x: test_list.count(x) for x in test_list}
x = []; y=[]
for key, value in histdic.iteritems():
    x.append(key)
    y.append(value)

plt.figure()
barwidth= 0.8
plt.bar(np.arange(len(y)),y, barwidth, color='r')
plt.gca().set_xticks(np.arange(len(y))+barwidth/2.)
plt.gca().set_xticklabels(x)
plt.show()

Since this is a histogram and it is created with matplotlib, it's definitely wrong to say that you "cannot plot histogram for categorical variables in matplotlib".

这篇关于Matplotlib无法绘制分类值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆