【问题标题】:Interractive plots Ipython 5.0.0 in the terminal在终端中交互式绘图 Python 5.0.0
【发布时间】:2016-07-16 15:36:46
【问题描述】:

我在终端中运行 Ipthon,在 linux 中(不是 jupyter notebook)。

在 Ipython 2.4.1 中,以下命令会打开一个弹出式交互 Tk 绘图窗口:

(prodEnv)[kv@loowkv1 sandbox]$ ipython
IPython 2.4.1 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.


In [1]: 

In [1]: %matplotlib
Using matplotlib backend: TkAgg
In [2]: import matplotlib.pyplot as plt
In [3]: import numpy as np
In [4]: plt.ion()
In [5]: x = np.arange(0, 4*np.pi, 0.1)
In [6]: y = [np.sin(i) for i in x]
In [7]: plt.plot(x, y, 'g-', linewidth=1.5, markersize=4)
Out[7]: [<matplotlib.lines.Line2D at 0x7f5c8a8b6410>]

在 Ipython 5.0.0 中,当我使用相同的代码时:

(prodEnv)[kv@loowkv1 sandbox]$ ipython3
Python 3.4.1 (default, Nov  3 2014, 14:38:10) 
Type "copyright", "credits" or "license" for more information.

IPython 5.0.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: %matplotlib
   ...: import matplotlib.pyplot as plt
   ...: import numpy as np
   ...: plt.ion()
   ...: x = np.arange(0, 4*np.pi, 0.1)
   ...: y = [np.sin(i) for i in x]
   ...: plt.plot(x, y, 'g-', linewidth=1.5, markersize=4)
   ...: 
Using matplotlib backend: agg
Out[1]: [<matplotlib.lines.Line2D at 0x7fe49ae9c208>]

什么都没有出现。如何在 Iptyhon 5.0.0 中弹出交互式情节?

【问题讨论】:

  • %matplotlib 为您要使用的后端提供一个额外的可选参数。有关所有可用后端的列表,请参阅 %matplotlib --list
  • 好的,我应该选择哪一个来获得与 ipython 2.4.1 中相同的图?

标签: linux matplotlib terminal ipython


【解决方案1】:

从您的输出中使用ipython 使用TkAgg 即tk (python-tk) 作为后端。

使用 matplotlib 后端:TkAgg

但是对于ipython3,它是agg

使用 matplotlib 后端:agg

我尝试使用ipython3 和TkAgg 后端,它与ipython 一样工作得很好。 您可能需要安装python3-tk 才能将TkAgg 与matplotlib 一起使用

【讨论】:

  • 是的,我希望有一种方法可以在不使用 sudo 命令(来自虚拟环境)的情况下做到这一点,但这似乎是不可能的。
猜你喜欢
  • 2021-10-07
  • 2018-07-27
  • 1970-01-01
  • 2019-12-23
  • 1970-01-01
  • 2014-11-07
  • 2013-03-27
  • 1970-01-01
  • 2018-10-13
相关资源
最近更新 更多