Unit 3
Unit 3
Installation
The PyLab Module is installed at the same time as the Matplotlib package.
However, if we wish to use this module in a Python program, we must first ensure
that the Matplotlib Module is installed on our system. If Matplotlib is not already
installed on the system, we may use the pip installer command in the command
prompt terminal shell to install Matplotlib Module and so get the PyLab Module
Basic Plotting
Plotting curves is done with the plot() function. It takes a pair of same-length
arrays (or sequences) −
Algorithm (Steps)
Plotting curves is done with the plot() function. It takes a pair of same-length
arrays (or sequences) −
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task −
Use the import keyword, to import all the functions(represented by *) from the numpy, pylab modules.
Use the numpy.linspace() function(returns number spaces evenly with respect to the interval) to
generate random points in x-axis
Get the y-axix values as the square of the x-axis values.
Plot the x,y values using the plot() function.
Display the plot using the show() function.
Example
from numpy import *
from pylab import *
x = linspace(-3, 3, 30)
y = x**2
plot(x, y)
show()
Advanced Plotting
We can utilize some variables in the plot() function, in addition to the x and y
variable arguments, to plot more interactive curves with the PyLab Module. To
print symbol lines instead of straight lines in curves, we must pass additional string
arguments to the plot() function.
Aside from that, we can print the lines in colors other than the default color plotted
in the output curve, and we must follow the same set of instructions to do so.
The color argument must be passed as an additional argument to the plot()
function in order for the line of curve displayed in the output to be printed in the
color of our choice.
numpy.linspace(start, stop, num = 50, endpoint = True, retstep = False, dtype = None)
Parameters
retstep − If True, then return (samples, step). Restep is set to False by default.
Example
from numpy import *
from pylab import *
x = linspace(-3, 3, 30)
y = x**2
plot(x, y, 'r.')
show()
The threading module exposes all the methods of the thread module and
provides some additional methods −
In addition to the methods, the threading module has the Thread class that
implements threading. The methods provided by the Thread class are as follows
−
To create and start a new thread in Python, you can use either the low-
level _thread module or the higher-level threading module. The threading
module is generally recommended due to its additional features and ease of
use. Below, you can see both approaches.
This method call returns immediately, and the new thread starts executing the
specified function with the given arguments. When the function returns, the
thread terminates.
Example
import _thread
import time
def print_name(name, *arg):
print(name, *arg)
name="SSSDIIT..."
_thread.start_new_thread(print_name, (name, 1))
_thread.start_new_thread(print_name, (name, 1, 2))
time.sleep(0.5)
Here are a few steps to start a new thread using the threading module −
Example
import threading
import time
def print_name(name, *args):
print(name, *args)
thread1 = threading.Thread(target=print_name, args=(name, 1))
thread2 = threading.Thread(target=print_name, args=(name, 1, 2))
thread1.start()
thread2.start()
thread1.join()
thread2.join()
print("Threads are finished...exiting")
Synchronizing Threads
The acquire(blocking) method of the new lock object is used to force threads to
run synchronously. The optional blocking parameter enables you to control
whether the thread waits to acquire the lock.
If blocking is set to 0, the thread returns immediately with a 0 value if the lock
cannot be acquired and with a 1 if the lock was acquired. If blocking is set to 1,
the thread blocks and wait for the lock to be released.
The release() method of the new lock object is used to release the lock when it
is no longer required.
The Queue module allows you to create a new queue object that can hold a
specific number of items. There are following methods to control the Queue −
get() − The get() removes and returns an item from the queue.
put() − The put adds item to a queue.
qsize() − The qsize() returns the number of items that are currently in the queue.
empty() − The empty( ) returns True if queue is empty; otherwise, False.
full() − the full() returns True if queue is full; otherwise, False.
A thread object goes through different stages during its life cycle. When a new
thread object is created, it must be started, which calls the run() method of
thread class. This method contains the logic of the process to be performed by
the new thread. The thread completes its task as the run() method is over, and
the newly created thread merges with the main thread.