環境
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らしいのでバージョンを上げたら解決するかも