【问题标题】:Matplotlib widen plotMatplotlib 扩大情节
【发布时间】:2017-08-02 19:11:17
【问题描述】:

我正在绘制 sklearn 分类报告,生成的图非常狭窄,标签难以阅读。我使用帖子here 来获取绘图代码。

关于如何水平拉伸这个情节有什么建议吗?谢谢

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))

【问题讨论】:

    标签: python matplotlib scikit-learn


    【解决方案1】:

    默认情况下,坐标区将具有图像的纵横比。您可以通过将aspect 参数更改为imshow 来更改它。

    要么把它放到"auto",让图像扩展到轴的给定空间。
    或者,将其设置为任意数字,表示高宽比; number == height/width

    在这种情况下尝试

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

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

    并根据您的需要进行调整。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-14
      • 1970-01-01
      • 1970-01-01
      • 2023-03-03
      • 2014-06-07
      • 2017-05-28
      • 1970-01-01
      相关资源
      最近更新 更多