2016-04-30 7 views
1

Ich habe den folgenden Code in meinem Jupyter-Notebook. Ich habe verschiedene Möglichkeiten ausprobiert, um die Größe der Figur, die ich bisher im Internet gefunden habe, zu ändern, aber nichts scheint zu funktionieren. Auch sind die Art und Weise die Objekte erzeugt und verändert in matplotlib nicht wirklich Sinn für mich sowieso, also bin ich nicht sicher, wohin gehen wir ...Matplotlib: Format für steigende Figsize, gegebener Code-Layout

Anyways, mein Code, wie es steht, ist:

from sklearn.metrics import confusion_matrix 
import matplotlib.pyplot as plt 
import numpy as np 
%matplotlib inline 

def plot_confusion_matrix(cm, title='Confusion matrix',fontsize=8, names=None,cmap=plt.cm.Blues): 
    plt.imshow(cm, interpolation='nearest', cmap=cmap) 
    plt.title(title) 
    plt.colorbar() 
    for i in range(cm.shape[0]): 
     for j in range(cm.shape[1]): 
      plt.text(i, j, str(cm[i, j]), fontsize=fontsize) 
    if names != None: 
     tick_marks = np.arange(len(names)) 
     plt.xticks(tick_marks, names) 
     plt.yticks(tick_marks, names) 
    plt.tight_layout() 
    plt.ylabel('True label') 
    plt.xlabel('Predicted label') 



confusion_matrix_path = 'mnist_cnn/main_project/CNN_Completed_Models/mnist_cnn_C_confusion.npy' 
cmat = np.load(confusion_matrix_path) 
plot_confusion_matrix(cmat,"MNIST Net C: Confusion Matrix",fontsize=8, names=[0,1,2,3,4,5,6,7,8,9]) 




plt.figure(figsize=(20,20)) <----- This Line Here 




plt.show() 

Die plt.figure(figsize=(20,20)) Linie tut nichts. Es gibt auch keine von mehreren Korrekturen, die ich gefunden habe. Wie ändere ich die Abmessungen dieses Grundstücks?

+0

würde ich vorschlagen, '% matplotlib mit notebook' Sie interaktive Figuren so erhalten anstelle von toten PNGs. – tacaswell

+0

und Sie haben Probleme mit der Zustandsmaschine von 'pyplot' und verlassen sich darauf, den globalen Zustand der 'aktuellen Figur' zu verwenden, um zu bestimmen, welche Figur in Ihre Plot-Methoden plotten soll. Sie sollten stattdessen ein 'Axes'-Objekt in' plot_confusion_matrix' übergeben. Siehe http://matplotlib.org/faq/usage_faq.html#coding-styles – tacaswell

+0

und '' plt.figure' erstellt eine neue Figur (http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.figure) ändert nicht die Größe einer bestehenden Figur. – tacaswell

Antwort

0

Das ist verrückt, und es macht keinen Sinn für mich, aber die plt.figure(figsize=(a,b)) Zeile vor dem Plotten etwas zu setzen macht es funktioniert. Ich verstehe wirklich nicht, wie sie diese Objekte strukturiert haben.

from sklearn.metrics import confusion_matrix 
import matplotlib.pyplot as plt 
import numpy as np 
%matplotlib inline 

def plot_confusion_matrix(cm, title='Confusion matrix',fontsize=8, names=None,cmap=plt.cm.Blues): 


    plt.figure(figsize=(10,10))  <---- Should Actually Go Here 



    plt.imshow(cm, interpolation='nearest', cmap=cmap) 
    plt.title(title) 
    plt.colorbar() 
    for i in range(cm.shape[0]): 
     for j in range(cm.shape[1]): 
      plt.text(i, j, str(cm[i, j]), fontsize=fontsize) 
    if names != None: 
     tick_marks = np.arange(len(names)) 
     plt.xticks(tick_marks, names) 
     plt.yticks(tick_marks, names) 
    plt.tight_layout() 
    plt.ylabel('True label') 
    plt.xlabel('Predicted label') 



confusion_matrix_path = 'mnist_cnn/main_project/CNN_Completed_Models/mnist_cnn_C_confusion.npy' 
cmat = np.load(confusion_matrix_path) 
plot_confusion_matrix(cmat,"MNIST Net C: Confusion Matrix",fontsize=8, names=[0,1,2,3,4,5,6,7,8,9]) 




plt.figure(figsize=(10,10)) <----- This Line Here 




plt.show() 

Ich weiß nicht, warum es so funktioniert ... und ich würde auf jeden Fall eine Antwort akzeptieren, die es erklärt ...

+0

'plt.figure()' erstellt eine neue Figur. Wenn Sie bereits eine Figur haben, verwenden Sie 'fig.set_size_inches()'. Wenn Sie 'plt.-Befehle verwenden, verwenden Sie' fig = plt.gcf() ', um ** ** ** ** ** ** ** ** ** ** ** ** ** * ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** zu **. – MaxNoe

Verwandte Themen