Search code examples
pythonpython-3.xmatplotlibscikit-learnconfusion-matrix

Python adjust scale intensity at confusion matrix plot


I have a number of confusion matrix plots with numbers not summing up to the same sum (the numbers are out of 100 for a benchmark) Please see attached example image below: confusion matrix I do not want the 22 and the 32 have the same color intensity, but be at the same scale from 0 to 100 intensity levels. How can I adjust the scale in python given the following used code:

import matplotlib.pyplot as plt
from sklearn.datasets import make_classification
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay

cm = confusion_matrix(y_true, y_pred, labels=["Up", "Down"])
disp = ConfusionMatrixDisplay(confusion_matrix=cm,display_labels=["Up", "Down"])
disp.plot(cmap="OrRd")

Solution

  • disp.ax_.get_images()[0].set_clim(0, 100)
    

    And the full code:

    import matplotlib.pyplot as plt
    from sklearn.datasets import make_classification
    from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay
    
    cm = confusion_matrix(y_true, y_pred, labels=["Up", "Down"])
    
    disp = ConfusionMatrixDisplay(confusion_matrix=cm,display_labels=["Up", "Down"])
    disp.plot(cmap="OrRd")
    
    disp.ax_.get_images()[0].set_clim(0, 100)