简体   繁体   English

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

[英]How to display a graph in ipython notebook

Trying to do some plotting in SymPy - 试着在SymPy做一些情节 -

As per this video I have written : 根据这段视频,我写道:

from sympy.plotting import plot, plot_parametric

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

But after evaling that cell I don't get any output? 但在评估该单元后,我没有得到任何输出? There isn't an error or anything, it just doesn't display anything. 没有错误或任何东西,它只是没有显示任何东西。

Another test : 另一个测试:

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();

So Im not sure what I'm doing wrong here, I'm not getting any errors though? 所以我不确定我在这里做错了什么,但我没有收到任何错误?

If the virtual environment content is of any interest here's a tree of that : venv 如果虚拟环境内容是任何感兴趣的,那么这是一棵树: venv

I'm running this on Linux Ubuntu. 我在Linux Ubuntu上运行它。 The virtual environment that it's running in can be seen in the above paste link 可以在上面的粘贴链接中看到它运行的虚拟环境

You need to use the magic functions , more specifically the ones for matplotlib : 你需要使用魔术函数 ,更具体地说是matplotlib的 函数

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

Runnable example using Python 3.4 Nov '15: 使用Python的Runnable示例3.4 2015年11月:

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)

To get plots to show inline in the IPython notebook, you need to enable matplotlib's inline backend. 要获得在IPython笔记本中显示内联的图,您需要启用matplotlib的内联后端。 You can do this by running 你可以通过运行来做到这一点

%matplotlib inline

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

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