0

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.

2
  • Try this: ax = plt.gca();ax.spines['left'].set_visible(False);ax.spines['right'].set_visible(False) ax.spines['top'].set_visible(False);ax.spines['bottom'].set_visible(False);ax.tick_params('x', length=0, which='major');ax.tick_params('y', length=0, which='major') Commented Nov 25, 2021 at 13:55
  • I tried it but it only removes black lines at left and right not the pins though. But combinig this with the answer below gives me what I want. Thank you! Commented Nov 25, 2021 at 13:58

1 Answer 1

1

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.