0% found this document useful (0 votes)
15 views34 pages

Python 3rd Unit by RJ

Unit 3 covers plotting using the PyLab module in Python, which is associated with Matplotlib and provides tools for data visualization. Key topics include basic plots, line styles, scatter plots, histograms, bar plots, pie charts, and dynamic programming concepts like memoization and the Fibonacci sequence. The document emphasizes the importance of visualizing data and efficient problem-solving techniques in programming.

Uploaded by

rashmitap
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views34 pages

Python 3rd Unit by RJ

Unit 3 covers plotting using the PyLab module in Python, which is associated with Matplotlib and provides tools for data visualization. Key topics include basic plots, line styles, scatter plots, histograms, bar plots, pie charts, and dynamic programming concepts like memoization and the Fibonacci sequence. The document emphasizes the importance of visualizing data and efficient problem-solving techniques in programming.

Uploaded by

rashmitap
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 34

UNIT-3

PLOTTING USING PYLAB


UNIT-3 TOPICS

 Plotting using PyLab,


 Plotting mortgages and extended

examples,
 Fibonacci sequence revisited, Dynamic

 programming and the 0/1 Knapsack

algorithm,
 Dynamic programming and divide and

conquer
PYLAB MODULE IN PYTHON
 PyLab is a Python package that provides us a
namespace in Python programming.

 PyLab Module is an associated module with the


Matplotlib Module of Python, and it gets installed
alongside when we are installing Matplotlib
Module in our system.

 Matplotlib was originally written by John D. Hunter.

 Matplotlib Module provides functions that help us to


create visualizations of data, whereas the Numpy
Module provides efficient numerical vector
calculation.

 PyLab Module gets installed alongside the


installation of the Matplotlib package.
PYLAB INSTALLATION
 If Matplotlib is not present in the system, then we
can use the following pip installer command in the
command prompt terminal shell to install Matplotlib
Module to get the PyLab Module with it:
 pipinstall matplotlib
 matplotlib.__version__

 PyLab Module also uses the mathematical and


vector operation functions from the Numpy Module.
 pipinstall numpy
 numpy.__version__
BASIC PLOTS
 The plot() function is used to draw points (markers) in
a diagram.
 By default, the plot() function draws a line from point

to point.
 The function takes parameters for specifying points in

the diagram.
 import matplotlib.pyplot as plt

 x = [1, 2, 3, 4, 5, 6] # X-axis points

 y = [1, 5, 3, 5, 7, 8] # Y-axis points

 plt.plot(x,y) # Plot the chart


 Parameter 1 is an array containing the points on the x-
axis.
 Parameter 2 is an array containing the points on the y-
axis.
 plt.show() # Plot the chart
LINE PLOT

import matplotlib.pyplot as plt

year = [1920, 1930, 1940, 1950, 1960, 1970, 1980, 1990, 2000, 2010]
unemployment_rate = [9.8, 12, 8, 7.2, 6.9, 7, 6.5, 6.2, 5.5, 6.3]

plt.plot(year, unemployment_rate)
plt.title('unemployment rate vs year')
plt.xlabel('year')
plt.ylabel('unemployment rate')
plt.show()
STYLE THE LINE

import matplotlib.pyplot as plt

year = [1920, 1930, 1940, 1950, 1960, 1970, 1980, 1990, 2000, 2010]
unemployment_rate = [9.8, 12, 8, 7.2, 6.9, 7, 6.5, 6.2, 5.5, 6.3]

plt.plot(year, unemployment_rate, color='red', marker='o')


plt.title('unemployment rate vs year', fontsize=14)
plt.xlabel('year', fontsize=14)
plt.ylabel('unemployment rate', fontsize=14)
plt.grid(True)
plt.show()
LINE STYLE & COLOR CODE
HEXADECIMAL COLORS
 Hexadecimal color values are also supported in all
browsers.

 A hexadecimal color is specified with: #RRGGBB.

 RR (red), GG (green) and BB (blue) are


hexadecimal integers between 00 and FF
specifying the intensity of the color.

 For example, #0000FF is displayed as blue,


because the blue component is set to its highest
value (FF) and the others are set to 00.
SCATTER PLOTS
 Alternatively, you may want to plot quantities which
have an x and y position.
 For example, plotting the location of stars or

galaxies in a field.

import numpy as np
import pylab as pl
# Make an array of x values
x = [1, 2, 3, 4, 5]
# Make an array of y values for each x value
y = [1, 4, 9, 16, 25]
# use pylab to plot x and y as red circles
pl.plot(x, y, ’ro’)
# show the plot on the screen
pl.show()
PLOT AND AXIS LIMITS
 You can change the x and y ranges displayed on your plot by:
 pl.xlim(x_low, x_high)
 pl.ylim(y_low, y_high)

