python-将1个直方图添加到Clustermap的一侧
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python-将1个直方图添加到Clustermap的一侧,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3048字,纯文字阅读大概需要5分钟。
内容图文
![python-将1个直方图添加到Clustermap的一侧](/upload/InfoBanner/zyjiaocheng/669/6a9f7f0de60f428d84eed30313804e39.jpg)
给定这个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)
尝试:
# 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')
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'])
解决方法:
由于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的一侧全部内容,希望文章能够帮你解决python-将1个直方图添加到Clustermap的一侧所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。