当我在 jupyter notebook 中使用 matplotlib 时,它总是引发"matplotlib 当前使用的是非 GUI 后端";错误? [英] When I use matplotlib in jupyter notebook,it always raise " matplotlib is currently using a non-GUI backend" error?

查看:43
本文介绍了当我在 jupyter notebook 中使用 matplotlib 时,它总是引发"matplotlib 当前使用的是非 GUI 后端";错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import matplotlib.pyplot as pl
%matplot inline
def learning_curves(X_train, y_train, X_test, y_test):
""" Calculates the performance of several models with varying sizes of training data.
    The learning and testing error rates for each model are then plotted. """

print ("Creating learning curve graphs for max_depths of 1, 3, 6, and 10. . .")

# Create the figure window
fig = pl.figure(figsize=(10,8))

# We will vary the training set size so that we have 50 different sizes
sizes = np.rint(np.linspace(1, len(X_train), 50)).astype(int)
train_err = np.zeros(len(sizes))
test_err = np.zeros(len(sizes))

# Create four different models based on max_depth
for k, depth in enumerate([1,3,6,10]):

    for i, s in enumerate(sizes):

        # Setup a decision tree regressor so that it learns a tree with max_depth = depth
        regressor = DecisionTreeRegressor(max_depth = depth)

        # Fit the learner to the training data
        regressor.fit(X_train[:s], y_train[:s])

        # Find the performance on the training set
        train_err[i] = performance_metric(y_train[:s], regressor.predict(X_train[:s]))

        # Find the performance on the testing set
        test_err[i] = performance_metric(y_test, regressor.predict(X_test))

    # Subplot the learning curve graph
    ax = fig.add_subplot(2, 2, k+1)

    ax.plot(sizes, test_err, lw = 2, label = 'Testing Error')
    ax.plot(sizes, train_err, lw = 2, label = 'Training Error')
    ax.legend()
    ax.set_title('max_depth = %s'%(depth))
    ax.set_xlabel('Number of Data Points in Training Set')
    ax.set_ylabel('Total Error')
    ax.set_xlim([0, len(X_train)])

# Visual aesthetics
fig.suptitle('Decision Tree Regressor Learning Performances', fontsize=18, y=1.03)
fig.tight_layout()
fig.show()

当我运行 learning_curves() 函数时,它显示:

when I run the learning_curves() function, it shows:

UserWarning:C:UsersAdministratorAnaconda3libsite-packagesmatplotlibfigure.py:397: UserWarning: matplotlib 当前使用的是非 GUI 后端,因此无法显示图

UserWarning:C:UsersAdministratorAnaconda3libsite-packagesmatplotlibfigure.py:397: UserWarning: matplotlib is currently using a non-GUI backend, so cannot show the figure

推荐答案

你不需要fig.show()"这一行.只需将其删除.那么它将没有警告消息.

You don't need the line of "fig.show()". Just remove it. Then it will be no warning message.

这篇关于当我在 jupyter notebook 中使用 matplotlib 时,它总是引发"matplotlib 当前使用的是非 GUI 后端";错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