logo

Seaborn ヒートマップ – 包括的なガイド

ヒートマップ は、行列の値を視覚化するために色を使用したデータのグラフィック表現として定義されます。ここでは、より一般的な値またはより高いアクティビティを表すには、基本的に明るい色が使用され、赤みがかった色が使用され、あまり一般的でない値またはアクティビティの値を表すには、暗い色が好まれます。ヒートマップは、シェーディング マトリックスの名前によっても定義されます。 Seaborn のヒートマップは、seaborn.heatmap() 関数を使用してプロットできます。

シーボーン.ヒートマップ()

構文: シーボーン.ヒートマップ( データ*vmin=なしvmax=なしcmap=なし中央=なしannot_kws=いいえ線幅=0linecolor='白'cbar=真**クワーグス )

重要なパラメータ:



    data: ndarray に強制的に変換できる 2D データセット。 vmin 、 vmax: カラーマップを固定する値。そうでない場合は、データおよび他のキーワード引数から推測されます。 cmap: データ値から色空間へのマッピング。 center: 発散データをプロットするときにカラーマップを中心にする値。注: True の場合、各セルにデータ値を書き込みます。 fmt: 注釈を追加するときに使用する文字列フォーマット コード。 linewidths: 各セルを分割する線の幅。 linecolor: 各セルを分割する線の色。 cbar: カラーバーを描画するかどうか。

データを除くすべてのパラメータはオプションです。

戻り値: matplotlib.axes._subplots.AxesSubplot 型のオブジェクト

例を挙げてヒートマップを理解しましょう。

基本的なヒートマップ

デフォルトのパラメータを使用してヒートマップを作成します。を使用して 10×10 の 2 次元データを作成します。 日付() NumPyモジュールの関数。

Python3




# importing the modules> import> numpy as np> import> seaborn as sn> import> matplotlib.pyplot as plt> > # generating 2-D 10x10 matrix of random numbers> # from 1 to 100> data>=> np.random.randint(low>=> 1>,> >high>=> 100>,> >size>=> (>10>,>10>))> print>(>'The data to be plotted: '>)> print>(data)> > # plotting the heatmap> hm>=> sn.heatmap(data>=> data)> > # displaying the plotted heatmap> plt.show()>

>

>

出力:

The data to be plotted: [[46 30 55 86 42 94 31 56 21 7] [68 42 95 28 93 13 90 27 14 65] [73 84 92 66 16 15 57 36 46 84] [ 7 11 41 37 8 41 96 53 51 72] [52 64 1 80 33 30 91 80 28 88] [19 93 64 23 72 15 39 35 62 3] [51 45 51 17 83 37 81 31 62 10] [ 9 28 30 47 73 96 10 43 30 2] [74 28 34 26 2 70 82 53 97 96] [86 13 60 51 95 26 22 29 14 29]]>

すべての例でこれと同じデータを使用します。

カラーマップの固定

を設定すると、 値を 30 にすると、 vmax 値を 70 にすると、30 ~ 70 の値を持つセルのみが表示されます。これをカラーマップのアンカーリングと呼びます。

Python3




# importing the modules> import> numpy as np> import> seaborn as sn> import> matplotlib.pyplot as plt> > # generating 2-D 10x10 matrix of random numbers> # from 1 to 100> data>=> np.random.randint(low>=>1>,> >high>=>100>,> >size>=>(>10>,>10>))> > # setting the parameter values> vmin>=> 30> vmax>=> 70> > # plotting the heatmap> hm>=> sn.heatmap(data>=>data,> >vmin>=>vmin,> >vmax>=>vmax)> > # displaying the plotted heatmap> plt.show()>

>

>

出力:

カラーマップの選択

この中で見ていきます。 cmap パラメータ。 Matplotlib は複数のカラーマップを提供しており、それらすべてを確認できます。 ここ 。この例では、次を使用します タブ20

Python3




