Austin Jin
Austin Jin

Reputation: 141

How to create a heatmap with different color ranges for the sum column?

I've got my pd.dataframe ready.

            Communication Services  Consumer Discretionary  Consumer Staples  Energy  Financials  Health Care  Industrials  Materials  Real Estate  Technology  Utilities  Sum
Date                                                                                                                                                                          
2020-09-15  61                      65                      39                3       36          53           68           89         74           43          53         584
2020-09-14  50                      70                      39                7       54          45           67           92         64           28          53         569
2020-09-11  38                      54                      30                0       28          27           46           82         25           18          28         376
2020-09-10  30                      52                      24                0       16          19           30           67         32           12          25         307
2020-09-09  50                      57                      36                0       33          30           52           71         51           30          42         452
2020-09-08  34                      55                      21                0       24          16           24           46         48           12          25         305
2020-09-04  53                      59                      51                3       66          32           47           71         74           35          28         519
2020-09-03  57                      67                      57                0       48          40           49           82         80           52          32         564
2020-09-02  73                      85                      78                3       80          74           94           89         87           94          64         821
2020-09-01  69                      78                      54                3       54          51           79           85         51           77          14         615
2020-08-31  76                      73                      78                7       50          61           75           64         54           70          21         629
2020-08-28  92                      81                      75                30      81          48           86           89         77           76          17         752
2020-08-27  88                      77                      81                11      83          53           82           82         70           64          14         705
2020-08-26  92                      81                      75                11      46          43           79           89         45           69          7          637
2020-08-25  92                      86                      78                23      65          45           82           82         64           64          21         702
2020-08-24  92                      88                      90                38      62          38           90           75         54           61          39         727
2020-08-21  80                      78                      69                11      28          37           71           50         45           49          17         535
2020-08-20  84                      72                      63                11      34          45           78           57         45           57          17         563
2020-08-19  80                      83                      81                34      48          56           84           71         29           60          35         661
2020-08-18  88                      88                      90                53      48          62           91           71         70           64          42         767
2020-08-17  80                      95                      87                80      69          62           94           78         77           63          42         827
2020-08-14  84                      100                     90                80      83          56           94           78         64           57          42         828
2020-08-13  88                      98                      87                69      81          56           95           78         64           66          57         839
2020-08-12  73                      96                      87                96      83          58           98           75         90           63          64         883
2020-08-11  73                      86                      72                84      89          50           95           78         77           53          46         803
2020-08-10  80                      93                      87                88      83          53           93           78         90           64          82         891
2020-08-07  69                      81                      84                65      84          58           91           60         83           71          89         835
2020-08-06  73                      80                      81                73      60          53           84           57         54           78          67         760
2020-08-05  69                      81                      87                73      68          69           89           64         51           78          64         793
2020-08-04  80                      63                      87                73      46          66           64           53         67           81          85         765
2020-08-03  69                      55                      78                50      60          74           68           42         51           81          78         706
2020-07-31  65                      62                      78                42      60          61           64           46         58           74          92         702
2020-07-30  65                      62                      75                34      65          74           71           50         64           61          89         710
2020-07-29  73                      78                      90                88      90          87           79           85         70           64          85         889
2020-07-28  46                      67                      81                38      71          72           78           85         61           47          89         735
2020-07-27  61                      78                      90                61      86          75           76           96         32           74          75         804
2020-07-24  80                      77                      87                73      87          72           83           100        32           56          96         843
2020-07-23  84                      81                      90                73      90          85           91           100        38           73          96         901
2020-07-22  88                      90                      90                84      92          93           94           100        45           90          96         962
2020-07-21  76                      91                      93                96      92          93           93           100        25           85          92         936
2020-07-20  65                      81                      81                34      62          91           84           96         32           87          89         802
2020-07-17  76                      86                      93                38      65          95           91           96         51           77          100        868
2020-07-16  80                      90                      93                50      81          93           89           96         22           70          85         849
2020-07-15  80                      96                      87                53      78          95           91           96         45           76          75         872
2020-07-14  69                      59                      81                23      53          82           73           96         25           60          82         703
2020-07-13  57                      34                      69                0       46          54           56           71         9            43          75         514
2020-07-10  61                      44                      66                0       43          59           39           60         35           66          64         537
2020-07-09  46                      31                      42                0       18          61           36           32         32           61          46         405
2020-07-08  50                      42                      57                3       34          67           50           46         25           61          57         492
2020-07-07  53                      34                      60                0       18          66           43           75         22           50          46         467
2020-07-06  50                      52                      54                7       30          75           64           89         41           76          53         591

Now I'd like to plot a heatmap by using matplotlib. The resulting heatmap should look something like this:

enter image description here

For the inner part (columns besides "sum"), if the value is above 50, then the color should be green, and the color should be darker for the largest values. Same logic for the values below 50.

For the "sum" column, the threshold is 550. How to achieve the gradual change in color?

Upvotes: 1

Views: 1925

Answers (2)

JohanC
JohanC

Reputation: 80459

A sns.diverging_palette(20, 145) standard has white in the center. Possible hue values for red are 20, and 145 for green.

vmin= will then set the numeric value corresponding to red and vmax= for the value corresponding to green. The value in the center will be white.

You need to create 2 separate heatmaps as they have different color ranges. The ax= keyword tells on which subplot the heatmap should be created. The colorbars can be left out: the numbers inside the cells already indicate the correspondence.

A newline character in the label names helps to better use the available space.

from matplotlib import pyplot as plt
import seaborn as sns
import pandas as pd

# df = pd.read_csv(...)
# df.set_index('Date', inplace=True)
column_labels = [col.replace(' ', '\n') for col in df.columns[:-1]]
fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(12, 10),
                               gridspec_kw={'width_ratios': [10, 1], 'wspace': 0.02, 'bottom': 0.14})
cmap = sns.diverging_palette(20, 145)
sns.heatmap(df[df.columns[:-1]], cmap=cmap, vmin=0, vmax=100, annot=True, fmt='.0f', annot_kws={'fontsize': 10},
            lw=0.6, xticklabels=column_labels, cbar=False, ax=ax1)
sns.heatmap(df[df.columns[-1:]], cmap=cmap, vmin=0, vmax=1100, annot=True, fmt='.0f', annot_kws={'fontsize': 10},
            lw=0.6, yticklabels=[], cbar=False, ax=ax2)
ax2.set_ylabel('')
ax2.tick_params(axis='x', labelrotation=90)
plt.show()

resulting plot

Upvotes: 3

300
300

Reputation: 323

plt.figure(figsize=(15, 15))
sns.heatmap(data, annot=True, cmap="YlGnBu", linewidths=.5)

enter image description here

Is that what you are looking for. and also If you want to add range on the values you can use vmin, vmax parameters.

Upvotes: 0

Related Questions