2017-03-12 3 views
0

私はSklearnの分類レポートをプロットしています。生成されたプロットは非常に狭く、ラベルを読みにくいです。プロットコードを取得するには、投稿hereを使用しました。Matplotlibはプロットを広げます

このプロットを水平に引き伸ばす方法についてのご意見はありますか?あなたは

def plot_classification_report(cr, title='Classification report ', with_avg_total=False, cmap=plt.cm.Blues): 

    lines = cr.split('\n') 

    classes = [] 
    plotMat = [] 
    for line in lines[2 : (len(lines) - 3)]: 
     #print(line) 
     t = line.split() 
     # print(t) 
     classes.append(t[0]) 
     v = [float(x) for x in t[1: len(t) - 1]] 
     #print(v) 
     plotMat.append(v) 

    if with_avg_total: 
     aveTotal = lines[len(lines) - 1].split() 
     classes.append('avg/total') 
     vAveTotal = [float(x) for x in t[1:len(aveTotal) - 1]] 
     plotMat.append(vAveTotal) 


    plt.imshow(plotMat, interpolation='nearest', cmap=cmap) 
    plt.title(title) 
    plt.colorbar() 
    x_tick_marks = np.arange(3) 
    y_tick_marks = np.arange(len(classes)) 
    plt.xticks(x_tick_marks, ['precision', 'recall', 'f1-score'], rotation=45) 
    plt.yticks(y_tick_marks, classes) 
    #plt.tight_layout() 
    plt.ylabel('Classes') 
    plt.xlabel('Measures') 

plot_classification_report(classification_report(y_test, y_pred)) 

plot_output_too_narrow

答えて

0

デフォルトでは、軸は画像のアスペクト比を持つことになりますありがとうございました。これを変更するには、にaspect引数を使用します。

"auto"に入力すると、画像が軸の指定されたスペースに拡張されます。
または、幅の高さを示す任意の数値に設定します。 number == height/width。この場合

plt.imshow(plotMat, interpolation='nearest', cmap=cmap, aspect="auto") 

または

plt.imshow(plotMat, interpolation='nearest', cmap=cmap, aspect=len(classes)/12.) 

を試してみて、自分のニーズに適合させます。

関連する問題