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

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

我希望直方图在右侧显示橙色,黄色和棕色的列数据.

我考虑过尝试将sns.jointplot()与here的marginal_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)

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

尝试:

# 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')

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

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'])

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

解决方法:

由于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()

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

上一篇:seaborn.heatmap刻度及标签设置


下一篇:如何在python中创建从绿色到红色的热图?