Skip to content

Instantly share code, notes, and snippets.

@naenumtou
Created December 22, 2022 14:03
Show Gist options
  • Save naenumtou/1672e9b1430042f9052f9d0794e2a8f7 to your computer and use it in GitHub Desktop.
Save naenumtou/1672e9b1430042f9052f9d0794e2a8f7 to your computer and use it in GitHub Desktop.
# Set auto reload
%reload_ext autoreload
%autoreload 2
# Import libraries
import warnings
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import math
# Config
%config InlineBackend.figure_format = 'retina' #Retina display
pd.options.display.float_format = '{:.4f}'.format #Change display format in DataFrame table
warnings.filterwarnings('ignore') #Disable warning
plt.style.use('seaborn-deep') #Plot style
# Define plot matrix function
def plotMatrix(data, plotName, format = None):
plt.figure(figsize = (10, 6))
plt.title(f'{plotName}')
if format is None:
formatType = '.4f'
cmapType = 'RdYlGn'
else:
formatType = '.2%'
cmapType = 'RdYlGn_r'
ax = sns.heatmap(
data,
annot = True,
fmt = formatType,
xticklabels = True,
yticklabels = True,
cmap = cmapType,
cbar = False
)
ax.xaxis.tick_top() # x-axis on top
ax.xaxis.set_label_position('top')
return plt.show()
# Import data
matrix = pd.read_csv(
'https://raw.githubusercontent.com/naenumtou/ifrs9/main/PD/datasets/exampleMatrix.csv'
).set_index('Rating')
# Show matrix
plotMatrix(matrix, 'Example 1-year transition matrix', 'Percent')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment