gpt4 book ai didi

python - 将 1 个直方图添加到 Clustermap 的一侧

转载 作者:太空宇宙 更新时间:2023-11-03 15:46:39 25 4
gpt4 key购买 nike

鉴于此 clustermap , 我将如何向右垂直轴添加条形图/直方图(而不是列出名称)?

我希望直方图在右侧显示橙色、黄色和棕色列数据。

我考虑尝试将 sns.jointplot() 与来自 heremarginal_kws 一起使用,但这是 2 个直方图...所以我尝试只向右侧添加一个直方图,但我似乎无法让它正常工作。

# Libraries
import seaborn as sns
import pandas as pd
from matplotlib import pyplot as plt

# Data set
url = 'https://python-graph-gallery.com/wp-content/uploads/mtcars.csv'
df = pd.read_csv(url)
df = df.set_index('model')
df

# Prepare a vector of color mapped to the 'cyl' column
my_palette = dict(zip(df.cyl.unique(), ["orange","yellow","brown"]))
row_colors = df.cyl.map(my_palette)

# plot
sns.clustermap(df, metric="correlation", method="single", cmap="Blues", standard_scale=1, row_colors=row_colors)

enter image description here

尝试:

# Libraries
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt

# Data set
url = 'https://python-graph-gallery.com/wp-content/uploads/mtcars.csv'
df = pd.read_csv(url)
df = df.set_index('model')
df


# Prepare a vector of color mapped to the 'cyl' column
my_palette = dict(zip(df.cyl.unique(), ["orange","yellow","brown"]))
row_colors = df.cyl.map(my_palette)

# plot
cluster = sns.clustermap(df, metric="correlation", method="single", cmap="Blues", standard_scale=1, row_colors=row_colors)
sns.jointplot(cluster, cluster, kind='clustermap')

ATTEMPT

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

url = 'https://python-graph-gallery.com/wp-content/uploads/mtcars.csv'
df = pd.read_csv(url)

fig, ax = plt.subplots()
plt.barh(df['model'], df['cyl'])

enter image description here

最佳答案

由于 clustermap 创建了自己的图,因此需要做一些小手术才能向其添加另一个图。

# Libraries
import numpy as np
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable

# Data set
url = 'https://python-graph-gallery.com/wp-content/uploads/mtcars.csv'
df = pd.read_csv(url)
df = df.set_index('model')
# Prepare a vector of color mapped to the 'cyl' column
my_palette = dict(zip(df.cyl.unique(), ["orange","yellow","brown"]))
row_colors = df.cyl.map(my_palette)

# plot clustermap
cluster = sns.clustermap(df, metric="correlation", method="single",
cmap="Blues", standard_scale=1, row_colors=row_colors)

#enlarge figure
cluster.fig.set_size_inches(8,6)
# make some space to the right in the figure
cluster.gs.update(right=0.95)
# divide existing axes
divider = make_axes_locatable(cluster.ax_heatmap)
divider2 = make_axes_locatable(cluster.ax_col_dendrogram)
# create new axes for bar plot
ax = divider.append_axes("right", size="20%", pad=1.7)
# create empty space of same size as bar plot axes (don't use this space)
nax = divider2.new_horizontal(size="20%", pad=1.7)

# Sort the values for the bar plot to have the same order as clusters
target = [t.get_text() for t in np.array(cluster.ax_heatmap.get_yticklabels())]
ind= np.array([list(df.index.values).index(t) for t in target])

# plot bar plot in ax
ax.barh(np.arange(len(target)), df['cyl'].values[ind])
ax.set_yticklabels([])

ax.set_ylim(-0.5,len(df.index)-.5)
ax.invert_yaxis()

plt.show()

enter image description here

关于python - 将 1 个直方图添加到 Clustermap 的一侧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49582178/

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