question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make it easy to change font size of values in plot_confusion_matrix

See original GitHub issue

Feature 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:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
glemaitrecommented, Dec 22, 2020

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:

disp.plot()
for labels in disp.text_.ravel():
    labels.set_fontsize(30)
plt.show()

So I would be -1 for adding a new keyword.

2reactions
amuellercommented, Feb 6, 2021

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 😉

Read more comments on GitHub >

github_iconTop Results From Across the Web

Confusion Matrix font size - Stack Overflow
Use sns.set to change the font size of the heatmap values. You can specify the font size of the labels and the title...
Read more >
Confusion Matrices - Kavi's Portfolio
Otherwise, take a look at the confusion matrix's functions' documentation ... fontsize=25, normalize=True, title=model.name.capitalize() + ': Test Set', ...
Read more >
How to customize font size of accuracies/elements of a ...
In my confusion matrix, I'm using one of the following two lines to change the font size of all the elements of a...
Read more >
How to Change Font Size in Seaborn Plots (With Examples)
Note that the default value for font_scale is 1. By increasing this value, you can increase the font size of all elements in...
Read more >
How can I change color and font size in plotconfusion figures?
I want to change the color of the fields of the confusion matrix and also to change the font size of the entries...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found