python - 生成以 y 轴作为相对频率的直方图?

标签 python matplotlib statistics histogram

今天我的任务是生成一个直方图,其中 y 轴是相对频率而不仅仅是绝对计数。我找到了与此相关的另一个问题(请参阅: Setting a relative frequency in a matplotlib histogram )但是,当我尝试实现它时,我收到错误消息:

'list'对象没有属性大小

尽管答案中给出了完全相同的代码 - 并且尽管它们的信息也存储在列表中。

另外,我尝试了这里的方法( http://www.bertplot.com/visualization/?p=229 ),但没有效果,因为输出仍然没有显示 y 标签的范围从 0 到 1。

import numpy as np
import matplotlib.pyplot as plt
import random
from tabulate import tabulate
import matplotlib.mlab as mlab

precision = 100000000000

def MarkovChain(n,s) :
    """

  """
    matrix = []
    for l in range(n) :
        lineLst = []
        sum = 0
        crtPrec = precision
        for i in range(n-1) :
            val = random.randrange(crtPrec)
            sum += val
            lineLst.append(float(val)/precision)
            crtPrec -= val
        lineLst.append(float(precision - sum)/precision)
        matrix2 = matrix.append(lineLst)

    print("The intial probability matrix.")    
    print(tabulate(matrix2))
    baseprob = []
    baseprob2 = []
    baseprob3 = []
    baseprob4 = []

    for i in range(1,s): #changed to do a range 1-s instead of 1000

        #must use the loop variable here, not s (s is always the same)
        matrix_n = np.linalg.matrix_power(matrix2, i)
        baseprob.append(matrix_n.item(0))
        baseprob2.append(matrix_n.item(1))
        baseprob3.append(matrix_n.item(2))

    baseprob = np.array(baseprob)
    baseprob2 = np.array(baseprob2)
    baseprob3 = np.array(baseprob3)
    baseprob4 = np.array(baseprob4)

    # Here I tried to make a histogram using the plt.hist() command, but the normed=True doesn't work like I assumed it would.
    '''    
  plt.hist(baseprob, bins=20, normed=True)
  plt.show()
  '''

    #Here I tried to make a histogram using the method from the second link in my post.
    # The code runs, but then the graph that is outputted isn't doesn't have the relative frequency on the y axis.
    '''
   n, bins, patches = plt.hist(baseprob, bins=30,normed=True,facecolor = "green",)
   y = mlab.normpdf(bins,mu,sigma)
   plt.plot(bins,y,'b-')
   plt.title('Main Plot Title',fontsize=25,horizontalalignment='right')
   plt.ylabel('Count',fontsize=20)
   plt.yticks(fontsize=15)
   plt.xlabel('X Axis Label',fontsize=20)
   plt.xticks(fontsize=15)
   plt.show()
   '''
    # Here I tried to make a histogram using the method seen in the Stackoverflow question I mentioned.
    # The figure that pops out looks correct in terms of the axes, but no actual data is posted. Instead the error below is shown in the console.
    # AttributeError: 'list' object has no attribute 'size'


    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.hist(baseprob, weights=np.zeros_like(baseprob)+1./ baseprob.size)
    n, bins, patches = ax.hist(baseprob, bins=100, normed=1, cumulative=0)
    ax.set_xlabel('Bins', size=20)
    ax.set_ylabel('Frequency', size=20)
    ax.legend
    plt.show()


    print("The final probability matrix.")
    print(tabulate(matrix_n))
    matrixTranspose = zip(*matrix_n)
    evectors = np.linalg.eig(matrixTranspose)[1][:,0]
    print("The steady state vector is:")
    print(evectors)






MarkovChain(5, 1000)

我尝试的方法都被注释掉了,因此要重现我的错误,请确保删除注释标记。

正如你所知,我对编程真的很陌生。而且这不是计算机科学课上的家庭作业,因此仅仅向我提供代码不存在道德问题。

最佳答案

matplotlib 函数的预期输入通常是 numpy 数组,其具有方法nparray.size。列表没有 size 方法,因此当在 hist 函数中调用 list.size 时,这会导致错误。您需要使用nparray = np.array(list)进行转换。您可以在使用附加功能构建列表的循环之后执行此操作,例如,

baseprob = []
baseprob2 = []
baseprob3 = []
baseprob4 = []

for i in range(1,s): #changed to do a range 1-s instead of 1000

 #must use the loop variable here, not s (s is always the same)
     matrix_n = numpy.linalg.matrix_power(matrix, i)
     baseprob.append(matrix_n.item(0))
     baseprob2.append(matrix_n.item(1))
     baseprob3.append(matrix_n.item(2))

 baseprob = np.array(baseprob)
 baseprob2 = np.array(baseprob2)
 baseprob3 = np.array(baseprob3)
 baseprob4 = np.array(baseprob4)

编辑:最小的历史示例

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)
baseprob = np.random.randn(1000000)
ax.hist(baseprob, weights=np.zeros_like(baseprob)+1./ baseprob.size, bins=100)
n, bins, patches = ax.hist(baseprob, bins=100, normed=1, cumulative=0, alpha = 0.4)
ax.set_xlabel('Bins', size=20)
ax.set_ylabel('Frequency', size=20)
ax.legend
plt.show()

这给出了,

enter image description here

关于python - 生成以 y 轴作为相对频率的直方图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31577272/

相关文章:

python - pandas内部合并两个不同大小的df,检查一个df的值是否在第二个df的范围内并将其存储到另一个df中

Python 使用 matplotlib 绘制日期

r - 如何在 R 中计算变差函数时更改滞后距离

python - 使用 pandas 中的方差范围对列进行分组

python - numpy polyfit 中使用的权重值是多少,拟合误差是多少

python - pandas Python Series 对象是可变的,因此它们不能在查询方法中散列

python - 使用 Django Rest Framework 时如何获取初始 CSRF token

python - 在 matplotlibrc Ubuntu 中导入 Open Sans 字体失败

python - yaxis 上的 HourLocator() 引发运行时错误意外超过 Locator.MAXTICK

python - 如何找到两个不同音频样本之间的标度?