pythonpandasyellowbrick

Adjust figure yellow bricks model - python


I am trying to adjust the axes limits on a yellow bricks figure. However, I can't seem to adjust it. I can change axes labels and titles but not the limits. It works if I don't render the figure with visualizer.show() but then I lose labels, titles, legend etc.

from sklearn.linear_model import RidgeClassifier
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import OrdinalEncoder, LabelEncoder
from yellowbrick.classifier import ROCAUC
from yellowbrick.datasets import load_game
import matplotlib.pyplot as plt

X, y = load_game()

X = OrdinalEncoder().fit_transform(X)
y = LabelEncoder().fit_transform(y)

fig, ax = plt.subplots()
ax.set_xlim([-0.05, 1.0])
ax.set_ylim([0.0, 1.05])

X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)

fig, ax = plt.subplots(figsize = (10,6))

model = RidgeClassifier()
visualizer = ROCAUC(model, classes=["win", "loss", "draw"], ax = ax)

visualizer.fit(X_train, y_train)       
visualizer.score(X_test, y_test)        
visualizer.show()                       

Solution

  • Instead of calling the visualizer.show() method, you can try calling the visualizer.finalize() method and then accessing the underlying matplotlib axes to change the limits. You are also overwriting ax which wasn't doing you any favours either.

    Here is the full code example:

    from sklearn.linear_model import RidgeClassifier
    from sklearn.model_selection import train_test_split
    from sklearn.preprocessing import OrdinalEncoder, LabelEncoder
    from yellowbrick.classifier import ROCAUC
    from yellowbrick.datasets import load_game
    import matplotlib.pyplot as plt
    
    X, y = load_game()
    
    X = OrdinalEncoder().fit_transform(X)
    y = LabelEncoder().fit_transform(y)
    
    X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
    
    fig, ax = plt.subplots(figsize = (10,6))
    
    model = RidgeClassifier()
    visualizer = ROCAUC(model, classes=["win", "loss", "draw"], ax=ax)
    
    visualizer.fit(X_train, y_train)       
    visualizer.score(X_test, y_test)        
    visualizer.finalize()   
    ax.set_xlim([-0.05, 1.0])
    ax.set_ylim([0.0, 1.05])