Yellowbrick: Increasing font size on Yellowbrick generated charts

百般思念 提交于 2019-12-07 13:49:20

问题


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.


回答1:


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.poof()

This results in the following image being generated:

You can start to manage the figure right after score and before poof 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.poof()

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

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 poof calls a finalize function, so some things like the title, axis labels, etc. should be modified after calling poof, or by short-circuiting poof 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.



来源:https://stackoverflow.com/questions/47450804/yellowbrick-increasing-font-size-on-yellowbrick-generated-charts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!