简体   繁体   English

如何在 Jupyter 中使用 matplotlib 后端“TkAgg”

[英]How to use matplotlib backend "TkAgg" in Jupyter

When running the following code in the Jupyter on Ubuntu 20.04:在 Ubuntu 20.04 上的 Jupyter 中运行以下代码时:

import matplotlib
try:
    matplotlib.use("TkAgg")
    import matplotlib.pyplot as plt
except ImportError as e:
    print(e)
    plt = None

assert plt is not None, "matplotlib backend failed"
print("done")

Here is the output:这是 output:

Cannot load backend 'TkAgg' which requires the 'tk' interactive framework, as 'headless' is currently running

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
/tmp/ipykernel_5612/97292621.py in <cell line: 9>()
      7     plt = None
      8 
----> 9 assert plt is not None, "matplotlib backend failed"
     10 print("done")

AssertionError: matplotlib backend failed断言错误:matplotlib 后端失败

But I can run it directy from python3 command line without any problem.但我可以直接从python3命令行运行它,没有任何问题。

Is there any python or system package need to be import or install first for running it in Jupyter notebook?是否需要先导入或安装任何pythonsystem package 才能在 Jupyter 笔记本中运行它?

Try running this command on a empty cell:尝试在空单元格上运行此命令:

%matplotlib tk

Then, execute your plotting commands.然后,执行您的绘图命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM