如何在ipython笔记本中显示图形 [英] How to display a graph in ipython notebook

查看:127
本文介绍了如何在ipython笔记本中显示图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试在SymPy中进行一些绘图-

Trying to do some plotting in SymPy -

根据此视频,我写过:

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.

另一项测试:

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?

如果您对虚拟环境的内容感兴趣,那么可以参考以下内容: venv

If the virtual environment content is of any interest here's a tree of that : venv

我正在Linux Ubuntu上运行它.在上面的粘贴链接中可以看到运行它的虚拟环境

I'm running this on Linux Ubuntu. The virtual environment that it's running in can be seen in the above paste link

推荐答案

您需要使用魔术函数,更具体地说,是 matplotlib :

You need to use the 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 3.4(2015年11月3.4日)的可运行示例:

Runnable example using Python 3.4 Nov '15:

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笔记本中显示图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