首页 > 解决方案 > matplotlib 中的直方图未正确分箱

问题描述

我正在尝试从一些自举数据创建直方图,但我的直方图没有正确分箱。它没有显示频率,而是将每个点绘制为自己的 bin。

每个点都是它自己的颜色,所以我不确定它是否将每个条目作为自己的系列。

这是我的代码:

 import pip
import xlrd as xlrd
import numpy as np
import plotly.plotly as py
import plotly.graph_objs as go
import matplotlib
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt

import pandas as pd
from pandas import DataFrame


exams212 = pd.read_excel('212bare.xlsx')
df = DataFrame(exams212)

mc =df.query('Gender == "M" and Treatment == "C"')
mt =df.query('Gender == "M" and Treatment == "T"')
fc =df.query('Gender == "F" and Treatment == "C"')
ft=df.query('Gender == "F" and Treatment == "T"')


#MC averages
mcave = []
for x in range(10):

    rows = mc.sample(n=381, replace=True)
    mcavg=rows.mean()

    mcave.append(mcavg)

print(mcave)

mcplot=plt.hist(mcave,bins=5)
plt.show(mcplot)

我最终会为更大的数字这样做,但我现在一直在玩十个,只是为了排除故障,试图强制 5 个垃圾箱,看看我是否可以让它做我想做的事。

我的打印结果如我所料:

[P212 EX1    0.73931
dtype: float64, P212 EX1    0.72205
dtype: float64, P212 EX1    0.71315
dtype: float64, P212 EX1    0.717682
dtype: float64, P212 EX1    0.730556
dtype: float64, P212 EX1    0.730562
dtype: float64, P212 EX1    0.726436
dtype: float64, P212 EX1    0.736711
dtype: float64, P212 EX1    0.749921
dtype: float64, P212 EX1    0.74974
dtype: float64]

但我得到的图看起来像这样,有 10 个 bin,每个数据点一个: histogram not binned

我错过了什么愚蠢的东西吗?我一直试图弄清楚这一点,但找不到任何东西。谢谢!

标签: pythonmatplotlib

解决方案


推荐阅读