seaborn heatmap with frames

2019-06-25 05:42发布

I rendered a heatmap with seaborn.heatmap() works nicely. However, for a certain purpose I need frames around the plot.

matplotlib.rcParams['axes.edgecolor'] = 'black'
matplotlib.rcParams['axes.linewidth'] = 1

both don't work.

2条回答
ゆ 、 Hurt°
2楼-- · 2019-06-25 06:10

Don't know if there is a technical command for that but if you want to mimic the behavior just try using axhline and axvline:

import string
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
letters = string.ascii_letters

rs = np.random.RandomState(33)
d = pd.DataFrame(data=rs.normal(size=(100, 26)),
                 columns=list(letters[:26]))

# Compute the correlation matrix
corr = d.corr()

# Generate a mask for the upper triangle
mask = np.zeros_like(corr, dtype=np.bool)
mask[np.triu_indices_from(mask)] = True

# Set up the matplotlib figure
f, ax = plt.subplots(figsize=(11, 9))

# Generate a custom diverging colormap
cmap = sns.diverging_palette(220, 10, as_cmap=True)

# Draw the heatmap with the mask and correct aspect ratio
ax = sns.heatmap(corr, mask=mask, cmap=cmap, vmax=.3,
            square=True, xticklabels=5, yticklabels=5,
            linewidths=.5, cbar_kws={"shrink": .5}, ax=ax)

ax.axhline(y=0, color='k',linewidth=10)
ax.axhline(y=corr.shape[1], color='k',linewidth=10)
ax.axvline(x=0, color='k',linewidth=10)
ax.axvline(x=corr.shape[0], color='k',linewidth=10)
plt.show()

, which results in:

heatmap with axis lines

查看更多
再贱就再见
3楼-- · 2019-06-25 06:16
ax = sns.heatmap(x)
for _, spine in ax.spines.items():
    spine.set_visible(True)
查看更多
登录 后发表回答