1
0

More than 3 years have passed since last update.

【メモ】Seaborn のhueに数値データに変換可能なものをいれるとAttributeErrorになる

Last updated at Posted at 2019-11-14

環境

seaborn==0.9.0

問題

seaborn のlineplot でhueに数値データに変換可能なものをいれるとAttributeError: 'str' object has no attribute 'view'となる

解決策

paletteでcolorを陽に指定すればよい(https://github.com/mwaskom/seaborn/issues/1515)
下の方法は10色までしか対応していないと思われるので、それ以上の色を使うなら別途paletteを指定する必要はある。(参考:https://seaborn.pydata.org/tutorial/color_palettes.html)

import pandas as pd
test_df = pd.DataFrame(
[{"a": 0.0, "b": 1.0, "c": "1"},
 {"a": 0.0, "b": 1.0, "c": "2"}]
)
num_colors = len(test_df["c"].unique())
current_palette = sns.color_palette()
palette = current_palette[:num_colors]
g = sns.lineplot(x="a", y="b",hue="c", palette=palette, data=test_df)

https://github.com/mwaskom/seaborn/issues/1515#issuecomment-408436200
を参考にしました

原因

hueがducktypingで、数値に変換可能なら数値として解釈されてしまうことが原因。バグはwipらしいのでバージョンを上げたら解決するかも

1
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
1
0