Lecture3 Matplotlib
Lecture3 Matplotlib
Matplotlib
Data visualization using Matplotlib
• Matplotlib is a plotting library for Python.
• Conventionally, the package is imported into the Python script by
adding the following statement −
• import matplotlib.pyplot as plt
• Here pyplot() is the most important function in matplotlib library,
which is used to plot 2D data.
Q. Which of the following library in Python is used for plotting
graphs and visualization.
A. Matplotlib
B. Pandas
C. NumPy
D. None of the above
Answer : A
• If plt.plot(ypoints, marker = '*')
• If plt.plot(ypoints, '*')
Q. The most important object defined in NumPy is an N-dimensional
array type called?
A. ndarray
B. narray
C. nd_array
D. darray
Answer : A
Draw 2 plots horizontally
Draw 2 plots vertically
Line Plot
• Plot the equation y = 2x + 5
• Instead of the linear graph, the values can be displayed discretely by adding a format
string to the plot() function.
Character Color
'b' Blue
'g' Green
'r' Red
'c' Cyan
'm' Magenta
'y' Yellow
'k' Black
'w' White
Bar chart in python
• A bar chart or bar graph is a chart or graph that presents categorical
data with rectangular bars with heights or lengths proportional to the
values that they represent. The bars can be plotted vertically or
horizontally.
• A bar graph shows comparisons among discrete categories. One axis
of the chart shows the specific categories being compared, and the
other axis represents a measured value.
Pie Chart
• A pie chart is a type of graph that records data in a circular manner
that is further divided into sectors for representing the data of that
particular part out of the whole part.
• Each of these sectors or slices represents the proportionate part of the
whole.
• Pie charts, also commonly known as pie diagrams help in interpreting
and representing the data more clearly. It is also used to compare the
given data.
• As you can see the pie chart draws one piece (called a wedge) for
each value in the array (in this case [35, 25, 25, 15]).
• By default the plotting of the first wedge starts from the x-axis and
move counterclockwise:
A simple pie chart
• import matplotlib.pyplot as plt
• import numpy as np
• y=np.array([35,25,25,15])
• plt.pie(y)
• plt.show()
Adding labels to a pie chart
• import matplotlib.pyplot as plt
• import numpy as np
• y=np.array([35,25,25,15])
• mylabels = ['Apples','Bananas','Cherries','Dates']
• plt.pie(y,labels = mylabels)
• plt.show()
Add legend in a pie chart
• import matplotlib.pyplot as plt
• import numpy as np
• y=np.array([35,25,25,15])
• mylabels = ['Apples','Bananas','Cherries','Dates']
• plt.pie(y,labels = mylabels)
• plt.legend()
• plt.show()
Histogram in python
• A histogram is a graphical representation that organizes a group of
data points into user-specified ranges.
• A histogram is a graph showing frequency distributions.
• It is a graph showing the number of observations within each given
interval.
• Similar in appearance to a bar graph, the histogram condenses a data
series into an easily interpreted visual by taking many data points and
grouping them into logical ranges or bins.
Say you ask for the height of 250 people, you
might end up with a histogram like this:
Creating Histograms
• In Matplotlib, we use the hist() function to create histograms.
• The hist() function will use an array of numbers to create a histogram,
the array is sent into the function as an argument.
• For simplicity we use NumPy to randomly generate an array with 250
values, where the values will concentrate around 170, and the
standard deviation is 10.
Normal data distribution, or the Gaussian data distribution
or the Bell curve using NumPy:
• import numpy as np
print(x)
Gaussian data distribution : to create an array where the values are concentrated around
a given value.
A random result is generated like this:
The hist() function will read the array and produce a
histogram:
plt.hist(x)
plt.show()
Extra
Sine Wave Plot
import numpy as np
import matplotlib.pyplot as plt
# Compute the x and y coordinates for points on a sine curve
x = np.arange(0, 3 * np.pi, 0.1)
y = np.sin(x)
plt.title("sine wave form")
# Plot the points using matplotlib
plt.plot(x, y)
plt.show()
subplot()
• The subplot() function allows you to plot different things in the same figure. In the
following script, sine and cosine values are plotted.
Program:
import numpy as np
import matplotlib.pyplot as plt
# Compute the x and y coordinates for points on sine and cosine curves
x = np.arange(0, 3 * np.pi, 0.1)
y_sin = np.sin(x)
y_cos = np.cos(x)
# Set up a subplot grid that has height 2 and width 1,
# and set the first such subplot as active.
plt.subplot(2, 1, 1)
# Make the first plot
plt.plot(x, y_sin)
plt.title('Sine')
# Set the second subplot as active, and make the second plot.
plt.subplot(2, 1, 2)
plt.plot(x, y_cos)
plt.title('Cosine')
# Show the figure.
Horizontal subplot
import numpy as np
import matplotlib.pyplot as plt
t=np.arange(0.0,20.0,1)
s=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
plt.subplot(2,1,1)
plt.title("sublot 1")
plt.plot(t,s)
plt.subplot(2,1,2)
plt.title("sublot 2")
plt.plot(t,s,"r-")
plt.show()
Vertical subplot
import numpy as np
import matplotlib.pyplot as plt
t=np.arange(0.0,20.0,1)
s=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
plt.subplot(1,2,1)
plt.title("sublot 1")
plt.plot(t,s)
plt.subplot(1,2,2)
plt.title("sublot 2")
plt.plot(t,s,"r-")
plt.show()
Subplot grid
import numpy as np
import matplotlib.pyplot as plt
t=np.arange(0.0,20.0,1)
s=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
plt.subplot(2,2,1)
plt.title("sublot 1")
plt.plot(t,s)
plt.subplot(2,2,2)
plt.title("sublot 2")
plt.plot(t,s,"r-")
plt.subplot(2,2,3)
plt.title("sublot 2")
plt.plot(t,s,"g-")
plt.subplot(2,2,4)
plt.title("sublot 2")
plt.plot(t,s,"y-")
plt.show()
import matplotlib.pyplot as plt; plt.rcdefaults()
import numpy as np
import matplotlib.pyplot as plt
objects = ('Python', 'C++', 'Java', 'Perl', 'Scala', 'Lisp')
y_pos = np.arange(len(objects))
performance = [10,8,6,4,2,1]
plt.bar(y_pos, performance, align='center', alpha=0.5)
plt.xticks(y_pos, objects)
plt.ylabel('Usage')
plt.title('Programming language usage')
plt.show()