Skip to content
Market Index Diagonal Correlation Plot-Complete
Market index correlation with a diagonal correlation plot
# Load packages
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
sns.set_theme(style="darkgrid")
%config InlineBackend.figure_format = 'retina'
# Upload your data as CSV and load as data frame
df = pd.read_csv('Market_index_data correlation.csv')
df.head()
# Compute the correlation matrix
corr = df.corr(method = 'pearson')
# Generate a mask for the upper triangle
mask = np.triu(np.ones_like(corr, dtype=bool))
# Set up the matplotlib figure
fig, ax = plt.subplots(figsize=(11, 9)) # Set figure size
# Generate a custom diverging colormap
cmap = sns.diverging_palette(230, 20, as_cmap=True)
# Draw the heatmap with the mask
sns.heatmap(corr,
mask = mask,
cmap = cmap,
vmax = 1, # Set scale min value
vmin = -1, # Set scale min value
center = 0, # Set scale min value
square = True, # Ensure perfect squares
linewidths = 1.5, # Set linewidth between squares
cbar_kws = {"shrink": .9}, # Set size of color bar
annot = True # Include values within squares
);
plt.xticks(rotation=45) # Rotate x labels
plt.yticks(rotation=45) # Rotate y labels
# plt.xlabel('X Axis Title', size=20) # Set x axis title
# plt.ylabel('Y Axis Title', size=20) # Set y axis title
plt.title('Diagonal Correlation Plot', size=30, y=1.05); # Set plot title and position