Simple Plotting Example


First we import the modules matplotlib for plotting and numpy for numerical array manipulation:


In [30]:
from matplotlib import pyplot as plt
import numpy as np

Next we set the variable xs to be an array of 100 equally spaced points in the interval [-1,1]:


In [31]:
xs = np.linspace(-2, 2, num=100)
print(xs)


[-2.         -1.95959596 -1.91919192 -1.87878788 -1.83838384 -1.7979798
 -1.75757576 -1.71717172 -1.67676768 -1.63636364 -1.5959596  -1.55555556
 -1.51515152 -1.47474747 -1.43434343 -1.39393939 -1.35353535 -1.31313131
 -1.27272727 -1.23232323 -1.19191919 -1.15151515 -1.11111111 -1.07070707
 -1.03030303 -0.98989899 -0.94949495 -0.90909091 -0.86868687 -0.82828283
 -0.78787879 -0.74747475 -0.70707071 -0.66666667 -0.62626263 -0.58585859
 -0.54545455 -0.50505051 -0.46464646 -0.42424242 -0.38383838 -0.34343434
 -0.3030303  -0.26262626 -0.22222222 -0.18181818 -0.14141414 -0.1010101
 -0.06060606 -0.02020202  0.02020202  0.06060606  0.1010101   0.14141414
  0.18181818  0.22222222  0.26262626  0.3030303   0.34343434  0.38383838
  0.42424242  0.46464646  0.50505051  0.54545455  0.58585859  0.62626263
  0.66666667  0.70707071  0.74747475  0.78787879  0.82828283  0.86868687
  0.90909091  0.94949495  0.98989899  1.03030303  1.07070707  1.11111111
  1.15151515  1.19191919  1.23232323  1.27272727  1.31313131  1.35353535
  1.39393939  1.43434343  1.47474747  1.51515152  1.55555556  1.5959596
  1.63636364  1.67676768  1.71717172  1.75757576  1.7979798   1.83838384
  1.87878788  1.91919192  1.95959596  2.        ]

Now we set the array ys to be the elementwise square of xs:


In [32]:
ys = xs*xs
print(ys)


[  4.00000000e+00   3.84001632e+00   3.68329762e+00   3.52984389e+00
   3.37965514e+00   3.23273135e+00   3.08907254e+00   2.94867871e+00
   2.81154984e+00   2.67768595e+00   2.54708703e+00   2.41975309e+00
   2.29568411e+00   2.17488011e+00   2.05734109e+00   1.94306703e+00
   1.83205795e+00   1.72431385e+00   1.61983471e+00   1.51862055e+00
   1.42067136e+00   1.32598714e+00   1.23456790e+00   1.14641363e+00
   1.06152433e+00   9.79900010e-01   9.01540659e-01   8.26446281e-01
   7.54616876e-01   6.86052444e-01   6.20752984e-01   5.58718498e-01
   4.99948985e-01   4.44444444e-01   3.92204877e-01   3.43230283e-01
   2.97520661e-01   2.55076013e-01   2.15896337e-01   1.79981635e-01
   1.47331905e-01   1.17947148e-01   9.18273646e-02   6.89725538e-02
   4.93827160e-02   3.30578512e-02   1.99979594e-02   1.02030405e-02
   3.67309458e-03   4.08121620e-04   4.08121620e-04   3.67309458e-03
   1.02030405e-02   1.99979594e-02   3.30578512e-02   4.93827160e-02
   6.89725538e-02   9.18273646e-02   1.17947148e-01   1.47331905e-01
   1.79981635e-01   2.15896337e-01   2.55076013e-01   2.97520661e-01
   3.43230283e-01   3.92204877e-01   4.44444444e-01   4.99948985e-01
   5.58718498e-01   6.20752984e-01   6.86052444e-01   7.54616876e-01
   8.26446281e-01   9.01540659e-01   9.79900010e-01   1.06152433e+00
   1.14641363e+00   1.23456790e+00   1.32598714e+00   1.42067136e+00
   1.51862055e+00   1.61983471e+00   1.72431385e+00   1.83205795e+00
   1.94306703e+00   2.05734109e+00   2.17488011e+00   2.29568411e+00
   2.41975309e+00   2.54708703e+00   2.67768595e+00   2.81154984e+00
   2.94867871e+00   3.08907254e+00   3.23273135e+00   3.37965514e+00
   3.52984389e+00   3.68329762e+00   3.84001632e+00   4.00000000e+00]

Now we plot the result using matplotlib:


In [33]:
plt.plot(xs, ys)
plt.xlabel('x')
plt.ylabel('y')
plt.title('plot of y = x**2')
plt.show()


For more information see the numpy webpage and the matplotlib webpage


In [ ]: