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

时间:2018-03-30 22:23:57

标签: python python-3.x histogram heatmap seaborn

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

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

我考虑过使用来自heresns.jointplot() marginal_kws,但这是2个直方图...所以我尝试向右添加一个直方图,但我可以&# 39;似乎可以让它正常工作。

# 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

1 个答案:

答案 0 :(得分:3)

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