2016-08-11 74 views
2

ヒストグラムの各「セル」に、色ではなくその「セル」内の合計ポイントの割合を表す数値が必要です。プログラムで行列を生成する以外の方法はありますか? 2D histogram of my datapyplot.hist2dの各 "セル"のポイントの割合をプロットする方法は?

M.Tさんの提案では、seaborn.heatmap()は2Dデータ配列を要求していると思います。しかし、私は2つの1D配列を持っています(高さと重さ、各データポイントは(高さ、重さ)の値を持つ人になります)コードはここにあり:)配列の値には特別な順序はありません。ここではサンプルコード:

plt.hist2d(height, weight, (20, 50), range=np.array([(0, 200), (0, 500)]), cmap=plt.cm.Paired) 
plt.colorbar() 
plt.show() 
+1

おそらく、[この]を見てください( http://stackoverflow.com/questions/11917547/how-to-annotate-heatmap-with-text-in-matplotlib)いくつかのアイデアを得るために。具体的には、「annot = True」を指定した[seabornのヒートマップ](http://stanford.edu/~mwaskom/software/seaborn/generated/seaborn.heatmap.html) –

+0

http://i.stack .imgur.com/Jnp3E.png? –

+0

はい、そのようなものです。しかしビンの境界はセルの境界であるので、相関線はグラフ自体に描くことができます。 – theVoonikIntern

答えて

1

次のようにあなたは、hist2dノルムに出力を通じてテキストやループを使用することができ、与え

import numpy as np 
import matplotlib.pyplot as plt 

height = [102, 99, 153, 98, 142, 198, 99] 
weight = [201, 192, 295, 181, 315, 311, 181] 

counts, xedges, yedges, Image = plt.hist2d(height, weight, (20, 50), 
              range=np.array([(0, 200), (0, 500)]), 
              cmap=plt.cm.Paired, normed=True) 

dx = xedges[2]-xedges[1] 
dy = yedges[2]-yedges[1] 
for i in range(xedges.size-1): 
    for j in range(yedges.size-1): 
     xb = xedges[i] + 0.25*dx 
     yb = yedges[j] + 0.25*dy 
     plt.text(xb, yb, str(np.round(100.*counts[i,j],2)), fontsize=6) 

plt.show() 

enter image description here

+0

は、コードを少し修正しました:normed = trueを削除し、合計数を保ちました: str(np.round(100 * counts [i、j]/total、2)))。 – theVoonikIntern

+0

ええ、それは良いかもしれません、私は 'ノルム'は単にヒストグラムの領域であると思います。 –

関連する問題