【问题标题】:2D histogram where one axis is cumulative and the other is not一个轴是累积的,另一个不是累积的二维直方图
【发布时间】:2021-10-01 21:31:55
【问题描述】:

假设我有两个可以视为配对的随机变量的实例。

import numpy as np
x = np.random.normal(size=1000)
y = np.random.normal(size=1000)

使用 matplotlib 很容易制作 2D 直方图。

import matplotlib.pyplot as plt
plt.hist2d(x,y)

在 1D 中,matplotlib 具有使直方图累积的选项。

plt.hist(x,cumulative=True)

我想要的是包含这两个类的元素。我想构建一个二维直方图,使得横轴是累积的,纵轴是不累积的。

有没有办法用 Python/Matplotlib 做到这一点?

【问题讨论】:

    标签: python numpy matplotlib histogram2d


    【解决方案1】:

    您可以利用np.cumsum 创建累积直方图。首先保存来自hist2d 的输出,然后在绘图时应用到您的数据。

    import matplotlib.pyplot as plt
    import numpy as np
    
    #Some random data
    x = np.random.normal(size=1000)
    y = np.random.normal(size=1000)
    
    #create a figure
    plt.figure(figsize=(16,8))
    
    ax1 = plt.subplot(121) #Left plot original
    ax2 = plt.subplot(122) #right plot the cumulative distribution along axis
    
    #What you have so far
    ax1.hist2d(x,y)
    
    #save the data and bins
    h, xedge, yedge,image = plt.hist2d(x,y)
    
    #Plot using np.cumsum which does a cumulative sum along a specified axis
    ax2.pcolormesh(xedge,yedge,np.cumsum(h.T,axis=1))
    
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 2013-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-05
      • 2019-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多