两个海生的,共用一个轴的countplots

2024-05-19 15:53:15 发布

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

我希望使用seaborn countplots来显示一个轴上两个不同数据列表的频率分布。我遇到的问题是两个列表都包含唯一的元素,所以我不能简单地用大列表的轴来绘制一个列表。在

我尝试过使用python的count对象,但是由于python字典是无序的,因此图形的轴与图形上显示的计数不匹配。在

import seaborn as sns


first_list = ["a", "b", "c", "d", "e", "a", "b", "c", "a", "b","n"]
second_list = ["a","b","c","d", "e", "e","d","c","e","q"]


sns.countplot(first_list, color="blue", alpha=.5)
sns.countplot(second_list, color="red",alpha=.5)


plt.show()

上面的代码应该显示一个图表,其中包括唯一值“n”和“q”的频率,但显示的图形轴只包含第二个列表中的值。在


Tags: 数据alpha图形元素列表seabornlistcolor
1条回答
网友
1楼 · 发布于 2024-05-19 15:53:15

我认为最好是把你的数据组合成一个数据帧,然后传递给seaborn,而不是把两个图放在一起。我打过电话sns.barplot公司而不是使用计数图在原始原始值上。在

#convert the lists to series and get the counts
first_list = pd.Series(
    ["a", "b", "c", "d", "e", "a", "b", "c", "a", "b","n"]
).value_counts()

second_list = pd.Series(
    ["a","b","c","d", "e", "e","d","c","e","q"]
).value_counts()

#get the counts as a dataframe
df=pd.concat([first_list,second_list],axis=1)
df.columns=['first','second']

# melt the data frame so it has a "tidy" data format
df=df.reset_index().melt(id_vars=['index'])
^{pr2}$ ^{3}$

The final plot

相关问题 更多 >