The Python Oracle

How to display all label values in matplotlib

This video explains
How to display all label values in matplotlib

--

Become part of the top 3% of the developers by applying to Toptal
https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Quiet Intelligence

--

Chapters
00:00 Question
00:54 Accepted answer (Score 79)
01:19 Answer 2 (Score 0)
02:10 Thank you

--

Full question
https://stackoverflow.com/questions/2613...

Answer 1 links:
[image]: https://i.stack.imgur.com/jJkzG.png

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #graph #matplotlib #axislabels

#avk47



ACCEPTED ANSWER

Score 88


The issue here is that the number of ticks -set automatically - isn’t the same as the number of points in your plot.

To resolve this, set the number of ticks:

ax1.set_xticks(np.arange(len(x)))

Before the ax1.set_xticklabels(x) call.




ANSWER 2

Score 0


or better

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

from other answers in SO

from matplotlib import ticker
import numpy as np

labels = [
    "tench",
    "English springer",
    "cassette player",
    "chain saw",
    "church",
    "French horn",
    "garbage truck",
    "gas pump",
    "golf ball",
    "parachute",
]
fig = plt.figure()
ax = fig.add_subplot(111)
plt.title('Confusion Matrix', fontsize=18)
data = np.random.random((10,10))
ax.matshow(data, cmap=plt.cm.Blues, alpha=0.7)
ax.set_xticklabels([''] + labels,rotation=90)
ax.set_yticklabels([''] + labels)
ax.xaxis.set_major_locator(ticker.MultipleLocator(1))
ax.yaxis.set_major_locator(ticker.MultipleLocator(1))
for i in range(data.shape[0]):
    for j in range(data.shape[1]):
        ax.text(x=j, y=i,s=int(data[i, j]), va='center', ha='center', size='xx-small')


plt.xlabel('Predicted')
plt.ylabel('True')
plt.show()

enter image description here