Welcome to Canopy's interactive data-analysis environment!

Type '?' for more information.

Python 2.7.13 |Enthought, Inc. (x86_64)| (default, Mar 2 2017, 16:05:12) [MSC v.1500 64 bit (AMD64)]

Type "copyright", "credits" or "license" for more information.


IPython 5.3.0 -- An enhanced Interactive Python.

? -> Introduction and overview of IPython's features.

%quickref -> Quick reference.

help -> Python's own help system.

object? -> Details about 'object', use 'object??' for extra details.


# Plots:

In [1]: from matplotlib import pyplot as plt


In [2]: X = np.linspace(-np.pi, np.pi, 16, endpoint=True)


In [3]: X

Out[3]:

array([-3.14159265, -2.72271363, -2.30383461, -1.88495559, -1.46607657,

-1.04719755, -0.62831853, -0.20943951, 0.20943951, 0.62831853,

1.04719755, 1.46607657, 1.88495559, 2.30383461, 2.72271363,

3.14159265])


In [4]: C = cos(X)


In [5]: S = sin(X)


In [6]: plt.plot(X)

Out[6]: [<matplotlib.lines.Line2D at 0xa798198>]


In [7]: plt.plot(X, S)

Out[7]: [<matplotlib.lines.Line2D at 0xa8d5588>]


In [8]: plt.cla()


In [9]: plt.plot(X, S)

Out[9]: [<matplotlib.lines.Line2D at 0xab204a8>]


In [10]: plt.plot(X, C)

Out[10]: [<matplotlib.lines.Line2D at 0xac35f98>]


In [11]: X = np.linspace(-np.pi, np.pi, 256, endpoint=True)


In [12]: S=sin(X)


In [13]: C=cos(X)


In [14]: plt.cla()


In [15]: plt.plot(X, C)

Out[15]: [<matplotlib.lines.Line2D at 0xac425c0>]


In [16]: plt.plot(X, S)

Out[16]: [<matplotlib.lines.Line2D at 0xac5deb8>]


In [17]: X = np.linspace(-np.pi, np.pi, 16, endpoint=True)


In [18]: C=cos(X)


In [19]: S=sin(X)


In [20]: plt.cla()


In [22]: plt.plot(X, C, color="red", linewidth=1.0, linestyle="-.", marker='+')

Out[22]: [<matplotlib.lines.Line2D at 0xaea4e80>]


In [23]: plt.plot(X, S, color="green", linewidth=2.0, linestyle="-", marker='2')

Out[23]: [<matplotlib.lines.Line2D at 0xaebf668>]


In [24]: plt.cla()


In [25]: plt.plot(X, C-S, '-.+r', X, C+S, '--.b')

Out[25]:

[<matplotlib.lines.Line2D at 0xaeb9d30>,

<matplotlib.lines.Line2D at 0xaeb9dd8>]


In [26]: plt.cla()


In [27]: plt.plot(X, C-S, '-.+r', X, C+S, '--.b')

Out[27]:

[<matplotlib.lines.Line2D at 0xaf0b240>,

<matplotlib.lines.Line2D at 0xaf0b208>]


In [28]: plt.xlim(-1, 1)

Out[28]: (-1, 1)


In [29]: plt.ylim(-1.0, 1.0)

Out[29]: (-1.0, 1.0)


In [31]: plt.cla()


In [32]: plt.plot(X, C-S, '-.+r', X, C+S, '--.b')

Out[32]:

[<matplotlib.lines.Line2D at 0xb532eb8>,

<matplotlib.lines.Line2D at 0xb532f60>]


In [33]: plt.ylim(-10.0, 10.0)

Out[33]: (-10.0, 10.0)


In [34]: plt.xlabel('x-label')

Out[34]: <matplotlib.text.Text at 0xac9cf28>


In [35]: plt.ylabel('x-label')

Out[35]: <matplotlib.text.Text at 0xaf73dd8>


In [36]: plt.title('some title')

Out[36]: <matplotlib.text.Text at 0xb460630>


In [37]: import numpy as np

    ...: import matplotlib.pyplot as plt

    ...:

    ...: def f(t):

    ...: return np.exp(-t) * np.cos(2*np.pi*t)

    ...:

    ...: t1 = np.arange(0.0, 5.0, 0.1)

    ...: t2 = np.arange(0.0, 5.0, 0.02)

    ...:

    ...: plt.figure(1)

    ...: plt.subplot(211)

    ...: plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k')

    ...:

    ...: plt.subplot(212)

    ...: plt.plot(t2, np.cos(2*np.pi*t2), 'r--')

    ...: plt.show()

    ...:


