gpt4 book ai didi

python - 子图中的 Seaborn 聚类图

转载 作者:太空宇宙 更新时间:2023-11-03 12:10:42 24 4
gpt4 key购买 nike

我正在尝试将数据框的聚类图和箱线图绘制为子图。我无法将 clustermap 绘制为子图,因为它是图形级图。有办法实现吗?

import pandas as pd
import seaborn as sns

# initiliaze a dataframe with index and column names
idf = pd.DataFrame.from_items([('A', [1, 2, 3]),
('B', [4, 5, 6]),
('C', [10, 20, 30]),
('D', [14, 15, 16])],
orient='index', columns=['x', 'y','z'])

# Get the figure and two subplots, unpack the axes array immediately
fig, (ax1, ax2) = plt.subplots(2, sharex=True)

# Plot a boxplot in one of the subplot
idf.plot(kind='box', ax=ax1)

# Plot the clustermap in the other subplot
cax = sns.clustermap(idf, col_cluster=False, row_cluster=True)

# I tried to change the axis from the clustermap to subplot axis
# but I don't think this works like this
cax.ax_heatmap=ax2

# Show the plot
plt.show()

我现在得到的:

图 1:

enter image description here


图 2: enter image description here

我需要的是这样的:

enter image description here

谢谢。

最佳答案

从@mwaskom 的评论中,我阅读了更多关于 clustermap 图形函数的内容,并意识到我可以用箱线图图像替换柱状树状图图像。但是,我会尝试找到如何添加另一个轴而不是替换列树状图轴,以防万一我需要用箱形图显示行和列树状图。但到目前为止我得到的对我来说很好。这是代码:

import pandas as pd
import seaborn as sns

# initiliaze a dataframe with index and column names
idf = pd.DataFrame.from_items([('A', [1, 2, 3]), ('B', [4, 5, 6]), ('C', [10, 20, 30]), ('D', [14, 15, 16])], orient='index', columns=['x', 'y', 'z'])

# Plot the clustermap which will be a figure by itself
cax = sns.clustermap(idf, col_cluster=False, row_cluster=True)

# Get the column dendrogram axis
cax_col_dend_ax = cax.ax_col_dendrogram.axes

# Plot the boxplot on the column dendrogram axis
# I still need to figure out how to show the axis for this boxplot
idf.plot(kind='box', ax=cax_col_dend_ax)

# Show the plot
plt.show()

这导致:

enter image description here

关于python - 子图中的 Seaborn 聚类图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39170455/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com