The risk matrix, a popular risk assessment method 1, presents risks in the form of a likelihood / impact heatmap. In this post, I share a convenient and elegant Python snippet for constructing risk matrices, used to generate this hypothetical project risk assessment:
Below are the code details:
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
likelihod_cols = ["Rare","Unlikely","Moderate","Likely","Almost certain"]
impact_cols = ["Insignificant","Minor","Significant","Major","Severe"]
score_vals = np.arange(len(impact_cols)).reshape(-1,1) + np.arange(len(likelihod_cols)).reshape(1,-1)
cmap = mpl.colormaps["RdYlGn"].reversed().resampled(9)
norm = mpl.colors.BoundaryNorm(range(10),10)
fig, ax = plt.subplots(figsize=(8,8))
ax.imshow(score_vals,cmap=cmap,norm=norm)
annotations = {
(2,3): ["competition"],
(3,2): ["too ambitiuous"],
(3,3): ["hardware expertise"],
(2,2): ["time crunch"],
(2,1): ["stretched resources"],
}
annots_style = {'fontweight':'bold'}
for (i,j),text in annotations.items():
text = '\n'.join( text )
_ = ax.text(i, j, text, ha="center", va="center", color="b", **annots_style)
ax.set_xlabel('Impact',fontweight='bold')
ax.set_xticks(range(len(impact_cols)), labels=impact_cols)
ax.set_ylabel('Likelihood',fontweight='bold')
ax.set_yticks(range(len(likelihod_cols)), labels=likelihod_cols)
ax.invert_yaxis()
plt.tight_layout()
plt.show()
fig.savefig("risk_heatmap.svg")
- 1.Peace C. The risk matrix: uncertain results? Policy and Practice in Health and Safety. Published online July 3, 2017:131-144. doi:10.1080/14773996.2017.1348571