Issue
I am using the below code to generate a confusion matrix using the Sklearn library. But while saving the image the y-axis label i.e. True label is not printed completely. It is shown . In the python console, it is printed okay, But I need a high-resolution image and hence I need to save the image. Also, the publisher wants only tiff or pdf format.
disp=ConfusionMatrixDisplay(confusion_matrix=cm1,display_labels=['anger','bordome','disgust','fear', 'happiness','sadness' ,'neutral'])
font={'size':'30'}
plt.rc('font',**font)
plt.rcParams['figure.figsize']=[20,20]
disp.plot(cmap='Blues',values_format='0.2f')
plt.xticks(rotation=45)
plt.savefig("Fig.5.tif",dpi=30)
plt.show()
Also can I remove both the axis labels somehow? As that would also solve my problem. Thanks
Solution
The picture is a matplotlib plot. So, to remove the ticks for each axis and the labels, you can use set_ticks([])
which will remove both. I am using the sample from here to create a confusion matrix. The get_gca()
will get the axis for the plot. Setting the set_ticks to blank will remove the ticks and labels. Of course the bbox_inches=tight
will be required as mentioned by @endive1783. The code is here. I have made the plot a little smaller than 30x30, but should work for any size.
import matplotlib.pyplot as plt
from sklearn.datasets import make_classification
from sklearn.metrics import ConfusionMatrixDisplay, confusion_matrix
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
X, y = make_classification(random_state=0)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
clf = SVC(random_state=0)
clf.fit(X_train, y_train)
SVC(random_state=0)
predictions = clf.predict(X_test)
cm = confusion_matrix(y_test, predictions, labels=clf.classes_)
disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=clf.classes_)
font={'size':'15'}
plt.rc('font',**font)
plt.rcParams['figure.figsize']=[6,6]
disp.plot(cmap='Blues',values_format='0.2f')
#plt.xticks(rotation=45)
#plt.savefig("Fig.5.tif",dpi=30)
plt.gca().axes.get_xaxis().set_ticks([])
plt.gca().axes.get_yaxis().set_ticks([])
plt.savefig("Fig.5.tif",dpi=30, bbox_inches = 'tight')
plt.show()
plot
Update to keep tick labels, but remove axes labels
Replace the two set_ticks()
rows with below.
plt.gca().xaxis.label.set_visible(False)
plt.gca().yaxis.label.set_visible(False)
Answered By - Redox
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.