In [38]: import matplotlib.pyplot as plt

    ...: plt.figure(1) # the first figure

    ...: plt.subplot(211) # the first subplot in the first figure

    ...: plt.plot([1, 2, 3])

    ...: plt.subplot(212) # the second subplot in the first figure

    ...: plt.plot([4, 5, 6])

    ...:

    ...:

    ...: plt.figure(2) # a second figure

    ...: plt.plot([4, 5, 6]) # creates a subplot(111) by default

    ...:

    ...: plt.figure(1) # figure 1 current; subplot(212) still current

    ...: plt.subplot(211) # make subplot(211) in figure1 current

    ...: plt.title('Easy as 1, 2, 3') # subplot 211 title

    ...:

Out[38]: <matplotlib.text.Text at 0xb5cce48>


In [39]: from mpl_toolkits.mplot3d import Axes3D

    ...:

    ...: fig = plt.figure()

    ...: ax = Axes3D(fig)

    ...: X = np.arange(-4, 4, 0.25)

    ...: Y = np.arange(-4, 4, 0.25)

    ...: X, Y = np.meshgrid(X, Y)

    ...: R = np.sqrt(X**2 + Y**2)

    ...: Z = np.sin(R)

    ...:

    ...: ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap='hot')

    ...:

Out[39]: <mpl_toolkits.mplot3d.art3d.Poly3DCollection at 0x146bf0b8>


# Measuring performance:

In [40]: f = lambda x: x**2


In [41]: a = 0


In [42]: b = 10


In [44]: %cd "C:\Users\Oleksandr\Python"

C:\Users\Oleksandr\Python


In [45]: import myalgs as ma


In [46]: ma.trapez(f, a, b, 6, True)

Out[46]: 337.96296296296299


In [47]: plt.cla()


In [48]: ma.trapez(f, a, b, 6, True)

Out[48]: 337.96296296296299


In [49]: ma.trapez(f, a, b, 3, True)

Out[49]: 351.8518518518519


In [50]: %run "C:\Users\Oleksandr\Python\run_trapez.py"


In [51]: import time


In [52]: time.clock()

Out[52]: 4.105287487124792e-07


In [53]: t1 = time.clock()


In [54]: t2 = time.clock()


In [55]: t2-t1

Out[55]: 3.326696725581641


In [56]: %run "C:\Users\Oleksandr\Python\run_measure.py"


# Classes:

In [57]: import mycircle as mc



In [59]: c1 = mc.circle(10, 14, 3)


In [60]: c1.cx

Out[60]: 10


In [61]: c1.cy

Out[61]: 14


In [62]: c1.r

Out[62]: 3


In [63]: c1 = mc.circle(0, 44, 31)


In [64]: c2 = mc.circle(0, 44, 31)


In [65]: c2.cx

Out[65]: 0


In [66]: c2.cy

Out[66]: 44



In [68]: mc.area()


AttributeErrorTraceback (most recent call last)
<ipython-input-68-5a134a85e20d> in <module>()
----> 1 mc.area()

AttributeError: 'module' object has no attribute 'area'


In [69]: c1.area()

Out[69]: 3019.0705400997913


In [71]: c3 = mc.circle(1, 1, 2)


In [72]: c3.area()

Out[72]: 12.566370614359172


In [73]: c0 = mc.circle(0, 0, 3)


In [74]: c0.inside(10, 10)

Out[74]: False


In [75]: c0.inside(1, 1)

Out[75]: True


In [76]: c2.inside(1,1)

Out[76]: False


In [77]: %run "C:\Users\Oleksandr\Python\move_cars.py"

Current time 0.0 seconds

Red is at 5.00. Green is at 97.50.

Current time 0.5 seconds

Red is at 10.00. Green is at 95.00.

Current time 1.0 seconds

Red is at 15.00. Green is at 92.50.

Current time 1.5 seconds

Red is at 20.00. Green is at 90.00.

Current time 2.0 seconds

Red is at 25.00. Green is at 87.50.

Current time 2.5 seconds

Red is at 30.00. Green is at 85.00.

Current time 3.0 seconds

Red is at 35.00. Green is at 82.50.

Current time 3.5 seconds

Red is at 40.00. Green is at 80.00.

Current time 4.0 seconds

Red is at 45.00. Green is at 77.50.

Current time 4.5 seconds

Red is at 50.00. Green is at 75.00.

Current time 5.0 seconds

Red is at 55.00. Green is at 72.50.

Current time 5.5 seconds

Red is at 60.00. Green is at 70.00.

Current time 6.0 seconds

Red is at 65.00. Green is at 67.50.

Crash!

Done.