Visualizing SET Index Performance: Creating a Heat Map Chart

Apr 10, 2023

3 min read


programmer-with-chrome

What Idea about heat map chart ?


The code provided uses pandas, matplotlib, and seaborn to create a heatmap chart of the SET index.


It reads a CSV file containing daily SET index data from 1975 to 2021, resamples the data to weekly and monthly periods, and calculates the percentage change for the last ten years.


It then pivots the data to create a matrix where the rows represent years, the columns represent months, and the cells contain the percentage change in the SET index for each month and year.


It also calculates the average percentage change for each year and month and adds them as a row and column to the matrix. Finally, it creates a heatmap using seaborn to visualize the matrix.


Import the necessary libraries and the dataset


import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
%matplotlib inline
plt.rcParams["figure.figsize"] = (15,11)
sns.set_style('whitegrid')
warnings.filterwarnings("ignore")

Resample the dataset to weekly and monthly intervals


data = pd.read_csv('SET_Data_from1970-2021.csv',index_col='Date',parse_dates=True)
data

SymbolOpenHighLowCloseVolume
Date
1975-04-30SET100.00100.00100.00100.00163310
1975-05-02SET99.9699.9699.9699.96150220
1975-05-06SET99.5399.5399.5399.53260100
1975-05-07SET99.1399.1399.1399.1335480
1975-05-08SET97.8897.8897.8897.8852500
2021-12-24SET1642.641644.121635.191637.2221132799040
2021-12-27SET1641.281642.421632.791636.5020516253580
2021-12-28SET1640.971650.281640.611641.5225104505000
2021-12-29SET1643.431654.981641.751653.3324677866340
2021-12-30SET1657.291660.851652.371657.6221309077960

11452 rows × 6 columns


data.resample('W-Fri').agg({'Open':'first',
                                'High':'max',
                                'Low':'min',
                                'Close':'last'})

OpenHighLowClose
Date
1975-05-02100.00100.0099.9699.96
1975-05-0999.5399.5396.6496.64
1975-05-1695.3795.3789.4589.45
1975-05-2387.1191.9185.7391.91
1975-05-3090.6990.6988.8989.98
2021-12-031608.011617.551563.041588.19
2021-12-101600.171623.821595.141618.23
2021-12-171626.291646.851616.511641.73
2021-12-241631.221645.711612.961637.22
2021-12-311641.281660.851632.791657.62

2436 rows × 4 columns


Calculate the monthly returns for the last ten years and add them to a new dataframe

add monthly return


df_monthly = data.copy()
df_monthly = df_monthly.resample('M').agg({'Open':'first',
                                'High':'max',
                                'Low':'min',
                                'Close':'last'})
df_monthly

OpenHighLowClose
Date
1975-04-30100.00100.00100.00100.00
1975-05-3199.9699.9685.7389.98
1975-06-3089.5391.6489.4291.64
1975-07-3191.90100.1191.9098.02
1975-08-31100.00100.0098.1498.39
2021-08-311523.421643.761512.281638.75
2021-09-301643.011658.081591.811605.68
2021-10-311600.591651.411593.321623.43
2021-11-301627.591658.601565.951568.69
2021-12-311576.261660.851563.041657.62

561 rows × 4 columns


type(df_monthly[['Close']])
# use this

pandas.core.frame.DataFrame

df_monthly[['Close']]

Close
Date
1975-04-30100.00
1975-05-3189.98
1975-06-3091.64
1975-07-3198.02
1975-08-3198.39
2021-08-311638.75
2021-09-301605.68
2021-10-311623.43
2021-11-301568.69
2021-12-311657.62

561 rows × 1 columns


lasttenyear = df_monthly.pct_change().loc['2011':'2021'][['Close']]
lasttenyear

Close
Date
2011-01-31-0.066482
2011-02-280.024697
2011-03-310.060299
2011-04-300.043991
2011-05-31-0.018042
2021-08-310.076765
2021-09-30-0.020180
2021-10-310.011055
2021-11-30-0.033719
2021-12-310.056691

132 rows × 1 columns


lasttenyear=lasttenyear.reset_index()

lasttenyear['year'] = pd.DatetimeIndex(lasttenyear['Date'] ).year

lasttenyear['month'] = pd.DatetimeIndex(lasttenyear['Date'] ).month

lasttenyear

DateCloseyearmonth
02011-01-31-0.06648220111
12011-02-280.02469720112
22011-03-310.06029920113
32011-04-300.04399120114
42011-05-31-0.01804220115
1272021-08-310.07676520218
1282021-09-30-0.02018020219
1292021-10-310.011055202110
1302021-11-30-0.033719202111
1312021-12-310.056691202112

132 rows × 4 columns


lasttenyear.Date.apply(lambda x: x.strftime('%b'))

    0      Jan
    1      Feb
    2      Mar
    3      Apr
    4      May
          ... 
    127    Aug
    128    Sep
    129    Oct
    130    Nov
    131    Dec
    Name: Date, Length: 132, dtype: object

lasttenyear['smonth']=   lasttenyear.Date.apply(lambda x: x.strftime('%b'))

lasttenyear

DateCloseyearmonthsmonth
02011-01-31-0.06648220111Jan
12011-02-280.02469720112Feb
22011-03-310.06029920113Mar
32011-04-300.04399120114Apr
42011-05-31-0.01804220115May
1272021-08-310.07676520218Aug
1282021-09-30-0.02018020219Sep
1292021-10-310.011055202110Oct
1302021-11-30-0.033719202111Nov
1312021-12-310.056691202112Dec

