Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.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 如何绘制两列数据帧';s元素作为直方图?_Python_Python 3.x_Pandas_Matplotlib - Fatal编程技术网

Python 如何绘制两列数据帧';s元素作为直方图?

Python 如何绘制两列数据帧';s元素作为直方图?,python,python-3.x,pandas,matplotlib,Python,Python 3.x,Pandas,Matplotlib,我有以下数据帧: A B 1 3 0 2 1 4 0 1 0 3 我想画出给定A的B仪器的频率,如下所示: | | | __ B | | | |

我有以下数据帧:

    A                    B

    1                    3
    0                    2
    1                    4
    0                    1
    0                    3
我想画出给定A的B仪器的频率,如下所示:

     |
     |
     |        __
 B   |       |  |
     |  ___  |  |
     |  | |  |  |
     |  | |  |  |
     |__|_|__|__|______________
                A
因此,我尝试了以下方法:

df2.groupby([df.A, df.B]).count().plot(kind="bar")
但是,我得到以下例外情况:

TypeError: Empty 'DataFrame': no numeric data to plot

因此,我的问题是,在给定A频率的情况下,如何绘制B中元素的频率?

我不完全确定“在给定A频率的情况下绘制B中元素的频率”是什么意思,但这给出了预期的输出:

In [4]: df
Out[4]: 
      A  B
3995  1  3
3996  0  2
3997  1  4
3998  0  1
3999  0  3

In [8]: df['data'] = df['A']*df['B']

In [9]: df
Out[9]: 
      A  B  data
3995  1  3     3
3996  0  2     0
3997  1  4     4
3998  0  1     0
3999  0  3     0

In [10]: df[['A','data']].plot(kind='bar', x='A', y='data')
Out[10]: <matplotlib.axes._subplots.AxesSubplot at 0x7fde7eebb9e8>

In [11]: plt.show()
[4]中的
:df
出[4]:
A B
3995  1  3
3996  0  2
3997  1  4
3998  0  1
3999  0  3
在[8]中:df['data']=df['A']*df['B']
In[9]:df
出[9]:
A B数据
3995  1  3     3
3996  0  2     0
3997  1  4     4
3998  0  1     0
3999  0  3     0
在[10]中:df[['A','data']].绘图(kind='bar',x='A',y='data')
出[10]:
[11]:plt.show()

我相信如果您试图绘制b列中值的出现频率,这可能会有所帮助

from collections import Counter
vals = list(df['b'])
cntr = Counter(vals)
# Out[30]: Counter({1: 1, 2: 1, 3: 2, 4: 1})

vals = [(key,cntr[key]) for key in cntr]
x = [tup[0] for tup in vals]
y = [tup[1] for tup in vals]

plt.bar(x,y,label='Bar1',color='red')
plt.show()

另一种方法是使用
matplotlib
中的
histogram
。 首先声明一个bins数组,它基本上是一个bucket,您的值将被放入其中

import matplotlib.pyplot as plt
import pandas as pd

l = [(1,3),(0,2),(1,4),(0,1),(0,3)]
df = pd.DataFrame(l)

df.columns = ['a','b']
bins = [1,2,3,4,5] #ranges of data
plt.hist(list(df['b']),bins,histtype='bar',rwidth=0.8)
这是我的方式:

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame([[1,3],[0,2],[1,4],[0,1],[0,3]])
df.columns = ['A', 'B']
x = df.loc[:,'A'].values
y = df.loc[:,'B'].values
plt.bar(x, y, label = 'Bar', align='center',)
plt.xticks(x)
plt.show()

听起来这就是你想要的: 你可以用

如果不希望对
值\u count
进行排序,可以执行以下操作:

print(df['B'].value_counts(sort=False).plot(kind='bar'))

谢谢你的帮助,我想我把你弄糊涂了,因为3995、3996、3997、3998、3999只是索引号,我只想画A和B列,我编辑了这个问题。@johndoe仍然,你所期望的和我答案中的截图有什么区别?你只想用0和1来代替3995..3999索引吗?是的,我想把A列的值画成x轴,把B列的值画成y轴。谢谢你的帮助谢谢你的帮助,这就是我的本意expected@john_doe当前位置我看到你把我的答案标对了,然后把它删除了。你为什么这么做?我还是很困惑你想要什么。。。您是只想将A绘制为x轴,将B绘制为y轴,而不考虑它们的值,还是希望绘制类似于ASCII图形的图形?意思是B x A?谢谢你们的帮助,我真的很感谢你们的时间。我本想用梅农的方法来解决这个问题。
print(df['B'].value_counts(sort=False).plot(kind='bar'))