Как показать все метки в сюжете. У меня есть 12 меток, из которых я должен построить матрицу путаницы. Но только 6 из них видны - PullRequest
1 голос
/ 28 апреля 2020

Я хочу построить матрицу путаницы из 12 данных, поэтому я создал 12 меток, чтобы построить матрицу путаницы, график идет правильно с 12 данными, но метки x и y показаны только наполовину.

Я использовал этот фрагмент -:

import matplotlib.pyplot as plt

labels = ['1','2','3','4','5','6','7','8','9','10','11','12']
cm = confusion_matrix(actualList, predictList, labels)
print(cm)
fig = plt.figure()
fig.set_figheight(10)
fig.set_figwidth(10)
ax = fig.add_subplot()
cax = ax.matshow(cm)
plt.title('Confusion matrix of the classifier',pad=-570)
fig.colorbar(cax)
ax.set_xticklabels([''] + labels)
ax.set_yticklabels([''] + labels)
plt.setp(ax.get_xticklabels(), rotation=30, ha="left",
         rotation_mode="anchor")
plt.xlabel('Predicted')
plt.ylabel('True')
plt.show()

и получил этот вывод:

enter image description here

1 Ответ

1 голос
/ 28 апреля 2020

Если у вас несколько категорий, matplotlib будет неправильно маркировать оси. Чтобы решить эту проблему, вы можете импортировать MultipleLocator из matplotlib.ticker, чтобы каждая ячейка была помечена.

import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator;

# the same values in your confusion matrix
labels = ['1','2','3','4','5','6','7','8','9','10','11','12']
cm = [[0, 0, 61, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 1099, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 131, 23, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 36, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 40, 0, 0, 3, 0, 0, 0, 0, 0, 0],
    [0, 0, 43, 0, 0, 0, 31, 0, 0, 0, 0, 0],
    [0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 269, 0, 0, 0, 0, 0, 86, 0, 0, 6],
    [0, 0, 101, 0, 0, 0, 0, 0, 0, 45, 0, 1],
    [0, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 283, 0, 0, 0, 0, 0, 0, 0, 0, 204]]

fig = plt.figure()
fig.set_figheight(10)
fig.set_figwidth(10)
ax = fig.add_subplot()
cax = ax.matshow(cm)
plt.title('Confusion matrix of the classifier',pad=-570)
fig.colorbar(cax)

ax.xaxis.set_major_locator(MultipleLocator(1))
ax.yaxis.set_major_locator(MultipleLocator(1))

ax.set_xticklabels([''] + labels)
ax.set_yticklabels([''] + labels)
plt.setp(ax.get_xticklabels(), rotation=30, ha="left",
         rotation_mode="anchor")
plt.xlabel('Predicted')
plt.ylabel('True')
plt.show()

enter image description here

...