![](/img/trans.png)
[英]How to make a 3D plot (X, Y, Z), assigning Z values to X,Y ordered pairs?
[英]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
我想用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)箱都必須表示滿足每個(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的方式有想法嗎?
最好,非常感謝!
毛。
看一下這個:
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]
我認為應該有更好的解決方案,希望對您有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.