1 \documentclass[12pt]{article} |
1 \documentclass[12pt]{article} |
2 %\title{Plotting Data} |
2 |
3 %\author{FOSSEE} |
3 |
|
4 \title{Plotting Points} |
|
5 \author{FOSSEE} |
|
6 \usepackage{listings} |
|
7 \lstset{language=Python, |
|
8 basicstyle=\ttfamily, |
|
9 commentstyle=\itshape\bfseries, |
|
10 showstringspaces=false, |
|
11 } |
|
12 \newcommand{\typ}[1]{\lstinline{#1}} |
|
13 \usepackage[english]{babel} |
|
14 \usepackage[latin1]{inputenc} |
|
15 \usepackage{times} |
|
16 \usepackage[T1]{fontenc} |
|
17 \usepackage{ae,aecompl} |
|
18 \usepackage{mathpazo,courier,euler} |
|
19 \usepackage[scaled=.95]{helvet} |
|
20 |
4 \begin{document} |
21 \begin{document} |
5 \date{} |
22 \date{} |
6 \vspace{-1in} |
23 \vspace{-1in} |
7 \begin{center} |
24 \begin{center} |
8 \LARGE{Plotting Data}\\ |
25 \LARGE{Plotting Points}\\ |
9 \large{FOSSEE} |
26 \large{FOSSEE} |
10 \end{center} |
27 \end{center} |
11 \section{Scripts} |
28 \section{Plotting Points with Lists} |
12 IPython History |
|
13 \begin{verbatim} |
|
14 In [1]: %hist |
|
15 In [2]: %hist -n |
|
16 \end{verbatim} |
|
17 |
29 |
18 Running a Script |
30 \begin{lstlisting} |
19 \begin{verbatim} |
31 In [1]: x = [0, 1, 2, 3] |
20 In [3]: %run script_name.py |
32 In [2]: y = [7, 11, 15, 19] |
21 \end{verbatim} |
33 In [3]: plot(x, y) |
|
34 In [4]: clf() |
|
35 In [5]: plot(x, y, 'o') # Plotting Circles |
|
36 #Dots - '.', #Dashed lines - '--' #Lines - '-' |
|
37 \end{lstlisting} |
22 |
38 |
23 \section{Plotting from Data files} |
39 \section{Lists} |
24 \begin{verbatim} |
40 |
25 In [1]: L = [] #Empty List |
41 Initializing |
26 In [2]: T = [] |
42 \begin{lstlisting} |
27 In [3]: for line in open('pendulum.txt'): # Opening & Reading files |
43 In [10]: mtlist = [] # Empty List |
28 ....: points = line.split() # Splitting a string |
44 In [11]: lst = [ 1, 2, 3, 4, 5] |
29 ....: L.append(float(points[0])) # Appending to a list |
45 \end{lstlisting} |
30 ....: T.append(float(points[1])) |
46 Slicing |
31 In [4]: TSq = [] |
47 \begin{lstlisting} |
32 In [5]: for t in T: #Iterating through lists |
48 In [12]: lst[1:3] # A slice. |
|
49 Out[12]: [2, 3] |
|
50 |
|
51 In [13]: lst[1:-1] |
|
52 Out[13]: [2, 3, 4] |
|
53 \end{lstlisting} |
|
54 Appending to lists |
|
55 \begin{lstlisting} |
|
56 In [14]: a = [ 6, 7, 8, 9] |
|
57 In [15]: b = lst + a |
|
58 In [16]: b |
|
59 Out[16]: [1, 2, 3, 4, 5, 6, 7, 8, 9] |
|
60 |
|
61 In [17]: lst.append(6) |
|
62 In [18]: lst |
|
63 Out[18]: [ 1, 2, 3, 4, 5, 6] |
|
64 \end{lstlisting} |
|
65 |
|
66 Iterating over a List |
|
67 \begin{lstlisting} |
|
68 In [19]: for each in b: # Iterating over the list, element-wise |
|
69 ....: print b # Print each element |
|
70 ....: |
|
71 \end{lstlisting} |
|
72 |
|
73 Splitting Strings |
|
74 \begin{lstlisting} |
|
75 In [20]: line = '1.2000e-01 7.4252e-01' |
|
76 In [21]: point = line.split() # Splits the string at the space |
|
77 Out[21]: ['1.2000e-01', '7.4252e-01'] |
|
78 \end{lstlisting} |
|
79 |
|
80 Plotting from Files |
|
81 \begin{lstlisting} |
|
82 In [22]: L = [] |
|
83 In [23]: T = [] |
|
84 |
|
85 #Open a file & operate on each line |
|
86 In [24]: for line in open('pendulum.txt'): |
|
87 .... point = line.split() |
|
88 .... L.append(float(point[0])) |
|
89 .... T.append(float(point[1])) |
|
90 In [25]: TSq = [] |
|
91 In [26]: for t in T: |
33 ....: TSq.append(t*t) |
92 ....: TSq.append(t*t) |
34 In [6]: plot(L, TSq, '.') # Plotting points |
93 ....: |
35 \end{verbatim} |
94 ....: |
|
95 In [27]: plot(L, TSq, '.') |
|
96 \end{lstlisting} |
|
97 |
|
98 |
36 \end{document} |
99 \end{document} |
37 |
100 |