![](/img/trans.png)
[英]turn scatter data into binned data with errors bars equal to standard deviation
[英]How to plot heatmap from standard deviation of binned data?
我正在嘗試從網格數據制作標准偏差(stdv)的熱圖,即我已經像網格一樣划分為單元格的數據,我想從每個單元格中獲取 stdv 並繪制其值作為每個單元格,顏色- 編碼為熱圖。 下面是我用來將 x -y 平面划分為 4 個相等的 bin 或單元格 ad 然后讓 v 值落入這 4 個 bin 的代碼。 我在每個 bin 中打印出它們和它們各自的 stdv。
import numpy as np
import matplotlib.pyplot as plt
x = np.array([-10,-2,4,12,3,6,8,14,3])
y = np.array([5,5,-6,8,-20,10,2,2,8])
v = np.array([4,-6,-10,40,22,-14,20,8,-10])
xi = x // 20+1
yi = y // 20+1
k=2
cells = [[[] for yi in range(k)] for xi in range(k)]
for ycell in range(k):
for xcell in range(k):
cells[ycell][xcell] = v[(yi == xcell) & (xi == ycell)]
for ycell in range(k):
for xcell in range(k):
this = cells[ycell][xcell]
print(ycell, xcell, len(this), this, sep='\t')
print('direct std dev is', np.std(this))
我現在想在 xy 平面上繪制它,其中 4 個 bin 中的每一個都將 stdv 值表示為強度,並顯示為如下圖所示的熱圖:
有人可以幫我弄清楚如何創建這種熱圖嗎? 謝謝!
你的意思是:
k=2
cells = [[[] for yi in range(k)] for xi in range(k)]
stds = [[[] for yi in range(k)] for xi in range(k)]
for ycell in range(k):
for xcell in range(k):
cells[ycell][xcell] = v[(yi == xcell) & (xi == ycell)]
# replace invalid (np.nan) with 0
stds[ycell][xcell] = np.std(cells[ycell][xcell]) or 0
# import plot lib
import seaborn as sns
# heatmap
sns.heatmap(stds, cmap='Blues', annot=True)
輸出:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.