321
|
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 |
|
|
116 |
|
|
117 |
{{{ Action corelating with the words }}}
|
|
118 |
|
|
119 |
As you can see I can specify format of file from the dropdown.
|
|
120 |
|
|
121 |
Formats like png ,eps ,pdf, ps are available.
|
|
122 |
|
|
123 |
Left to the save button is the slider button to specify the margins.
|
|
124 |
|
|
125 |
{{{ Action corelating with the words }}}
|
|
126 |
|
|
127 |
Left to this is zoom button to zoom into the plot. Just specify the
|
|
128 |
region to zoom into.
|
|
129 |
The button left to it can be used to move the axes of the plot.
|
|
130 |
|
|
131 |
{{{ Action corelating with the words }}}
|
|
132 |
|
|
133 |
The next two buttons with a left and right arrow icons change the state of the
|
|
134 |
plot and take it to the previous state it was in. It more or less acts like a
|
|
135 |
back and forward button in the browser.
|
|
136 |
|
|
137 |
{{{ Action corelating with the words }}}
|
|
138 |
|
|
139 |
The last one is 'home' referring to the initial plot.
|
|
140 |
|
|
141 |
{{{ Action corelating with the words}}}
|
|
142 |
|
|
143 |
|
|
144 |
|
|
145 |
{{{ Summary Slide }}}
|
|
146 |
|
|
147 |
|
|
148 |
In this tutorial we have looked at
|
|
149 |
|
|
150 |
1. Starting Ipython with pylab
|
|
151 |
|
|
152 |
2. Using linspace function to create `num` equaly spaced points in a region.
|
|
153 |
|
|
154 |
3. Finding length of sequnces using len.
|
|
155 |
|
|
156 |
4. Plotting mathematical functions using plot.
|
|
157 |
|
|
158 |
4. Clearing drawing area using clf
|
|
159 |
|
|
160 |
5. Using the UI of plot for studying it better . Using functionalities like save , zoom , moving the plots on x and y axis
|
|
161 |
|
|
162 |
etc ..
|
|
163 |
|
|
164 |
|
|
165 |
|
|
166 |
{{{ Show the "sponsored by FOSSEE" slide }}}
|
|
167 |
|
|
168 |
|
|
169 |
|
|
170 |
This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
|
|
171 |
|
|
172 |
|
|
173 |
|
|
174 |
Hope you have enjoyed and found it useful.
|
|
175 |
|
|
176 |
Thankyou
|
|
177 |
|
|
178 |
|
|
179 |
|
|
180 |
Author : Amit Sethi
|
|
181 |
Internal Reviewer :
|
|
182 |
Internal Reviewer 2 :
|