# importing the modules> import> numpy as np> import> seaborn as sn> import> matplotlib.pyplot as plt> > # generating 2-D 10x10 matrix of random numbers> # from 1 to 100> data>=> np.random.randint(low>=>1>,> >high>=>100>,> >size>=>(>10>,>10>))> > # setting the parameter values> cmap>=> 'tab20'> > # plotting the heatmap> hm>=> sn.heatmap(data>=>data,> >cmap>=>cmap)> > # displaying the plotted heatmap> plt.show()>

>

>

出力:

カラーマップを中央に配置する

を渡すことで cmap を 0 にセンタリングします。 中心 パラメータを0にします。

Python3




# importing the modules> import> numpy as np> import> seaborn as sn> import> matplotlib.pyplot as plt> > # generating 2-D 10x10 matrix of random numbers> # from 1 to 100> data>=> np.random.randint(low>=>1>,> >high>=>100>,> >size>=>(>10>,>10>))> > # setting the parameter values> cmap>=> 'tab20'> center>=> 0> > # plotting the heatmap> hm>=> sn.heatmap(data>=>data,> >cmap>=>cmap,> >center>=>center)> > # displaying the plotted heatmap> plt.show()>

>

>

出力:

セルの値を表示する

セルの値を表示したい場合は、パラメータを渡します。 彼らが言う 真実として。 fmt 表示されるセルの内容のデータ型を選択するために使用されます。

夕食対夕食

Python3




# importing the modules> import> numpy as np> import> seaborn as sn> import> matplotlib.pyplot as plt> > # generating 2-D 10x10 matrix of random numbers> # from 1 to 100> data>=> np.random.randint(low>=>1>,> >high>=>100>,> >size>=>(>10>,>10>))> > # setting the parameter values> annot>=> True> > # plotting the heatmap> hm>=> sn.heatmap(data>=>data,> >annot>=>annot)> > # displaying the plotted heatmap> plt.show()>

>

>

出力:

区切り線のカスタマイズ

セルを区切る線の太さと色は、 線幅 そして 線色 パラメータをそれぞれ指定します。

Python3




# importing the modules> import> numpy as np> import> seaborn as sn> import> matplotlib.pyplot as plt> > # generating 2-D 10x10 matrix of random numbers> # from 1 to 100> data>=> np.random.randint(low>=>1>,> >high>=>100>,> >size>=>(>10>,>10>))> > # setting the parameter values> linewidths>=> 2> linecolor>=> 'yellow'> > # plotting the heatmap> hm>=> sn.heatmap(data>=>data,> >linewidths>=>linewidths,> >linecolor>=>linecolor)> > # displaying the plotted heatmap> plt.show()>

>

>

出力:

カラーバーを非表示にする

カラーバーを無効にするには、 シーバー パラメータを False に設定します。

Python3




# importing the modules> import> numpy as np> import> seaborn as sn> import> matplotlib.pyplot as plt> > # generating 2-D 10x10 matrix of random numbers> # from 1 to 100> data>=> np.random.randint(low>=>1>,> >high>=>100>,> >size>=>(>10>,>10>))> > # setting the parameter values> cbar>=> False> > # plotting the heatmap> hm>=> sn.heatmap(data>=>data,> >cbar>=>cbar)> > # displaying the plotted heatmap> plt.show()>

>

>

出力:

ラベルを剥がす

に False を渡すことで、x ラベルと y ラベルを無効にできます。 xticklabels そして yticklabels パラメータをそれぞれ指定します。

Python3




# importing the modules> import> numpy as np> import> seaborn as sn> import> matplotlib.pyplot as plt> > # generating 2-D 10x10 matrix of random numbers> # from 1 to 100> data>=> np.random.randint(low>=>1>,> >high>=>100>,> >size>=>(>10>,>10>))> > # setting the parameter values> xticklabels>=> False> yticklabels>=> False> > # plotting the heatmap> hm>=> sn.heatmap(data>=>data,> >xticklabels>=>xticklabels,> >yticklabels>=>yticklabels)> > # displaying the plotted heatmap> plt.show()>

>

>

出力: