Control individual linewidths in seaborn heatmap

2024/9/8 8:22:56

Is it possible to widen the linewidth for sepcific columns and rows in a seaborn heatmap?

For example, can this heatmap

import numpy as np; np.random.seed(0)
import seaborn as sns; sns.set()
uniform_data = np.random.rand(10, 12)
ax = sns.heatmap(uniform_data, linewidths=1.0)

be transformed into something like this:

enter image description here

Answer

It's possible, but may be a lot of work. A possible solution might look like shown below. It involves plotting 6 different heatmaps and adjusting the spacings such that it looks okish. One then also needs to synchronize the colorscaling and manually set the colorbar.

import matplotlib
import matplotlib.pyplot as plt
import numpy as np; np.random.seed(0)
import seaborn as sns; sns.set()data = np.random.rand(10, 12)asp = data.shape[0]/float(data.shape[1])
figw = 8
figh = figw*aspcmap = plt.cm.copper
norm = matplotlib.colors.Normalize(vmin= data.min(), vmax= data.max())gridspec_kw = {"height_ratios":[9,1], "width_ratios" : [4,5,3]}
heatmapkws = dict(square=False, cbar=False, cmap = cmap, linewidths=1.0, vmin= data.min(), vmax= data.max() ) 
tickskw =  dict(xticklabels=False, yticklabels=False)left = 0.07; right=0.87
bottom = 0.1; top = 0.9
fig, axes = plt.subplots(ncols=3, nrows=2, figsize=(figw, figh), gridspec_kw=gridspec_kw)
plt.subplots_adjust(left=left, right=right,bottom=bottom, top=top, wspace=0.1, hspace=0.1*asp )
sns.heatmap(data[:9,0:4], ax=axes[0,0], xticklabels=False, yticklabels=True, **heatmapkws)
sns.heatmap(data[:9,4:9], ax=axes[0,1], xticklabels=False, yticklabels=False, **heatmapkws)
sns.heatmap(data[:9,9:12], ax=axes[0,2],xticklabels=False, yticklabels=False, **heatmapkws)sns.heatmap(data[9:,:4], ax=axes[1,0], xticklabels=True, yticklabels=True, **heatmapkws)
sns.heatmap(data[9:,4:9], ax=axes[1,1], xticklabels=True, yticklabels=False, **heatmapkws)
sns.heatmap(data[9:,9:12], ax=axes[1,2], xticklabels=True, yticklabels=False,**heatmapkws)axes[1,0].set_yticklabels([9])
axes[1,1].set_xticklabels([4,5,6,7,8])
axes[1,2].set_xticklabels([9,10,11])cax = fig.add_axes([0.9,0.1,0.03,0.8])
sm = matplotlib.cm.ScalarMappable(cmap=cmap, norm=norm)
sm.set_array([])
fig.colorbar(sm, cax=cax)plt.show()

enter image description here

https://en.xdnf.cn/q/73279.html

Related Q&A

openerp context in act_window

In OpenERP 6.1 this act_window:<act_windowdomain="[(id, =, student)]"id="act_schedule_student"name="Student"res_model="school.student"src_model="school.s…

Djangos redirects app doesnt work with URL parameters

I recently installed Djangos default redirects app on my site using the exact instructions specified:Ensured django.contrib.sites framework is installed. Added django.contrib.redirects to INSTALLED_APP…

get fully qualified method name from inspect stack

I have trouble completing the following function:def fullyQualifiedMethodNameInStack(depth=1):"""The function should return <file>_<class>_<method> for the method in th…

Project Euler #18 - how to brute force all possible paths in tree-like structure using Python?

Am trying to learn Python the Atlantic way and am stuck on Project Euler #18.All of the stuff I can find on the web (and theres a LOT more googling that happened beyond that) is some variation on well …

Is it possible to sniff the Character encoding?

I have a webpage that accepts CSV files. These files may be created in a variety of places. (I think) there is no way to specify the encoding in a CSV file - so I can not reliably treat all of them as …

numpy.empty giving nonempty array

When I create an empty numpy array using foo = np.empty(1) the resulting array contains a float64:>>> foo = np.empty(1) >>> foo array([ 0.]) >>> type(foo[0]) <type numpy.f…

Accessing password protected url from python script

In python, I want to send a request to a url which will return some information to me. The problem is if I try to access the url from the browser, a popup box appears and asks for a username and passwo…

Solving multiple linear sparse matrix equations: numpy.linalg.solve vs. scipy.sparse.linalg.spsolve

I have to solve a large amount of linear matrix equations of the type "Ax=B" for x where A is a sparse matrix with mainly the main diagonal populated and B is a vector. My first approach was …

I want to return html in a flask route [duplicate]

This question already has answers here:Python Flask Render Text from Variable like render_template(4 answers)Closed 6 years ago.Instead of using send_static_file, I want to use something like html(<…

Why doesnt cv2 dilate actually affect my image?

So, Im generating a binary (well, really gray scale, 8bit, used as binary) image with python and opencv2, writing a small number of polygons to the image, and then dilating the image using a kernel. Ho…