Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.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_Histogram - Fatal编程技术网

Python 具有非数值数据的Matplotlib直方图

Python 具有非数值数据的Matplotlib直方图,python,matplotlib,histogram,Python,Matplotlib,Histogram,无法使用非数字数据在Matplotlib中绘制直方图 A=na,R,O,na,na,O,R A是一个数据帧,它有3个不同的值:na、R、O 我尝试: plt.hist(A, bins=3, color='#37777D') 你会期待这样的事情吗 它适用于数值数据,但对于非数值数据,我得到以下错误: --------------------------------------------------------------------------- ValueError

无法使用非数字数据在Matplotlib中绘制直方图

A=na,R,O,na,na,O,R

A是一个数据帧,它有3个不同的值:na、R、O

我尝试:

plt.hist(A, bins=3, color='#37777D')
你会期待这样的事情吗

它适用于数值数据,但对于非数值数据,我得到以下错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-44-60369a6f9af4> in <module>
      1 A = dataset2.iloc[:, 2 - 1].head(30)
----> 2 plt.hist(A, bins=3, histtype='bar', color='#37777D')

C:\Anaconda\lib\site-packages\matplotlib\pyplot.py in hist(x, bins, range, density, weights, cumulative, bottom, histtype, align, orientation, rwidth, log, color, label, stacked, normed, data, **kwargs)
   2657         align=align, orientation=orientation, rwidth=rwidth, log=log,
   2658         color=color, label=label, stacked=stacked, normed=normed,
-> 2659         **({"data": data} if data is not None else {}), **kwargs)
   2660 
   2661 

C:\Anaconda\lib\site-packages\matplotlib\__init__.py in inner(ax, data, *args, **kwargs)
   1808                         "the Matplotlib list!)" % (label_namer, func.__name__),
   1809                         RuntimeWarning, stacklevel=2)
-> 1810             return func(ax, *args, **kwargs)
   1811 
   1812         inner.__doc__ = _add_data_doc(inner.__doc__,

C:\Anaconda\lib\site-packages\matplotlib\axes\_axes.py in hist(self, x, bins, range, density, weights, cumulative, bottom, histtype, align, orientation, rwidth, log, color, label, stacked, normed, **kwargs)
   6563                     "color kwarg must have one color per data set. %d data "
   6564                     "sets and %d colors were provided" % (nx, len(color)))
-> 6565                 raise ValueError(error_message)
   6566 
   6567         # If bins are not specified either explicitly or via range,

ValueError: color kwarg must have one color per data set. 30 data sets and 1 colors were provided
---------------------------------------------------------------------------
ValueError回溯(最近一次调用上次)
在里面
1a=dataset2.iloc[:,2-1]。头(30)
---->2 plt.hist(A,bin=3,histtype='bar',color='37777D')
C:\Anaconda\lib\site packages\matplotlib\pyplot.py in hist(x、箱子、范围、密度、重量、累计、底部、历史类型、对齐、方向、rwidth、日志、颜色、标签、堆叠、规范化、数据,**kwargs)
2657 align=align,orientation=orientation,rwidth=rwidth,log=log,
2658颜色=颜色,标签=标签,堆叠=堆叠,标准化=标准化,
->2659**({“数据”:数据}如果数据不是其他{}),**kwargs)
2660
2661
C:\Anaconda\lib\site packages\matplotlib\\uuuu init\uuuuuu.py在内部(ax、数据、*args、**kwargs)
1808“Matplotlib列表!)”%(标签名称,函数名称),
1809运行时警告,堆栈级别=2)
->1810返回函数(ax,*args,**kwargs)
1811
1812内部.\uuuuu文档\uuuuu=\u添加数据\uu文档(内部.\uuuuu文档\uuuuuuuuuu),
C:\Anaconda\lib\site packages\matplotlib\axes\\u axes.py(self、x、bins、range、density、weights、累计、底部、histtype、align、orientation、rwidth、log、color、label、stacked、normed、**kwargs)
6563“颜色kwarg每个数据集必须有一种颜色。%d个数据”
提供了6564“套和%d种颜色”%(nx,len(颜色)))
->6565提升值错误(错误消息)
6566
6567#如果未明确或通过范围指定存储箱,
ValueError:color kwarg每个数据集必须有一种颜色。提供了30个数据集和1种颜色

我想你需要的是条形图而不是柱状图。此外,还不清楚你的价值观是什么。考虑到它们是字符串(基于曲线图),您需要首先使用例如
计数器
模块来计算它们的频率。然后,您可以绘制频率并将关键点的名称指定为记号标签

from collections import Counter
from matplotlib import pyplot as plt

A = ['na', 'R', 'O', 'na', 'na', 'R']

freqs = Counter(A)

xvals = range(len(freqs.values()))
plt.bar(xvals, freqs.values() , color='#37777D')
plt.xticks(xvals, freqs.keys())
plt.show() 

这是不可复制的。但是如果我们创建一个数据帧并运行以下代码

import numpy as np; np.random.seed(42)
import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame(np.random.choice(["na", "O", "A"], size=10))

plt.hist(df.values, histtype='bar', bins=3)

plt.show()

无论如何,这可能不是最好的选择,因为直方图是连续的。所以我们可以创建一个计数的条形图

import numpy as np; np.random.seed(42)
import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame(np.random.choice(["na", "O", "A"], size=10))

counts = df[0].value_counts()
plt.bar(counts.index, counts.values)

plt.show()

如果绘制非数字数据,您希望看到什么?一个直方图,包含3个不同选项的分布:1个条形图包含na的数量,另一个条形图包含O的数量,最后一个条形图包含R的数量。