Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将第二个颜色条添加到Seaborn热图/群集图_Python_Python 3.x_Heatmap_Seaborn_Colorbar - Fatal编程技术网

Python 将第二个颜色条添加到Seaborn热图/群集图

Python 将第二个颜色条添加到Seaborn热图/群集图,python,python-3.x,heatmap,seaborn,colorbar,Python,Python 3.x,Heatmap,Seaborn,Colorbar,我试图帮助某人为下图中的垂直蓝色条添加颜色条。我们尝试了许多不同的plt.colorbar(row\u colors)(如上面和下面的sns.clustermap())并在线查看了2个小时,但没有成功。我们只想为蓝色添加一个颜色条,请帮助 import pickle import numpy as np import seaborn as sns import pandas as pd import matplotlib.pyplot as plt feat_mat, freq, label

我试图帮助某人为下图中的垂直蓝色条添加颜色条。我们尝试了许多不同的
plt.colorbar(row\u colors)
(如上面和下面的
sns.clustermap()
)并在线查看了2个小时,但没有成功。我们只想为蓝色添加一个颜色条,请帮助

import pickle
import numpy as np
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt

feat_mat, freq, label = pickle.load(open('file.pkl', 'rb'))

feat_mat_df = pd.DataFrame(feat_mat[4])

freq_df = pd.DataFrame(freq)
freq_df_transposed = freq_df.transpose()

my_palette = dict(zip(set(freq_df_transposed[int('4')]), sns.color_palette("PuBu", len(set(freq_df_transposed[int('4')]))))))
row_colors = freq_df_transposed[int('4')].map(my_palette)

sns.clustermap(feat_mat_df, metric="euclidean", standard_scale=1, method="complete", cmap="coolwarm", row_colors = row_colors)

plt.show()


这就是他代码的基础:

我认为类似的东西应该适合您的目的-我没有可用的clustermap示例,但逻辑与您想做的是一样的。基本上,你将得到你制作的颜色列表,并显示它,然后隐藏imshow绘图,并在它的位置绘制颜色条。 在我的示例中,我使用make_axes_locatable将轴放置在带有数据的绘图旁边,以将颜色栏放在-。我发现为其他对象(图例、颜色贴图或其他)放置新轴比在同一轴上绘制它们更容易

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np; np.random.seed(0)
import seaborn as sns
from mpl_toolkits.axes_grid1 import make_axes_locatable
import random


uniform_data = np.random.rand(10, 12)

fig, ax = plt.subplots(1,1, figsize = (5,5))
divider = make_axes_locatable(ax)

axDivY = divider.append_axes( 'right', size=0.2, pad= 0.1)
axDivY2 = divider.append_axes( 'right', size=0.2, pad= 0.2)

# we will use this for the colorscale bar
axDivY3 = divider.append_axes( 'right', size=0.2, pad= 0.2)
ax1 = sns.heatmap(uniform_data, ax=ax, cbar_ax=axDivY)


# the palette you were using to make the label column on the clustermap

# some simulated labels for your data with values
color_label_list =[random.randint(0,20) for i in range(20)]

pal =  sns.color_palette("PuBu", len(set(color_label_list)))
n = len(pal)
size = 1

# plot the colors with imshow to make a colormap later
ax2 = axDivY2.imshow(np.array([color_label_list]),
              cmap=mpl.colors.ListedColormap(list(pal)),
              interpolation="nearest", aspect="auto")
# turn off the axes so they aren't visible- note that you need ax.axis('off) if you have older matplotlib
axDivY2.set_axis_off()
axDivY2.set_visible(False)
# plot the colorbar on the other axes (which is on top of the one that we turned off)
plt.colorbar(ax2, cax = axDivY3) ;

这就是他代码的来源:我的答案解决了这个问题吗?或者clustermap还有其他问题吗?