如何在 ipython 笔记本中显示图表

How to display a graph in ipython notebook

尝试在 SymPy 中进行绘图 -

根据 this video 我写了 :

from sympy.plotting import plot, plot_parametric

e = sin(2*sin(x**3))
plot(e, (x, 0, 5));

但是在评估那个单元格之后我没有得到任何输出?没有错误或 什么,就是什么都不显示。

另一个测试:

from sympy import *
from sympy.plotting import plot, plot_parametric
import math
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt


expr = x**2 + sqrt(3)*x - Rational(1, 3)
lf = lambdify(x, expr)

fig = plt.figure()
axes = fig.add_subplot(111)

x_vals = np.linspace(-5., 5.)
y_vals = lf(x_vals)

axes.grid()
axes.plot(x_vals, y_vals)

plt.show();

所以我不确定我在这里做错了什么,虽然我没有收到任何错误?

如果对虚拟环境内容感兴趣,请看这里的树: venv

我在 Linux Ubuntu 上 运行。它所在的虚拟环境 运行 可以在上面的粘贴 link

中看到

您需要使用 magic functions, more specifically the ones for matplotlib:

%matplotlib qt # displays a pop-up of the plot
%matplotlib inline # keeps it within the notebook

使用 Python 2015 年 11 月 3 日的可运行示例:

from sympy import *
from sympy.plotting import plot, plot_parametric
import math
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

%matplotlib inline

expr = x**2 + sqrt(3)*x - Rational(1, 3)
lf = lambdify(x, expr)

fig = plt.figure()
axes = fig.add_subplot(111)

x_vals = np.linspace(-5., 5.)
y_vals = lf(x_vals)

axes.grid()
axes.plot(x_vals, y_vals)

要让绘图在 IPython 笔记本中内联显示,您需要启用 matplotlib 的内联后端。您可以通过 运行

%matplotlib inline