0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Seaborn HeatmapのRGB値を取得する方法

Posted at

概要

seabornで作成したheatmapのRGB値を取得します。
matplotlibのcmでheatmapと同じcolormapを指定し、cmapにheatmapで描画したデータをあてはめることでRGB値を取得しています。

実装

import numpy as np
from scipy import stats
import seaborn as sns
from matplotlib.colors import Normalize
import matplotlib.cm as cm

matrix = np.random.rand(3, 3)

heatmap = sns.heatmap(matrix,
                      cmap='plasma',
                      xticklabels = [""],
                      yticklabels = [""],
                      cbar_kws={"orientation": "horizontal"},
                      annot=True,
                      fmt=".1f")

cmap = cm.get_cmap('plasma')
data = matrix.flatten()
norm = Normalize()
rgba_values = cmap(norm(data))
for i in range(len(rgba_values)):
  print([round(255*x) for x in rgba_values[i]])

おわりに

Heatmapのobjectから直接RGB値を取得する方法があればよかったのですが、ここは調査不足で、また調べてみようと思います。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?