singhuist
singhuist

Reputation: 302

How to plot and display a graph in Python

Pyplot

I wrote the code to plot and display a simple graph in Python:

import matplotlib.pyplot as plt
import numpy as np
from matplotlib import interactive
interactive(True)

x = np.arange(0,5,0.1)
y = np.sin(x)
plt.plot(x,y)
plt.show

And all I got is a blank screen.

And when I remove the "interactive" thing it shows no error but diplays nothing.

How can I display the graph?

(P.S: I use Python 2.7)

Upvotes: 1

Views: 27741

Answers (4)

Default picture
Default picture

Reputation: 728

You can also plot graphs with pyformulas. First pip install pyformulas, then

import pyformulas as pf
import numpy as np

x = np.linspace(-10,10,100)
y = x**2 + x*np.e**(np.cos(x)**2)
pf.plot(x, y)

Disclaimer: I'm the maintainer of pyformulas

enter image description here

Upvotes: 0

recurseuntilfor
recurseuntilfor

Reputation: 2411

Just a note for others for future reference the full code should also include plt.figure() with the interactive elements removed.

Here what I came up with.

import matplotlib.pyplot as plt
import numpy as np
plt.figure()
x = np.arange(0, 5, 0.1)
y = np.sin(x)
plt.plot(x, y)
plt.show()

But this may be a 3.5 problem I've not tried in 2.7

Upvotes: 0

singhuist
singhuist

Reputation: 302

There is a syntax error. Replace plt.show with plt.show()

Upvotes: 1

Lucas
Lucas

Reputation: 7341

Remove these lines, they are not for a simple graphic:

from matplotlib import interactive
interactive(True)

And you're missing the () in the plt.show()

plt.show()

Upvotes: 3

Related Questions