【问题标题】:How can I change the values on Y axis of Histogram plot in Python如何在 Python 中更改直方图 Y 轴上的值
【发布时间】:2020-12-26 02:51:43
【问题描述】:

我在 CSV 文件中有数据。我正在尝试使用 matplotlib 绘制直方图。 这是我正在尝试的代码。

data.hist(bins=10)
plt.ylabel('Frequency')
plt.xlabel('Data')
plt.show()

这是我得到的情节。 现在使用相同的代码,我需要创建一个显示数据概率分布的标准化直方图。但是现在在 y 轴上,您将绘制每个 bin 中的数据点数除以数据点总数,而不是绘制每个 bin 中的数据点数。

我该怎么做?

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    Pandas' histogram 为底层pyplot.hist() 添加了一些功能。很多参数都是通过的。其中之一是density=

    import pandas as pd
    import numpy as np
    from matplotlib import pyplot as plt
    
    data = pd.DataFrame(np.random.uniform(258.1, 262.3, 20))
    data.hist(bins=10, density=True)
    plt.ylabel('Density')
    plt.xlabel('Data')
    plt.show()
    

    一个相关的库 seaborn 有一个命令来创建一个 density histogram 和一个 kde curve 作为概率分布的近似值。

    import seaborn as sns
    sns.distplot(data, bins=10)
    

    【讨论】:

      猜你喜欢
      • 2013-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-17
      • 2017-11-17
      • 2023-03-24
      相关资源
      最近更新 更多