回答編集履歴

1

追記

2022/09/08 09:32

投稿

bsdfan
bsdfan

スコア4578

test CHANGED
@@ -14,3 +14,29 @@
14
14
  ax.legend(handles=handles, labels=["-","/","+","0","1","2"], ncol=2, title="H_Type S_Type")
15
15
  ```
16
16
  ![結果](https://ddjkaamml8q8x.cloudfront.net/questions/2022-09-06/d3b33eb2-69b5-40d1-9087-cb60e5b6b3f9.png)
17
+
18
+ ---
19
+ 追加の質問に関して。
20
+ 存在しないデータのlegendを追加しないといけないので、まじめにやるなら [https://matplotlib.org/stable/gallery/text_labels_and_annotations/custom_legends.html](https://matplotlib.org/stable/gallery/text_labels_and_annotations/custom_legends.html) あたりを参考にして、対応するマーカーを作って、handlesに設定してやればできます。
21
+
22
+ 簡単にするなら、ダミーデータを追加してプロットすれば、legendに出せるようになります。
23
+ ```python
24
+ import seaborn as sns
25
+ import matplotlib.pylab as plt
26
+
27
+ x=["a","b","c","d","e","f"]
28
+ y=[1,2,3,4,5,6]
29
+ h=[0,0,0,0,0,0]
30
+ s=[10,20,10,10,20,20]
31
+
32
+ x2 = x + [None] * 3
33
+ y2 = y + [None] * 3
34
+ h2 = h + [-1, 0, 1]
35
+ s2 = s + [10, 20, 40]
36
+ sizes = {x: x for x in s2}
37
+ colp = {-1:'#1F3FDF', 0:'#222222', +1:'#DF1F3F'}
38
+ ax = sns.scatterplot(x=x2, y=y2, hue=h2, size=s2, sizes=sizes, palette=colp, legend="full")
39
+ handles, labels = ax.get_legend_handles_labels()
40
+ ax.legend(handles=handles, labels=["-","/","+","0","1","2"], ncol=2, title="H_Type S_Type")
41
+ ```
42
+