使用Python的Seaborn重新排列我的热图的柱

发布于 2025-02-12 09:48:39 字数 1220 浏览 1 评论 0 原文

我正在尝试可视化以下.csv数据:

Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8,Q9,Q10,Q11,Q12,Q13,Q14,Q15,Q16,Q17,Q18,Q19,Q20
4,4,2,2,4,2,3,5,3,4,2,5,2,1,4,4,2,1,5,2
2,2,4,4,4,2,2,2,4,4,2,4,2,2,3,2,2,4,5,2
4,5,4,1,4,2,2,4,4,3,2,2,2,1,2,4,4,2,5,4
3,4,2,4,4,2,2,2,4,3,2,4,4,3,3,4,2,4,5,1
4,4,3,2,4,3,4,5,4,3,1,5,3,2,4,2,2,3,4,2
4,5,2,3,5,1,3,4,3,3,1,2,4,4,5,4,1,4,5,4
5,5,5,2,4,3,2,4,4,2,2,4,4,2,4,2,2,4,4,5
4,4,3,1,5,3,2,4,2,2,1,4,4,2,4,1,2,5,5,3
1,3,5,2,4,4,3,1,4,4,2,3,1,4,3,4,3,3,4,1
3,3,5,2,4,2,4,4,3,4,1,5,4,2,1,2,2,4,5,2

这是我的代码:

import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
         
df =  pd.read_csv('data.csv') 

map = sns.clustermap(df, annot=True, linewidths=2, linecolor='yellow', metric="correlation", method="single")

plt.show()

返回:

“

我想重新安排我的热图并按照每个响应的频率从列下订购。例如,列 Q5 具有值 4 重复8次(比任何其他列更多),因此应该是第一列。列 17 19 具有重复7次的值,因此它们应该以第二和第三为第三(确切的顺序无关紧要)。我该怎么做?

I'm trying to visualize the following .csv data:

Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8,Q9,Q10,Q11,Q12,Q13,Q14,Q15,Q16,Q17,Q18,Q19,Q20
4,4,2,2,4,2,3,5,3,4,2,5,2,1,4,4,2,1,5,2
2,2,4,4,4,2,2,2,4,4,2,4,2,2,3,2,2,4,5,2
4,5,4,1,4,2,2,4,4,3,2,2,2,1,2,4,4,2,5,4
3,4,2,4,4,2,2,2,4,3,2,4,4,3,3,4,2,4,5,1
4,4,3,2,4,3,4,5,4,3,1,5,3,2,4,2,2,3,4,2
4,5,2,3,5,1,3,4,3,3,1,2,4,4,5,4,1,4,5,4
5,5,5,2,4,3,2,4,4,2,2,4,4,2,4,2,2,4,4,5
4,4,3,1,5,3,2,4,2,2,1,4,4,2,4,1,2,5,5,3
1,3,5,2,4,4,3,1,4,4,2,3,1,4,3,4,3,3,4,1
3,3,5,2,4,2,4,4,3,4,1,5,4,2,1,2,2,4,5,2

Here's my code:

import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
         
df =  pd.read_csv('data.csv') 

map = sns.clustermap(df, annot=True, linewidths=2, linecolor='yellow', metric="correlation", method="single")

plt.show()

Which returns:

heatmap

I want to rearrange my heatmap and order it column-wise by the frequency of each response. For example, The column Q5 has the value 4 repeated 8 times (more than any other column), so it should be the first column. Columns 17 and 19 have a value that is repeated 7 times, so they should come in second and third (exact order doesn't matter). How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小苏打饼 2025-02-19 09:48:39

您可以在使用 clusterMap 中的数据之前计算订单和reindex:

order = (df.apply(pd.Series.value_counts)
           .max()
           .sort_values(ascending=False)
           .index
         )

import seaborn as sns
cm = sns.clustermap(df[order], col_cluster=False, annot=True, linewidths=2, linecolor='yellow', metric="correlation", method="single")

output:

< img src =“ https://i.sstatic.net/4ml2c.png” alt =”输入图像描述在这里”>

You can compute the order and reindex before using the data in clustermap:

order = (df.apply(pd.Series.value_counts)
           .max()
           .sort_values(ascending=False)
           .index
         )

import seaborn as sns
cm = sns.clustermap(df[order], col_cluster=False, annot=True, linewidths=2, linecolor='yellow', metric="correlation", method="single")

Output:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文