How to display a graph in ipython notebook

耗尽温柔 提交于 2021-02-04 17:51:07

问题


Trying to do some plotting in 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

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


回答1:


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

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)



回答2:


To get plots to show inline in the IPython notebook, you need to enable matplotlib's inline backend. You can do this by running

%matplotlib inline


来源:https://stackoverflow.com/questions/33855699/how-to-display-a-graph-in-ipython-notebook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!