在数据矩阵上绘制层次聚类的结果
在Python中,如何在值矩阵的顶部绘制树状图,并适当地重新排序以反映聚类?下图就是一个示例:
这是图 6,来自:一组诱导多能性黑猩猩干细胞:比较功能基因组学的资源
我使用 scipy.cluster.dendrogram 来制作树状图并在数据矩阵上执行分层聚类。然后,如何将数据绘制为矩阵,其中行已重新排序以反映在特定阈值切割树状图引起的聚类,并将树状图与矩阵一起绘制?我知道如何在 scipy 中绘制树状图,但不知道如何绘制数据的强度矩阵及其旁边的右侧比例尺。
How can I plot a dendrogram right on top of a matrix of values, reordered appropriately to reflect the clustering, in Python? An example is the following figure:
This is Figure 6 from: A panel of induced pluripotent stem cells from chimpanzees: a resource for comparative functional genomics
I use scipy.cluster.dendrogram
to make my dendrogram and perform hierarchical clustering on a matrix of data. How can I then plot the data as a matrix where the rows have been reordered to reflect a clustering induced by the cutting the dendrogram at a particular threshold, and have the dendrogram plotted alongside the matrix? I know how to plot the dendrogram in scipy
, but not how to plot the intensity matrix of data with the right scale bar next to it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个问题没有很好地定义矩阵:“值矩阵”,“数据矩阵”。我假设你指的是距离矩阵。换句话说,对称非负N×N距离矩阵D中的元素D_ij表示两个特征向量x_i和x_j之间的距离。这是正确的吗?
如果是这样,请尝试此操作(2010 年 6 月 13 日编辑,以反映两个不同的树状图)。
在
python 3.10
和matplotlib 3.5.1
中测试编辑:对于不同的颜色,调整
cmap<
imshow
中的 /code> 属性。有关示例,请参阅 scipy/matplotlib 文档。该页面还描述了如何创建您自己的颜色图。为了方便起见,我建议使用预先存在的颜色图。在我的示例中,我使用了YlGnBu
。编辑:
add_axes
(请参阅此处的文档)接受列表或元组:(left,bottom,width,height)
。例如,(0.5,0,0.5,1)
在图形的右半部分添加一个Axes
。(0,0.5,1,0.5)
在图形的上半部分添加一个Axes
。大多数人可能会为了方便而使用
add_subplot
。我喜欢add_axes
的控制功能。要删除边框,请使用 add_axes([left,bottom,width,height], frame_on=False)。 请参阅此处的示例。
The question does not define matrix very well: "matrix of values", "matrix of data". I assume that you mean a distance matrix. In other words, element D_ij in the symmetric nonnegative N-by-N distance matrix D denotes the distance between two feature vectors, x_i and x_j. Is that correct?
If so, then try this (edited June 13, 2010, to reflect two different dendrograms).
Tested in
python 3.10
andmatplotlib 3.5.1
Edit: For different colors, adjust the
cmap
attribute inimshow
. See the scipy/matplotlib docs for examples. That page also describes how to create your own colormap. For convenience, I recommend using a preexisting colormap. In my example, I usedYlGnBu
.Edit:
add_axes
(see documentation here) accepts a list or tuple:(left, bottom, width, height)
. For example,(0.5,0,0.5,1)
adds anAxes
on the right half of the figure.(0,0.5,1,0.5)
adds anAxes
on the top half of the figure.Most people probably use
add_subplot
for its convenience. I likeadd_axes
for its control.To remove the border, use
add_axes([left,bottom,width,height], frame_on=False)
. See example here.如果除了矩阵和树状图之外,还需要显示元素的标签,可以使用以下代码,该代码显示所有标签,旋转 x 标签并更改字体大小以避免在 x 轴上重叠。它需要移动颜色条以便为 y 标签留出空间:
获得的结果是这样的(使用不同的颜色图):
If in addition to the matrix and dendrogram it is required to show the labels of the elements, the following code can be used, that shows all the labels rotating the x labels and changing the font size to avoid overlapping on the x axis. It requires moving the colorbar to have space for the y labels:
The result obtained is this (with a different color map):