132 rows × 5 columns


lasttenyear.dtypes

    Date      datetime64[ns]
    Close            float64
    year               int64
    month              int64
    smonth            object
    dtype: object

df=lasttenyear[['year','month','Close']]

df.pivot(index='year',columns='month').Close

month123456789101112
year
2011-0.0664820.0246970.0602990.043991-0.018042-0.0301260.088384-0.056002-0.1437690.0638940.0211130.030131
20120.0572020.0709710.0308980.026505-0.0708110.0268160.0231970.0234970.0580950.0000620.0193780.051275
20130.0591050.0457060.0126360.023574-0.022399-0.070528-0.019809-0.0905320.0686550.043176-0.049727-0.052818
2014-0.0188110.0400620.0384280.0281050.0005580.0494590.0112000.0394310.015394-0.0009520.006155-0.060380
20150.0558070.003643-0.0510830.013812-0.0201020.005682-0.042823-0.040073-0.0241680.034055-0.025263-0.052718
20160.0100620.0241280.056538-0.0021950.0140040.0145410.0547270.015990-0.0421260.0084340.0097080.021652
20170.022276-0.0112530.009971-0.005581-0.0029750.0083760.0008510.0254300.0352690.028814-0.0139310.033180
20180.0417120.001790-0.0294350.002167-0.029852-0.0760810.0665650.0116290.020231-0.049715-0.016350-0.047460
20190.0497800.007157-0.0089690.021280-0.0318490.067966-0.010616-0.033324-0.010695-0.021824-0.006806-0.006758
2020-0.041586-0.114666-0.1601320.1561470.031644-0.002845-0.007841-0.013451-0.056170-0.0340250.1785510.029141
20210.0121640.0203140.060416-0.0025710.006607-0.003640-0.0414850.076765-0.0201800.011055-0.0337190.056691

Display the pivoted dataframe as a heat map chart using the heatmap() function


pivot = df.pivot(index='year',columns='month').Close
pivot.columns = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']

import seaborn as sns
sns.set(rc = {'figure.figsize':(15,8)})
sns.heatmap(pivot,cmap='RdYlGn',annot=True,fmt='.2%')

png


df.groupby('year').mean()

monthClose
year
20116.50.001507
20126.50.026424
20136.5-0.004413
20146.50.012387
20156.5-0.011936
20166.50.015455
20176.50.010869
20186.5-0.008733
20196.50.001278
20206.5-0.002936
20216.50.011868

pivot['avg']=df.groupby('year').mean()['Close']


Modify the column names to show abbreviated month names and add a column to display the average monthly return for each year


sns.heatmap(pivot,cmap='RdYlGn',annot=True,fmt='.2%')

png


pivot.columns=[1,2,3,4,5,6,7,8,9,10,11,12,'avg']

Add a row to show the average monthly return for each month


pivot.loc['mavg']=df.groupby('month').mean()['Close']

pivot

123456789101112avg
year
2011-0.0664820.0246970.0602990.043991-0.018042-0.0301260.088384-0.056002-0.1437690.0638940.0211130.0301310.001507
20120.0572020.0709710.0308980.026505-0.0708110.0268160.0231970.0234970.0580950.0000620.0193780.0512750.026424
20130.0591050.0457060.0126360.023574-0.022399-0.070528-0.019809-0.0905320.0686550.043176-0.049727-0.052818-0.004413
2014-0.0188110.0400620.0384280.0281050.0005580.0494590.0112000.0394310.015394-0.0009520.006155-0.0603800.012387
20150.0558070.003643-0.0510830.013812-0.0201020.005682-0.042823-0.040073-0.0241680.034055-0.025263-0.052718-0.011936
20160.0100620.0241280.056538-0.0021950.0140040.0145410.0547270.015990-0.0421260.0084340.0097080.0216520.015455
20170.022276-0.0112530.009971-0.005581-0.0029750.0083760.0008510.0254300.0352690.028814-0.0139310.0331800.010869
20180.0417120.001790-0.0294350.002167-0.029852-0.0760810.0665650.0116290.020231-0.049715-0.016350-0.047460-0.008733
20190.0497800.007157-0.0089690.021280-0.0318490.067966-0.010616-0.033324-0.010695-0.021824-0.006806-0.0067580.001278
2020-0.041586-0.114666-0.1601320.1561470.031644-0.002845-0.007841-0.013451-0.056170-0.0340250.1785510.029141-0.002936
20210.0121640.0203140.060416-0.0025710.006607-0.003640-0.0414850.076765-0.0201800.011055-0.0337190.0566910.011868
mavg0.0164750.0102320.0017790.027749-0.013020-0.0009440.011123-0.003695-0.0090420.0075430.0081010.000176NaN

Display the modified heatmap chart using the heatmap() function with the desired settings.


pivot.columns = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec','avg']

sns.heatmap(pivot,cmap='RdYlGn',annot=True,fmt='.2%')

png


Finally, we plotted the updated pivot table as a heatmap using seaborn library.



Conclusion


In conclusion, the heat map chart provides an easy and efficient way to visualize data in a clear and concise manner.


The heatmap chart used in this code helps to visualize the SET index’s monthly percentage change for the last ten years, making it easier to understand the SET index’s performance over time.


The addition of the average percentage change for each year and month to the heatmap chart helps to provide further insights into the SET index’s performance.