Mario
Mario

Reputation: 1966

How can smooth heatmap plots easily in Seaborn?

I was wondering if there is any options to make following picture which are outputs of sns.heatmap(df) subplots smoothy: img I've just found one relevant answer here which is suggested by using zsmooth:

data = [go.Heatmap(z=[[1, 20, 30],
                      [20, 1, 60],
                      [30, 60, 1]],
                   zsmooth = 'best')]
iplot(data)

The snippet of my codes which I used seaborn are following:

#plotting all columns ['A','B','C'] in-one-window side by side
fig, axes = plt.subplots(nrows=1, ncols=3 , figsize=(20,10))

plt.subplot(131)
sns.heatmap(df1, vmin=-1, vmax=1, cmap ="coolwarm", linewidths=.75 , linecolor='black', cbar=True , cbar_kws={"ticks":[-1.0,-0.75,-0.5,-0.25,0.00,0.25,0.5,0.75,1.0]})
fig.axes[-1].set_ylabel('[MPa]', size=20) #cbar_kws={'label': 'Celsius'}
plt.title('A', fontsize=12, color='black', loc='center', style='italic')
plt.axis('off')

plt.subplot(132)
sns.heatmap(df2, vmin=-1, vmax=1, cmap ="coolwarm", linewidths=.75 , linecolor='black', cbar=True , cbar_kws={"ticks":[-1.0,-0.75,-0.5,-0.25,0.00,0.25,0.5,0.75,1.0]})
fig.axes[-1].set_ylabel('[Mpa]', size=20) #cbar_kws={'label': 'Celsius'}
plt.title('B', fontsize=12, color='black', loc='center', style='italic')
plt.axis('off')

plt.subplot(133)
sns.heatmap(df3, vmin=-40, vmax=150, cmap ="coolwarm" , linewidths=.75 , linecolor='black', cbar=True , cbar_kws={"ticks":[-40,150,-20,0,25,50,75,100,125]}) 
plt.title('C', fontsize=12, color='black', loc='center', style='italic')
plt.axis('off')


plt.suptitle(f'Analysis of data in cycle Nr.: {count}', color='yellow', backgroundcolor='black', fontsize=48, fontweight='bold')
plt.subplots_adjust(top=0.7, bottom=0.3, left=0.05, right=0.95, hspace=0.2, wspace=0.2)
plt.savefig(f'{i}/{i}{i}{count}.png') 
plt.show()

Problem is I'm not sure if can i use it since it calls following library while mine is another. it would be great if someone explain me if it's possible and how can I implement it on my snippet?

from plotly.offline import download_plotlyjs, init_notebook_mode, plot
import plotly.graph_objs as go

Upvotes: 4

Views: 10130

Answers (1)

Freya W
Freya W

Reputation: 549

The question you linked uses plotly. If you don't want to use that and want to simply smooth the way your data looks, I suggest just using a gaussian filter using scipy.

At the top, import

from scipy.ndimage.filters import gaussian_filter

Then use it like this:

df3_smooth = gaussian_filter(df3, sigma=1)
sns.heatmap(df3_smooth, vmin=-40, vmax=150, cmap ="coolwarm" , cbar=True , cbar_kws={"ticks":[-40,150,-20,0,25,50,75,100,125]}) 

You can change the amount of smoothing, using e.g. sigma=3, or any other number that gives you the amount of smoothing you want.

Keep in mind that that will also "smooth out" any maximum data peaks you have, so your minimum and maximum data will not be the same that you specified in your normalization anymore. To still get good looking heatmaps I would suggest not using fixed values for your vmin and vmax, but:

sns.heatmap(df3_smooth, vmin=np.min(df3_smooth), vmax=np.max(df3_smooth), cmap ="coolwarm" , cbar=True , cbar_kws={"ticks":[-40,150,-20,0,25,50,75,100,125]}) 

Upvotes: 5

Related Questions