【问题标题】:How to plot a 3d histogram from 3 different lists (x, y and z), where z is the mean of all repeated x,y coordinates如何从 3 个不同的列表(x、y 和 z)绘制 3d 直方图,其中 z 是所有重复 x、y 坐标的平均值
【发布时间】:2014-09-30 01:17:33
【问题描述】:

我想用 matplotlib 绘制一个 3D 直方图,来自三个如下所示的列表:

x = [random.randint(0, 12) for i in range(101)]  
y = [random.randint(0, 12) for i in range(101)]  
z = [random.random() for i in range(101)]

关键是 x 和 y 轴的范围必须从 0 到 12,并且每个 (x,y) bin 必须代表满足每个 (x,y) 标准的所有 z 值的平均值。例如,让我们想象一下

x[0] = 5  
y[0] = 3  
z[0] = 0.8

可能发生这种情况:

x[25] = 5  
y[25] = 3  
z[25] = 0.6

因此直方图必须在坐标 (x,y) = (5,3) 上放置一个 0.7 高的 bin(x = 5 AND y = 3 的所有实例的平均值)。

有人知道如何以 Python 方式进行吗?

最好的,非常感谢!
毛里西奥。

【问题讨论】:

    标签: python matplotlib 3d histogram


    【解决方案1】:

    看看这个:

    import numpy as np
    n=101
    
    x = [np.random.randint(0, 12) for i in range(n)]  
    y = [np.random.randint(0, 12) for i in range(n)]  
    z = [np.random.random() for i in range(n)]
    
    #coord=[(x[i],y[i]) for i in range(n)]
    
    m=np.asarray([x,y]).transpose()
    
    d=dict()
    for i in range(n):
        coord=tuple(m[i])
        d[coord]=d[coord]+[z[i]] if coord in d else [z[i]]    
    
    #for e in d:
    #    print e,np.asarray(d[e]).mean()
    
    m2=[]
    for e in d:
        m2.append([list(e)[0],list(e)[1],np.asarray(d[e]).mean()])
    
    m2=np.asarray(m2).transpose()
    

    首先,它通过转置 x 和 y 数组将坐标保存在 m 中。

    然后,它创建一个字典来存储每个唯一坐标的 z 值列表。

    最后,它将每个坐标的平均值存储在 m2 中,其中 'x'=m2[0], 'y'=m2[1] 和 'z-mean'=m2[2]

    我认为应该有更好的解决方案, 希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2013-02-26
      • 1970-01-01
      • 2011-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-20
      • 2012-09-23
      • 1970-01-01
      相关资源
      最近更新 更多