为什么我在seaborn countplot中的传奇没有显示所有标签?

2024-05-16 11:43:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个海生计数图,显示了七种不同的动物种类:

g = sns.countplot(data=df, x="class_type")
plt.legend(title="Animal Type", loc="upper right", labels=["Mammal", "Bird", "Reptile", "Fish", 
                                                           "Amphibian", "Bug", "Invertebrate"])
plt.show(g)

这给了我这个印象。 countplot

如您所见,仅显示第一个标签。请问如何显示每个酒吧的标签

这是我的数据帧的顶部(前20行):

   animal_name  hair  feathers  eggs  milk  airborne  aquatic  predator  \
0     aardvark     1         0     0     1         0        0         1   
1     antelope     1         0     0     1         0        0         0   
2         bass     0         0     1     0         0        1         1   
3         bear     1         0     0     1         0        0         1   
4         boar     1         0     0     1         0        0         1   
5      buffalo     1         0     0     1         0        0         0   
6         calf     1         0     0     1         0        0         0   
7         carp     0         0     1     0         0        1         0   
8      catfish     0         0     1     0         0        1         1   
9         cavy     1         0     0     1         0        0         0   
10     cheetah     1         0     0     1         0        0         1   
11     chicken     0         1     1     0         1        0         0   
12        chub     0         0     1     0         0        1         1   
13        clam     0         0     1     0         0        0         1   
14        crab     0         0     1     0         0        1         1   
15    crayfish     0         0     1     0         0        1         1   
16        crow     0         1     1     0         1        0         1   
17        deer     1         0     0     1         0        0         0   
18     dogfish     0         0     1     0         0        1         1   
19     dolphin     0         0     0     1         0        1         1   

    toothed  backbone  breathes  venomous  fins  legs  tail  domestic  \
0         1         1         1         0     0     4     0         0   
1         1         1         1         0     0     4     1         0   
2         1         1         0         0     1     0     1         0   
3         1         1         1         0     0     4     0         0   
4         1         1         1         0     0     4     1         0   
5         1         1         1         0     0     4     1         0   
6         1         1         1         0     0     4     1         1   
7         1         1         0         0     1     0     1         1   
8         1         1         0         0     1     0     1         0   
9         1         1         1         0     0     4     0         1   
10        1         1         1         0     0     4     1         0   
11        0         1         1         0     0     2     1         1   
12        1         1         0         0     1     0     1         0   
13        0         0         0         0     0     0     0         0   
14        0         0         0         0     0     4     0         0   
15        0         0         0         0     0     6     0         0   
16        0         1         1         0     0     2     1         0   
17        1         1         1         0     0     4     1         0   
18        1         1         0         0     1     0     1         0   
19        1         1         1         0     1     0     1         0   

    catsize  class_type  
0         1           1  
1         1           1  
2         0           4  
3         1           1  
4         1           1  
5         1           1  
6         1           1  
7         0           4  
8         0           4  
9         0           1  
10        1           1  
11        0           2  
12        0           4  
13        0           7  
14        0           7  
15        0           7  
16        0           2  
17        1           1  
18        1           4  
19        1           1  

如您所见,它是数字、字符串和一个热编码数据的混合体


Tags: dfdatatitletypeplt标签class计数
1条回答
网友
1楼 · 发布于 2024-05-16 11:43:18

感谢您提供示例数据

一个问题似乎是plt.legend()命令没有在当前轴上运行

您可以这样做:

import seaborn as sns, pandas as pd, matplotlib.pyplot as plt 

ax = sns.countplot(data=df, x="class_type", hue='class_type', dodge=False) 
h,l = ax.get_legend_handles_labels()
labels=["Mammal", "Bird", "Reptile", "Fish", "Amphibian", "Bug", "Invertebrate"]
ax.legend(h,labels,title="Animal Type", loc="upper right") 
plt.show()

结果: enter image description here

在这里发现了dodge=Falsekwarg尖端:https://github.com/mwaskom/seaborn/issues/871

相关问题 更多 >