Home > Software engineering >  Hide matplotlib plot axis ruler pins
Hide matplotlib plot axis ruler pins

Time:11-25

I want to remove ruler pins of x-axis and y-axis from plot. Here's my code :

import numpy as np
import matplotlib.pyplot as plt

normalised_confusion_matrix  = np.array(
[[160,1,0,1,0,0,1,1,1,2,2],
[4,46,0,0,0,0,0,0,0,0,0],
[3,0,77,0,0,0,0,0,0,0,0],
[12,0,0,136,0,0,0,0,0,0,0],
[11,0,0,0,55,0,0,0,0,0,0],
[22,0,0,0,0,66,0,0,0,0,0],
[1,0,0,0,0,0,26,0,0,0,0],
[27,0,0,0,0,0,0,232,0,0,0],
[10,0,1,0,0,0,0,0,60,0,0],
[6,0,0,0,0,0,0,0,0,37,0],
[12,0,0,0,0,0,0,0,0,0,71]]
)

classes = ['0', '1','2', '3', '4', '5', '6', '7', '8', '9', '10']

fig = plt.figure(figsize=(14, 10))
plt.imshow(
    normalised_confusion_matrix,
    interpolation='nearest',
    cmap=plt.cm.rainbow
)
plt.title("Confusion Matrix", fontsize=15, weight='bold')
cbar = plt.colorbar()
cbar.ax.tick_params(labelsize=15)

tick_marks = np.arange(11)
plt.xticks(tick_marks, classes, rotation=35, fontsize=10, weight='bold')
plt.yticks(tick_marks, classes, rotation=35, fontsize=10, weight='bold')

plt.tight_layout()
plt.grid(False)
plt.ylabel('Groundtruth label', fontsize=13, weight='bold')
plt.xlabel('Predicted label', fontsize=13, weight='bold')
plt.show()

enter image description here

I want to remove the black lines at left and right of the plot and also remove those little ruler pins on all axis. How can I do this ? I tried to set the plot grid to false, set axis xticks and yticks to none but none of them worked.

CodePudding user response:

Use plt.tick_params:

normalised_confusion_matrix  = np.array(
[[160,1,0,1,0,0,1,1,1,2,2],
[4,46,0,0,0,0,0,0,0,0,0],
[3,0,77,0,0,0,0,0,0,0,0],
[12,0,0,136,0,0,0,0,0,0,0],
[11,0,0,0,55,0,0,0,0,0,0],
[22,0,0,0,0,66,0,0,0,0,0],
[1,0,0,0,0,0,26,0,0,0,0],
[27,0,0,0,0,0,0,232,0,0,0],
[10,0,1,0,0,0,0,0,60,0,0],
[6,0,0,0,0,0,0,0,0,37,0],
[12,0,0,0,0,0,0,0,0,0,71]]
)

classes = ['0', '1','2', '3', '4', '5', '6', '7', '8', '9', '10']

fig = plt.figure(figsize=(14, 10))
plt.imshow(
    normalised_confusion_matrix,
    interpolation='nearest',
    cmap=plt.cm.rainbow
)
plt.title("Confusion Matrix", fontsize=15, weight='bold')
cbar = plt.colorbar()
cbar.ax.tick_params(labelsize=15)


plt.tick_params(
    axis='both',          # changes apply to both axes
    which='both',      # both major and minor ticks are affected
    bottom=False,      # ticks along the bottom edge are off
    top=False,         # ticks along the top edge are off
    left=False,         # ticks along the left edge are off
    right=False)         # ticks along the right edge are off

tick_marks = np.arange(11)
plt.xticks(tick_marks, classes, rotation=35, fontsize=10, weight='bold')
plt.yticks(tick_marks, classes, rotation=35, fontsize=10, weight='bold')

plt.tight_layout()
plt.grid(False)
plt.ylabel('Groundtruth label', fontsize=13, weight='bold')
plt.xlabel('Predicted label', fontsize=13, weight='bold')
plt.show()
  • Related