2017-03-09 14 views
0

フォーラムの初心者ユーザー。助けてください。私はデータセットを持っています:x、y座標、各x、yには値があります。カラースケールで各ビンの値の合計を表示する2dヒストグラムをプロットしたいと思います。 matplotlib hexbinはまっすぐです。私がすることができます。例えば:2ヒストグラムをカウントではなく合計値でプロットする

import matplotlib.pyplot as plt 
import numpy as np 
from matplotlib.colors import LogNorm 

xpos = np.random.rand(0,10) 
ypos = np.random.rand(0,10) 
plt.hexbin(x = xpos, y = ypos, C=mass, cmap= plt.cm.jet, gridsize=100, reduce_C_function=sum, bins="log") 
cb = plt.colorbar() 
cb.ax.set_ylabel('log (sum value in each bin)') 
plt.xlabel('Xpos') 
plt.ylabel('Ypos') 
plt.show() 

しかし、私はhistogram2dまたはmatplotlibのをhist2dと同様のプロットを作るのに苦労しています。私はbinned_statistic_2dとhistogram2dを何とか組み合わせなければならないと思う。上記のplt.hexbin行を置き換えても問題ありません:

plt.hist2d(x = xpos, y = ypos, bins = 50, norm = LogNorm()) 

手がかりはありますか?私はフォーラムを見ているが、動作するコードを見つけることができない。

+0

:ここ

df.mass.groupby([pd.cut(df.x, bins=xbins, include_lowest=True), pd.cut(df.y, bins=ybins, include_lowest=True)]) \ .sum().unstack(fill_value=0) 

は完全な例でありますか? –

+0

いいえ、私は実際のデータセットx、yを持っています。私はタイプミスを参照してください。私はそれを修正します。 – hamster

+0

ただ更新。私は問題を解決しました。 hist2dを使用する代わりに、私自身のビニングコードを書きます。 – hamster

答えて

0

プロットする前にビニングされた2Dプロットに表示する値を計算してから、imshowプロットとして表示することができます。

パンダを使用していただければ、(pandas.cut)xとyのデータをカットして大量データをグループ化することもできます。その後、合計(.sum())を適用してスタックを解除し、ピボットテーブルを取得します。 `np.random.rand(0,10,0.1を)`:これが行うことになっていただきまし

import numpy as np; np.random.seed(1) 
import pandas as pd 
import matplotlib.pyplot as plt 
import matplotlib.colors 

xpos = np.random.randint(0,10, size=50) 
ypos = np.random.randint(0,10, size=50) 
mass = np.random.randint(0,75, size=50) 

df = pd.DataFrame({"x":xpos, "y":ypos, "mass":mass}) 

xbins = range(10) 
ybins = range(10) 
su = df.mass.groupby([pd.cut(df.x, bins=xbins, include_lowest=True), 
        pd.cut(df.y, bins=ybins, include_lowest=True)]) \ 
      .sum().unstack(fill_value=0) 
print su 
im = plt.imshow(su.values, norm=matplotlib.colors.LogNorm(1,300)) 
plt.xticks(range(len(su.index)), su.index, rotation=90) 
plt.yticks(range(len(su.columns)), su.columns) 
plt.colorbar(im) 
plt.show() 

enter image description here

関連する問題