147
|
1 |
Hello and welcome to the tutorial on creating simple plots using
|
|
2 |
Python.This tutorial is presented by the Fossee group.
|
|
3 |
{{{ Show the Title Slide }}}
|
|
4 |
|
|
5 |
I hope you have IPython running on your computer.
|
|
6 |
|
|
7 |
In this tutorial we will look at plot command and also how to study
|
|
8 |
the plot using the UI.
|
|
9 |
|
|
10 |
{{{ Show Outline Slide }}}
|
|
11 |
|
|
12 |
Lets start ipython on your shell, type ::
|
|
13 |
|
|
14 |
$ipython -pylab
|
|
15 |
|
|
16 |
|
|
17 |
Pylab is a python library which provides plotting functionality.It
|
|
18 |
also provides many other important mathematical and scientific
|
|
19 |
functions. After running IPython -pylab in your shell if at the top of
|
|
20 |
the result of this command, you see something like ::
|
|
21 |
|
|
22 |
|
|
23 |
`ERROR: matplotlib could NOT be imported! Starting normal
|
|
24 |
IPython.`
|
|
25 |
|
|
26 |
|
|
27 |
{{{ Slide with Error written on it }}}
|
|
28 |
|
|
29 |
Then you have to install matplotlib and run this command again.
|
|
30 |
|
|
31 |
Now type in your ipython shell ::
|
|
32 |
|
|
33 |
In[]: linpace?
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
as the documentation says, it returns `num` evenly spaced samples,
|
|
38 |
calculated over the interval start and stop. To illustrate this, lets
|
|
39 |
do it form 1 to 100 and try 100 points. ::
|
|
40 |
|
|
41 |
In[]: linspace(1,100,100)
|
|
42 |
|
|
43 |
As you can see a sequence of numbers from 1 to 100 appears.
|
|
44 |
|
|
45 |
Now lets try 200 points between 0 and 1 you do this by typing ::
|
|
46 |
|
|
47 |
|
|
48 |
In[]: linspace(0,1,200)
|
|
49 |
|
|
50 |
0 for start , 1 for stop and 200 for no of points. In linspace
|
|
51 |
the start and stop points can be integers, decimals , or
|
|
52 |
constants. Let's try and get 100 points between -pi to pi. Type ::
|
|
53 |
|
|
54 |
In[]: p = linspace(-pi,pi,100)
|
|
55 |
|
|
56 |
|
|
57 |
'pi' here is constant defined by pylab. Save this to the variable, p
|
|
58 |
.
|
|
59 |
|
|
60 |
If you now ::
|
|
61 |
|
|
62 |
In[]: len(p)
|
|
63 |
|
|
64 |
You will get the no. of points. len function gives the no of elements
|
|
65 |
of a sequence.
|
|
66 |
|
|
67 |
|
|
68 |
Let's try and plot a cosine curve between -pi and pi using these
|
|
69 |
points. Simply type ::
|
|
70 |
|
|
71 |
|
|
72 |
In[]: plot(p,cos(points))
|
|
73 |
|
|
74 |
Here cos(points) gets the cosine value at every corresponding point to
|
|
75 |
p.
|
|
76 |
|
|
77 |
|
|
78 |
We can also save cos(points) to variable cosine and plot it using
|
|
79 |
plot.::
|
|
80 |
|
|
81 |
In[]: cosine=cos(points)
|
|
82 |
|
|
83 |
In[]: plot(p,cosine)
|
|
84 |
|
|
85 |
|
|
86 |
|
|
87 |
Now do ::
|
|
88 |
|
|
89 |
In[]: clf()
|
|
90 |
|
|
91 |
this will clear the plot.
|
|
92 |
|
|
93 |
This is done because any other plot we try to make shall come on the
|
|
94 |
same drawing area. As we do not wish to clutter the area with
|
|
95 |
overlaid plots , we just clear it with clf(). Now lets try a sine
|
|
96 |
plot. ::
|
|
97 |
|
|
98 |
|
|
99 |
In []: plot(p,sin(p))
|
|
100 |
|
|
101 |
|
|
102 |
|
|
103 |
|
|
104 |
The Window on which the plot appears can be used to study it better.
|
|
105 |
|
|
106 |
First of all moving the mouse around gives us the point where mouse
|
|
107 |
points at.
|
|
108 |
|
|
109 |
Also we have some buttons the right most among them is
|
|
110 |
for saving the file.
|
|
111 |
|
|
112 |
Just click on it specifying the name of the file. We will save the plot
|
|
113 |
by the name sin_curve in pdf format.
|
|
114 |
|
|
115 |
{{{ Action corelating with the words }}}
|
|
116 |
|
|
117 |
As you can see I can specify format of file.
|
|
118 |
Left to the save button is the slider button to specify the margins.
|
|
119 |
|
|
120 |
{{{ Action corelating with the words }}}
|
|
121 |
|
|
122 |
Left to this is zoom button to zoom into the plot. Just specify the
|
|
123 |
region to zoom into.
|
|
124 |
The button left to it can be used to move the axes of the plot.
|
|
125 |
|
|
126 |
{{{ Action corelating with the words }}}
|
|
127 |
|
|
128 |
The next two buttons with a left and right arrow icons change the state of the
|
|
129 |
plot and take it to the previous state it was in. It more or less acts like a
|
|
130 |
back and forward button in the browser.
|
|
131 |
|
|
132 |
{{{ Action corelating with the words }}}
|
|
133 |
|
|
134 |
The last one is 'home' referring to the initial plot.
|
|
135 |
|
|
136 |
{{{ Action corelating with the words}}}
|
|
137 |
|
|
138 |
|
|
139 |
|
|
140 |
{{{ Summary Slide }}}
|
|
141 |
|
|
142 |
|
|
143 |
In this tutorial we have looked at
|
|
144 |
|
|
145 |
1. Starting Ipython with pylab
|
|
146 |
|
|
147 |
2. Using linspace function to create `num` equaly spaced points in a region.
|
|
148 |
|
|
149 |
3. Finding length of sequnces using len.
|
|
150 |
|
|
151 |
4. Plotting mathematical functions using plot.
|
|
152 |
|
|
153 |
4. Clearing drawing area using clf
|
|
154 |
|
|
155 |
5. Using the UI of plot for studying it better . Using functionalities like save , zoom , moving the plots on x and y axis
|
|
156 |
|
|
157 |
etc ..
|
|
158 |
|
|
159 |
|
|
160 |
|
|
161 |
{{{ Show the "sponsored by FOSSEE" slide }}}
|
|
162 |
|
|
163 |
|
|
164 |
|
|
165 |
This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
|
|
166 |
|
|
167 |
|
|
168 |
|
|
169 |
Hope you have enjoyed and found it useful.
|
|
170 |
|
|
171 |
Thankyou
|
|
172 |
|
|
173 |
|
|
174 |
|
|
175 |
Author : Amit Sethi
|
|
176 |
Internal Reviewer :
|
|
177 |
Internal Reviewer 2 :
|