pythonmatplotlibscikit-learnyellowbrick

Yellowbrick: Increasing font size on Yellowbrick generated charts


Is there a way to increase the font size on Yellowbrick generated charts? I find it difficult to read the text. I wasn't able to find anything on it in the documentation.

I'm using Python 3.6, Yellowbrick 0.5 in a Jupyter Notebook.

enter image description here


Solution

  • Update: The yellowbrick API now uses viz.show instead of viz.poof.

    Yellowbrick wraps matplotlib in order to produce visualizations, so you can influence all the visual settings for the figure by making calls directly to matplotlib. I find that the easiest way to do that is by getting access to the Visualizer.ax property and setting things directly there, though of course, you can use plt directly to manage the global figure.

    Here's some code that produces an example similar to yours:

    import pandas as pd 
    
    from yellowbrick.classifier import ConfusionMatrix 
    from sklearn.ensemble import AdaBoostClassifier
    from sklearn.model_selection import train_test_split as tts
    
    data = pd.read_csv('examples/data/occupancy/occupancy.csv') 
    
    features = ["temperature", "relative humidity", "light", "C02", "humidity"]
    
    # Extract the numpy arrays from the data frame 
    X = data[features].as_matrix()
    y = data.occupancy.as_matrix()
    
    X_train, X_test, y_train, y_test = tts(X, y, test_size=0.2)
    
    clf = AdaBoostClassifier()
    viz = ConfusionMatrix(clf)
    
    viz.fit(X_train, y_train)
    viz.score(X_test, y_test)
    viz.show()
    

    This results in the following image being generated:

    Small Font Confusion Matrix

    You can start to manage the figure right after score and before show as follows:

    viz.fit(X_train, y_train)
    viz.score(X_test, y_test)
    
    for label in viz.ax.texts:
        label.set_size(12)
    
    viz.show()
    

    This produces the following image, with slightly larger fonts on the inside:

    Large Font Confusion Matrix

    What's happening here is that I'm directly accessing the matplotlib Axes object on the visualizer that contains all the elements of the drawing. The labels in the middle of the grid are Text objects, so I loop through all the text objects setting their size to 12pt. This technique can be used to modify any visual element before display if required (typically I use it to put annotations on the visualization).

    Note, however, that show calls a finalize function, so some things like the title, axis labels, etc. should be modified after calling show, or by short-circuiting show by calling finalize then plt.show().

    This particular code only works with the ConfusionMatrix, but I've added an issue in the Yellowbrick library to hopefully make this easier or at least more readable in the future.