Python 当我在jupyter笔记本中使用matplotlib时,它总是会引发;matplotlib当前使用的是非GUI后端";错误?

Python 当我在jupyter笔记本中使用matplotlib时,它总是会引发;matplotlib当前使用的是非GUI后端";错误?,python,matplotlib,jupyter-notebook,Python,Matplotlib,Jupyter Notebook,当我运行learning_curves()函数时,它显示: UserWarning:C:\Users\Administrator\Anaconda3\lib\site packages\matplotlib\figure.py:397:UserWarning:matplotlib当前正在使用非GUI后端,因此无法显示该图 您不需要“fig.show()”这一行。把它拿走。那么就不会有警告信息了 您可以通过以下方式更改matplotlib使用的后端: import matplotlib.pyplo

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

UserWarning:C:\Users\Administrator\Anaconda3\lib\site packages\matplotlib\figure.py:397:UserWarning:matplotlib当前正在使用非GUI后端,因此无法显示该图


您不需要“fig.show()”这一行。把它拿走。那么就不会有警告信息了

您可以通过以下方式更改matplotlib使用的后端:

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()
在您的第1行
之前导入matplotlib.pyplot作为pl
,因为它必须先设置。有关更多信息,请参阅

(还有其他后端选项,但当我遇到类似问题时,将后端更改为
TkAgg
对我来说很有效)

使用我刚刚添加的

import matplotlib
matplotlib.use('TkAgg')

看起来不错,但有点颠簸。我不得不时不时地停止内核:-(

我试图使3d集群与类似。我最初认为
fig.show()
可能是正确的,但得到了相同的警告。。。 简要查看..但后来我尝试了
plt.show()
,它完全按照预期显示了我的3d模型。我想这也有道理。 这相当于您的
pl.show()

使用python 3.5和Jupyter笔记本电脑

导入时添加有助于在笔记本电脑中平滑绘图

%matplotlib notebook
将matplotlib的后端设置为“内联”后端:
使用此后端,绘图命令的输出将在前端(如Jupyter笔记本)内联显示,直接显示在生成它的代码单元的下方。生成的绘图也将存储在笔记本文档中。

如果您使用任何分析库(如pandas_profiling),请尝试注释它们并执行代码。在我的例子中,我使用pandas_评测为样本列车数据生成报告。注释导入pandas_评测帮助我解决了问题。

%matplotlib笔记本适合我


但是加载需要时间,但很明显。

我也有同样的错误。然后我使用了

导入matplotlib
matplotlib.use('WebAgg')

它工作正常。(您必须安装tornado才能在web上查看,(
pip安装tornado

Python版本:3.7 matplotlib版本:3.1.1

当我试图使用命令
fig.show()
显示绘图时,也出现了错误“matplotlib当前使用的是非GUI后端”。我发现在Jupyter笔记本中,命令
fig,ax=plt.subplots()
和绘图命令必须位于同一单元格中,才能渲染绘图

例如,以下代码将成功显示输入输出[5]的条形图:

在[3]中:

%matplotlib inline
import matplotlib.pyplot as plt
在[4]中:

import matplotlib.pyplot as plt
%matplotlib inline
在[5]中:

x = 'A B C D E F G H'.split()
y = range(1, 9)
fig, ax = plt.subplots()
ax.bar(x, y)
Out[5]:(8位艺术家的容器对象)

另一方面,以下代码将不显示绘图

在[5]中:

x = 'A B C D E F G H'.split()
y = range(1, 9)
fig, ax = plt.subplots()
ax.bar(x, y)
出[5]:

在[6]中:

fig, ax = plt.subplots()
Out[6]:(8位艺术家的容器对象)


在Out[6]中,只有“8位艺术家的容器对象”的语句,但没有显示条形图。

您仍然可以通过图savefig()保存图形

如果你想在网页上查看,你可以试试

ax.bar(x, y)

只需键入
fig
而不是
fig.show()

您将matplotlib.pyplot导入为pl。最后键入pl.show()而不是fig。show()

您使用的matplotlib版本是什么?您可以使用
导入matplotlib
打印(matplotlib
1.5.1,最新版本。添加“%pylab inline”"MPLOTTLIB版本3.0.3。如果你只使用Juyter来编码而不是生产,那就忽略它。它将在JupyterCan之外消失。请编辑你的答案来解释如何解决这个问题,而不是解释你经历过的过程。你也应该考虑看看TH。未来的e文章:)不会有警告信息,但人们会如何看待这个图形。我正在处理Pycharm,当在没有fig.show()的情况下执行时,它甚至不会显示图形。请问怎么办?如果绘图是笔记本单元格中的最后一个对象,那么jupyter会尝试渲染它。如果您有多个绘图或进一步的输出,并且排除了
fig.show()
s,那么您将看不到图形。换句话说,一种选择是简单地将最后一行设置为
fig
,这尤其适用于PyCharm 2019.1+中的新jupyter实现。您应该尝试给出一个解决问题或有助于解决问题的答案。像这样的一般响应对OP没有帮助。
%matplotlib笔记本
提供了一个交互式、可平移和可缩放的图形。这对我很有用!塔克斯!我只需要用于验证目的,不需要生产中的图形,因此它对我有效。这有效,正如上面的注释一样,只需使用
fig
作为单元格的最后一行。这为我解决了问题,谢谢