问题描述
现在我正在使用 Seaborn 的 clustermap 来生成一些集群热图 - 到目前为止效果很好.
Right now I`m using Seaborn's clustermap to generate some clustered heatmaps - so far so good.
对于某个用例,我需要在特定单元格周围绘制彩色边框.有没有办法做到这一点?还是在matplotlib中使用pcolormesh或其他方式?
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?
推荐答案
您可以通过重复绘制要突出显示的单元格上的矩形补丁.使用 seaborn docs
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:
请注意,单元格的索引是基于左下角的原点为 0.
Note the the indexing of the cells is 0 based with the origin at the bottom left.
这篇关于将自定义边框添加到matplotlib/seaborn图中的某些单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!