简体   繁体   中英

Change the color of specific labels in sns.heatmap?

Is there a way to change the color of specific labels in sns.heatmap?

Basing my attempt on this answer , I tried the following:

mask = np.zeros_like(cor, dtype=np.bool)
mask[np.triu_indices_from(mask)] = True
plt.figure(figsize=(12,10))
g = sns.heatmap(cor,
            vmin=-1,
            cmap='coolwarm',
            annot=False,
            mask = mask);

columns = df.columns
lut = dict(zip(columns, "rbg"))
row_colors = columns.map(lut)

for tick_label in g.ax_heatmap.axes.get_yticklabels():
    tick_text = tick_label.get_text()
    if tick_text == 'ascii':
        column = columns.loc[int(tick_text)]
        tick_label.set_color(lut[column])

But got:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-204-67642fc463ec> in <module>
     12 row_colors = columns.map(lut)
     13 
---> 14 for tick_label in g.ax_heatmap.axes.get_yticklabels():
     15     tick_text = tick_label.get_text()
     16     if tick_text == 'ascii':

AttributeError: 'AxesSubplot' object has no attribute 'ax_heatmap'

And a heatmap where the ascii label is not red.

The answer you copied was for a clustermap , which returns an object with several axes.

Here, you are using heatmap which operates on a single axes. You should write:

ax = sns.heatmap(cor,
            vmin=-1,
            cmap='coolwarm',
            annot=False,
            mask = mask);
(...)
for tick_label in ax.get_yticklabels():
    (...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM