Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display a graph in ipython notebook

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

like image 310
baxx Avatar asked Nov 22 '15 14:11

baxx


People also ask

How do you show graphs in Jupyter notebook?

Jupyter Notebook - Big Data Visualization Tool IPython kernel of Jupyter notebook is able to display plots of code in input cells. It works seamlessly with matplotlib library. The inline option with the %matplotlib magic function renders the plot out cell even if show() function of plot object is not called.

How do I display an image in IPython notebook?

To display the image, the Ipython. display() method necessitates the use of a function. In the notebook, you can also specify the width and height of the image.

Which of the following is used to display plots on a Jupyter notebook?

The %matplotlib magic command sets up your Jupyter Notebook for displaying plots with Matplotlib. The standard Matplotlib graphics backend is used by default, and your plots will be displayed in a separate window.


1 Answers

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)
like image 159
Leb Avatar answered Nov 01 '22 08:11

Leb