import numpy as np
import pylab as pl
# Make an array of x values
x = [1, 2, 3, 4, 5]
# Make an array of y values for each x value
y = [1, 4, 9, 16, 25]
# use pylab to plot x and y
pl.plot(x, y)
# give plot a title
pl.title(’Plot of y vs. x’)
# make axis labels
pl.xlabel(’x axis’)
pl.ylabel(’y axis’)
# set axis limits
pl.xlim(0.0, 7.0)
pl.ylim(0.0, 30.)
# show the plot on the screen
pl.show()
FIGURE LEGENDS
 It’s very useful to add legends to plots to differentiate
between the different lines or quantities being plotted.
 In python you can make a legend as follows:

 pl.legend((plot1, plot2), (’label1, label2’), ’best’,

numpoints=1)
 The first parameter is a list of the plots you want labelled.
 The second parameter is the list of labels.
 The third parameter is where you would like matplotlib to
place your legend.
 Other optiions are:
 ‘upper right’, ‘upper left’, ‘center’, ‘lower left’,
‘lower right’.
 ‘best’ means that matplotlib will try to choose the position
on your plot where the legend will interfere least with what
is plotted (i.e. avoid overlaps etc.).
import numpy as np
import pylab as pl
# Make x, y arrays for each graph
x1 = [1, 2, 3, 4, 5]
y1 = [1, 4, 9, 16, 25]
x2 = [1, 2, 4, 6, 8]
y2 = [2, 4, 8, 12, 16]
# use pylab to plot x and y : Give your plots names
plot1 = pl.plot(x1, y1, ’r’)
plot2 = pl.plot(x2, y2, ’go’)
# give plot a title
pl.title(’Plot of y vs. x’)
# make axis labels
pl.xlabel(’x axis’)
pl.ylabel(’y axis’)
# set axis limits
pl.xlim(0.0, 9.0)
pl.ylim(0.0, 30.)
# make legend
pl.legend(["plot1", "plot2"],loc="upper
left",scatterpoints=1,shadow=True)
# show the plot on the screen
pl.show()
HISTOGRAMS
 A histogram is a graph showing frequency distributions.
 It is a graph showing the number of observations within

each given interval.

from matplotlib import pyplot as plt


import numpy as np
a=
np.array([2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,
71,73,79,83,89,97])
bins = [0,20,40,60,80,100] #bins=5
plt.hist(a, bins, facecolor='r', alpha=0.7, edgecolor='k',
linewidth=1)
plt.title("Primes between 1 and 100")
plt.xlabel("Primes")
plt.ylabel("Frequency")
plt.show()
BAR PLOT IN MATPLOTLIB
 A bar plot or bar chart is a graph that represents
the category of data with rectangular bars
with lengths and heights that is relative to
the values which they represent.

 The bar plots can be plotted horizontally or


vertically. A bar chart describes the comparisons
between the discrete categories.

 One of the axis of the plot represents the specific


categories being compared, while the other axis
represents the measured values corresponding to
those categories.
CREATING A BAR PLOT
 The matplotlib API in Python provides the bar()
function which can be used in MATLAB style use or
as an object-oriented API.

 The syntax of the bar() function to be used with the


axes is as follows:-
 plt.bar(x, height, width, bottom, align)
PIE CHARTS
 A Pie Chart is a circular statistical plot that can
display only one series of data.
 The area of the chart is the total percentage of the

given data.
 The area of slices of the pie represents the

percentage of the parts of the data.


 The slices of pie are called wedges.

 The area of the wedge is determined by the length

of the arc of the wedge.


 The area of a wedge represents the relative

percentage of that part with respect to whole data.


 Pie charts are commonly used in business

presentations like sales, operations, survey results,


resources, etc as they provide a quick summary.
CREATING PIE CHART
 Matplotlib API has pie() function in its pyplot module
which create a pie chart representing the data in an
array.
 Syntax: matplotlib.pyplot.pie(data, explode=None,

labels=None, colors=None, autopct=None,


shadow=False)
Parameters:
 data represents the array of data values to be plotted, the
fractional area of each slice is represented
by data/sum(data). If sum(data)<1, then the data values
returns the fractional area directly, thus resulting pie will
have empty wedge of size 1-sum(data).
 labels is a list of sequence of strings which sets the label of
each wedge.
 color attribute is used to provide color to the wedges.
 autopct is a string used to label the wedge with their
numerical value.
 shadow is used to create shadow of wedge.
SUBPLOT
 With the subplot() function you can draw multiple plots
in one figure.
import matplotlib.pyplot as
plt
import numpy as np

#plot 1:
x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])

plt.subplot(1, 2, 1)
plt.plot(x,y)

#plot 2:
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])

plt.subplot(1, 2, 2)
plt.plot(x,y)

plt.show()
DYNAMIC PROGRAMMING
 Dynamic programming was invented by Richard
Bellman in the early 1950s.
 Dynamic programming is a method for efficiently

solving problems that show the characteristics of


overlapping sub problems and optimal substructure.
 A problem has optimal substructure if a globally

