I have a heatmap and I would like to find some rectangles. enter image description here

I have already used clustermap. But here, I can not calculate these rectangles. The order of the data should not be changed.

This Code is not working:

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
tips2 = pd.read_csv('heatmapExport.csv')
g = sns.clustermap(tips2, cbar="false")

Does somebody have an idea?

Thanks a lot

Here is an example of what I want:

enter image description here

  • 1
    $\begingroup$ What is pd, pandas? And sns? What are the reasons to select those rectangles and not others (without the reasons it will be hard to find a solution)? Why do you say the code is not working, how does it fail (which error, what is the output compared to the expected output...) ? $\endgroup$
    – llrs
    Commented Oct 18, 2017 at 7:15
  • 1
    $\begingroup$ What is "the recheck"? How do you define the rectangles?A rectangle is a cluster of samples and genes? $\endgroup$
    – llrs
    Commented Oct 18, 2017 at 8:57
  • 1
    $\begingroup$ Can you give us a Minimal, Complete, and Verifiable example with some sample data, the rules to find the cluster ('recognizing optically' is not exact enough), and the expected results? $\endgroup$
    – BioGeek
    Commented Oct 18, 2017 at 14:37
  • 1
    $\begingroup$ Your example says "Cluster (for example)". Did you have criteria in mind to delimit the rectangles this way? Has the delimitation of rectangles an influence on the delimitation of others (for instance, by forbidding overlaps)? I think your question amounts to being able to formalize an intuition you have about how to form those rectangles. $\endgroup$
    – bli
    Commented Oct 19, 2017 at 8:46
  • 1
    $\begingroup$ Also, edit your question to add an explanation of what you mean by "This Code is not working". That's an important point. $\endgroup$
    – bli
    Commented Oct 19, 2017 at 8:49

1 Answer 1


Based on your description I think you should have a look at a technique called 'biclustering'.

The example on this page defines the goal of this technique as 'Finding subgroups of rows and columns which are as similar as possible to each other and as different as possible to the rest.'

Since your examples are python-based, you could check out scikit-learn's implementations of biclustering.

  • $\begingroup$ Many thanks for your response. I have implemented the algorithm as described. He does (almost) what I want. Now I have only one problem: I would not re-sort the rows and columns. For example, the algorithm says: Cluster 1 [line 1,3,4; Columns 1, 2, 3] Cluster 2 [line 2; Column 4]. I would like to know the result, however, the other way around. Lines 1.2 Columns 1 -> cluster 1; Lines 3.4 columns 2,3 -> clusters 2, ... etc. At all, it is important that the different Clusters Switch between rows and cols. If one Cluster has finished, no Datapoint should be add to this cluster $\endgroup$ Commented Oct 29, 2017 at 13:35
  • $\begingroup$ I would second scitest-learn as the approach $\endgroup$
    – M__
    Commented Jan 30, 2019 at 18:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.