Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add custom border to certain cells in a matplotlib / seaborn plot

Tags:

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?

like image 377
Justin Nelligan Avatar asked Jul 08 '15 11:07

Justin Nelligan


People also ask

How do you add a line in Seaborn?

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.

Can you use Matplotlib with Seaborn?

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.

What is Relplot in Python?

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)


1 Answers

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:

enter image description here

Note the the indexing of the cells is 0 based with the origin at the bottom left.

like image 175
Simon Gibbons Avatar answered Sep 16 '22 21:09

Simon Gibbons