Make it easy to change font size of values in plot_confusion_matrix
See original GitHub issueFeature Request
It would be great to have an additional parameter in the plot_confusion_matrix function to easily change the font size of the values in the confusion matrix. Currently, there is only a parameter for formatting the values (defaults of d
or .2g
, whichever is shorter).
Proposed Solution
A values_size
parameter that is basically the equivalent of Matplotlib’s fontsize
functionality.
Current Workaround
The only workaround I’ve found success with is changing Matplotlib’s global settings for font size in plt.rcParams["font-size"]
, but that ends up changing the font size of everything else in the plot, so then I have to manually adjust everything else (i.e. ax.set_xlabel
’s font size, ax.set_ylabel
’s fontsize, etc.)
Additional Context
Here is the example from https://scikit-learn.org/stable/auto_examples/model_selection/plot_confusion_matrix.html:
import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm, datasets
from sklearn.model_selection import train_test_split
from sklearn.metrics import plot_confusion_matrix
# import some data to play with
iris = datasets.load_iris()
X = iris.data
y = iris.target
class_names = iris.target_names
# Split the data into a training set and a test set
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
# Run classifier, using a model that is too regularized (C too low) to see
# the impact on the results
classifier = svm.SVC(kernel='linear', C=0.01).fit(X_train, y_train)
np.set_printoptions(precision=2)
# Plot non-normalized confusion matrix
titles_options = [("Confusion matrix, without normalization", None),
("Normalized confusion matrix", 'true')]
for title, normalize in titles_options:
disp = plot_confusion_matrix(classifier, X_test, y_test,
display_labels=class_names,
cmap=plt.cm.Blues,
normalize=normalize)
disp.ax_.set_title(title)
print(title)
print(disp.confusion_matrix)
plt.show()
Summary/Tl;dr
It would be awesome if the plot_confusion_matrix
could just have a values_size
parameter in there, so the argument passed would simply be whatever font size you desire:
disp = plot_confusion_matrix(classifier, X_test, y_test,
display_labels=class_names,
cmap=plt.cm.Blues,
normalize=normalize,
values_size=16)
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:8 (5 by maintainers)
Top GitHub Comments
I think that the idea with our plotting API is to add the minimum number of parameters to control the rendering and let use the
matplotlib
objects to get what one wants. If it is actually complicated to achieve the intended result with some code, then we could add a keyword. Here, this is relatively easy to tweak the rendering:Basically in this case one could do:
So I would be -1 for adding a new keyword.
Hm I think there might be too many different things to configure to do it via keywords. Matplotlib has a lot of options. Btw, my “preferred” way to make the font bigger is to make the figsize smaller 😉