Right now I`m using Seaborn's clustermap to generate some clustered heatmaps - so far so good.
For a certain use case, I need to draw colored borders around specific cells. Is there a way to do that? Or with pcolormesh in matplotlib, or any other way?
To add a horizontal and vertical line we can use Seaborn's refline() function with x and y y co-ordinates for the locations of the horizontal and vertical lines.
Seaborn provides an API on top of Matplotlib that offers sane choices for plot style and color defaults, defines simple high-level functions for common statistical plot types, and integrates with the functionality provided by Pandas DataFrame s.
This function provides access to several different axes-level functions that show the relationship between two variables with semantic mappings of subsets. The kind parameter selects the underlying axes-level function to use: scatterplot() (with kind="scatter" ; the default)
You can do this by overplotting a Rectangle patch on the cell that you would want to highlight. Using the example plot from the seaborn docs
import seaborn as sns import matplotlib.pyplot as plt sns.set() flights = sns.load_dataset("flights") flights = flights.pivot("month", "year", "passengers") g = sns.clustermap(flights)
We can highlight a cell by doing
from matplotlib.patches import Rectangle ax = g.ax_heatmap ax.add_patch(Rectangle((3, 4), 1, 1, fill=False, edgecolor='blue', lw=3)) plt.show()
This will produce the plot with a highlighted cell like so:
Note the the indexing of the cells is 0 based with the origin at the bottom left.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With