python - how can i plot simple functions like sine x with IPython? - Ask Ubuntu


my teacher plot functions jupyter or ipython. have installed jupyter , have home screen.

but how can plot function?

i have tried: plot sin(x)

but message:

  file "<ipython-input-4-7979a129f00f>", line 1     plot sin(x)            ^ syntaxerror: invalid syntax 

screenshot

from terminal type:

sudo apt-get install ipython-notebook python-numpy python-matplotlib   

the latest version of ipython notebook known jupyter notebook. can optionally install jupyter notebook instead of ipython notebook. in ubuntu 14.04/16.04/16.10 follow instructions in this answer install jupyter notebook upgrading ipython notebook jupyter notebook. starting ubuntu 17.04, can install jupyter notebook default ubuntu repositories (sudo apt install jupyter-notebook jupyter-core python-ipykernel). python-ipykernel necessary running python 2.x programs in jupyter notebook, otherwise supports python 3.x. if jupyter notebook installed, command start jupyter jupyter notebook , first line of code enable plotting in current notebook %matplotlib inline.

follow these steps generate sine wave plot in example matplotlib.org.

  1. open ipython-notebook web browser interface. terminal run:

    ipython notebook --pylab   

    the --pylab option removed when transitioned ipython jupyter notebook. instead use jupyter notebook start jupyter.

  2. make new notebook. ipython notebook interface click new notebook button. new notebook tab open in default web browser. new notebook tab select file -> rename, rename new notebook descriptive name sine_wave , click ok button.

  3. copy example python code plotting sine wave listed below , paste sine_wave notebook right of says in [1]: using keyboard combination ctrl+v. paste whole code block together, not 1 line @ time.

    import matplotlib.pyplot plt   import numpy np    t = np.arange(0.0, 2.0, 0.01)   s = np.sin(2*np.pi*t)   plt.plot(t, s)    plt.xlabel('time (s)')   plt.ylabel('voltage (mv)')   plt.title('voltage (mv) vs. time (sec)')    plt.grid(true)   plt.savefig("plot-voltage-vs.-time.png")   plt.show()   

    plt.savefig("plot-voltage-vs.-time.png") saves image of plot without window chrome in home directory.

  4. click black triangular-shaped run button ( ) on menu bar run code block.

  5. your output plot appear in small popup window looks popup window in below screenshot.

    img:

  6. repeat steps 3. , 4. run new code block (in [2]:). try pasting following simple python code after in [2]: , running it.

    import matplotlib.pyplot plt   import numpy np   x = np.arange(0.0, 2.0, 0.01) y = np.sin(2*np.pi*x) plt.plot(x, y) plt.show()   

the error message importerror: no module named 'matplotlib' caused using python 3 jupyter installed python 2.x. possible use python 3 in jupyter notebook python 2 adding kernel python 2. if you're running jupyter on python 3, can set python 2 kernel this:

python2 -m pip install ipykernel python2 -m ipykernel install --user 

Comments

Popular posts from this blog

download - Firefox cannot save files (most of the time), how to solve? - Super User

windows - "-2146893807 NTE_NOT_FOUND" when repair certificate store - Super User

sql server - "Configuration file does not exist", Event ID 274 - Super User