0

I have a 3 confusion matrix , each one related to an algorithm ( SVM, LR , RF)

This is my code for the confusion matrix of SVM, LR and RF:

import seaborn as sn
import pandas as pd
import matplotlib.pyplot as plt
svm_A = [[11,1], 
        [2,2]]
df_cm = pd.DataFrame(svm_A, index = [i for i in "01"],
                  columns = [i for i in "01"])
plt.figure(figsize = (10,7))
sn.heatmap(df_cm, annot=True)

RF_A = [[12,0], 
        [3,1]]
df_cm = pd.DataFrame(RF_A, index = [i for i in "01"],
                  columns = [i for i in "01"])
plt.figure(figsize = (10,7))
sn.heatmap(df_cm, annot=True)

LR_A = [[11,1], 
        [2,2]]
df_cm = pd.DataFrame(RF_A, index = [i for i in "01"],
                  columns = [i for i in "01"])
plt.figure(figsize = (10,7))
sn.heatmap(df_cm, annot=True)

I want to put 3 plot together side by side.

How can I do it?

Sunderam Dubey
  • 1
  • 11
  • 20
  • 40

1 Answers1

0

You can use this as a more modular approach. This enables you to flexibly add more algorithms (add a new dict to data) and in a single place change the code for plotting each confusion matrix.

Code

import seaborn as sn
import pandas as pd
import matplotlib.pyplot as plt

svm_A = [[11,1],
        [2,2]]
rf_A = [[12,0],
        [3,1]]
lr_A = [[11,1],
        [2,2]]

data = [
    {"algorithm_name": "SVM", "confusion_matrix": svm_A},
    {"algorithm_name": "RF", "confusion_matrix": rf_A},
    {"algorithm_name": "LR", "confusion_matrix": lr_A},
]

n_algorithms = len(data)
fig, axs = plt.subplots(1, n_algorithms, figsize=(5 * n_algorithms, 4.5))

for i, algo_data in enumerate(data):
    df_cm = pd.DataFrame(algo_data["confusion_matrix"], index=[j for j in "01"],
                         columns=[j for j in "01"])
    sn.heatmap(df_cm, annot=True, ax=axs[i])
    axs[i].set_title(algo_data["algorithm_name"])

plt.tight_layout()
plt.show()

Plot outputted plots

Erik Fubel
  • 661
  • 1
  • 15