optimal solution can be found by combining


optimal solutions to local subproblems. We’ve already
looked at a number of such problems. Merge sort, for
example, exploits the fact that a list can be sorted by
first sorting sublists and then merging the solutions.
 A problem has overlapping sub problems if an

optimal solution involves solving the same


problem multiple times. Merge sort does not exhibit
this property. Even though we are performing a merge
many times, we are merging different lists each time.
 Dynamic programming (also known as
dynamic optimization) is a method for
solving a complex problem by breaking it
down into a collection of simpler
subproblems, solving each of those
subproblems just once, and storing their
solutions.
 for ex. I want to do some math with

 A + (B * C)

 for this formula, I divide my problem into

two parts rather than solving it as a whole.


First part will be (B * C) and than the result
will be added with A i.e. A + Result of (B *
C) can be call a dynamic programming.
 1.) Top-Down : Start solving the given problem by
breaking it down. If you see that the problem has
been solved already, then just return the saved
answer. If it has not been solved, solve it and save
the answer. This is usually easy to think of and very
intuitive. This is referred to as Memoization.

 2.) Bottom-Up : Analyze the problem and see the


order in which the sub-problems are solved and
start solving from the trivial subproblem, up
towards the given problem. In this process, it is
guaranteed that the subproblems are solved before
solving the problem. This is referred to as Dynamic
Programming.
RECURSIVE IMPLEMENTATION OF
FIBONACCI FUNCTION
 While this implementation of the

recurrence is obviously correct,


it is terribly inefficient. Try, for
example, running fib(120), but
don’t wait for it to complete.
 The complexity of the
implementation is a bit hard to
derive, but it is roughly
O(fib(n)).
 That is, its growth is
proportional to the growth in the
value of the result, and the
growth rate of the Fibonacci
sequence is substantial.
 For example, fib(120) is
8,670,007,398,507,948,658,
051,921. If each recursive
call took a nanosecond,
fib(120) would take about
250,000 years to finish.
FIBONACCI SEQUENCES, REVISITED

•Notice that we are computing the same values over and over
again. For example fib gets called with 3 three times, and each of
these calls provokes four additional calls of fib.
•It doesn’t require a genius to think that it might be a good idea to
record the value returned by the first call, and then look it up
rather than compute it each time it is needed.
•This is called memoization, and is the key idea behind dynamic
programming.
•To evaluate fib(10) we need to compute fib(8) and fib(9).
But we already computed fib(8) when computing fib(9).
The trick is to remember these results. This is
MEMOIZATION
 Memoization effectively refers to remembering
("memoization" -> "memorandum" -> to be
remembered) results of method calls based on the
method inputs and then returning the remembered
result rather than computing the result again. You can
think of it as a cache for method results.
 Memoization is a way of caching the results of a function

call. If a function is memoized, evaluating it is simply a


matter of looking up the result you got the first time the
function was called with those parameters.
 This is recorded in the memoization cache. If the lookup

fails, that’s because the function has never been called


with those parameters. Only then do you need to run
the function itself.
 memoization can result in a massive speedup.
Dynamic Programming
1. The development of a dynamic-programming
algorithm can be broken into a sequence of four steps.
a. Characterize the structure of an optimal solution.
b. Recursively define the value of an optimal solution.
c. Compute the value of an optimal solution in a
bottom-up fashion.
d. Construct an optimal solution from computed
information
2. Dynamic Programming is not recursive.

3. DP solves the sub problems only once and then stores


it in the table.

4. In DP the sub-problems are not independent.

5. Example : Matrix chain multiplication


0/1 KNAPSACK PROBLEM
 A knapsack is a bag with straps, usually carried by soldiers to
help them take their valuables or things which they might need
during their journey.
 Given a set of items, each of which is associated with some
weight and value. Find the subset of items which can be carried
into the knapsack with weight limit W.
 It asks you to pick certain items from the set of items
such that their total weight is less than or equal to W and
the sum of their values is maximum.
 The knapsack problem or rucksack problem is a problem in
combinatorial optimization: Given a set of items, each with a
weight and a value, determine the number of each item to
include in a collection so that the total weight is less than or
equal to a given limit and the total value is as large as possible.
 0/1 means that either we can pick an item or we can leave
the item. It is impossible to take a fraction of the item.
Dynamic programming and divide and
conquer
 Divide & Conquer
1. The divide-and-conquer paradigm involves three steps at
each level of the recursion:
• Divide the problem into a number of sub problems.
• Conquer the sub problems by solving them recursively. If
the sub problem sizes are small enough, however, just solve
the sub problems in a straightforward manner.
• Combine the solutions to the sub problems into the solution
for the original problem.
2. They call themselves recursively one or more times to deal
with closely related sub problems.
3. D&C does more work on the sub-problems and hence has
more time consumption.
4. In D&C the sub problems are independent of each other.
5. Example: Merge Sort, Binary Search

You might also like