UNIT-
IV
DATA VISUALIZATION
Prepared by
[Link] AP/IT
Syllabus-UNIT-IV
Importing Matplotlib – Simple Line Plots – Simple Scatter Plots – Density
and Contour Plots – Histograms - 2D and 3D Plotting
Importing Matplotlib
•Matplotlib is a cross-platform, data visualization and graphical plotting library for Python and
its numerical extension NumPy.
•Matplotlib is a comprehensive library for creating static, animated, and interactive
visualizations in Python.
•Matplotlib is a plotting library for the Python programming language. It allows to make
quality charts in few lines of code. Most of the other python plotting library are build on top of
Matplotlib.
•The library is currently limited to 2D output, but it still provides you with the means to
express graphically the data patterns.
Visualizing Information: Starting with Graph
•Data visualization is the presentation of quantitative information in a graphical form. In other
words, data visualizations turn large and small datasets into visuals that are easier for the human
brain to understand and process.
•Good data visualizations are created when communication, data science, and design collide.
Data visualizations done right offer key insights into complicated datasets in ways that are
meaningful and intuitive.
•A graph is simply a visual representation of numeric data. MatPlotLib supports a large number
of graph and chart types.
•Matplotlib is a popular Python package used to build plots. Matplotlib can also be used to make
3D
plots and animations.
•Line plots can be created in Python with Matplotlib's pyplot library. To build a line plot,
first import Matplotlib. It is a standard convention to import Matplotlib's pyplot library as plt.
•import [Link] as
plt
•[Link]([1,2,3],[5,7,4])
•[Link]()
Simple Line Plots
Simple Line Plots
•More than one line can be in the plot. To add another line, just call the plot (x,y) function
again.
import [Link] as
plt import numpy as np
x = [Link](-1, 1, 50)
y1 = 2*x+ 1
y2 = 2**x + 1
[Link](num = 3, figsize=(8,
5)) [Link](x, y2)
[Link](x, y1,
linewidth=1.0,
linestyle='--'
)
[Link]()
Example 5.1.1: Write a simple python program that draws a line graph where x = [1,2,3,4]
and y = [1,4,9,16] and gives both axis label as "X-axis" and "Y-axis".
import [Link] as plt
import numpy as np
# define data values
x = [Link]([1, 2, 3, 4]) # X-axis points
y = x*2 # Y-axis points
print("Values of :")
print("Values of Y):")
print (Y)
[Link](X, Y)
# Set the x axis label of the current
axis. [Link]('x-axis')
# Set the y axis label of the current
axis. [Link]('y-axis')
# Set a title
[Link]('Draw a line.')
# Display the figure.
[Link]()
Setting the Axis, Ticks,
Grids
•The axes define the x and y plane of the graphic. The x axis runs horizontally, and the y axis
runs vertically.
•An axis is added to a plot layer. Axis can be thought of as sets of x and y axis that lines and
bars are drawn on.
•An Axis contains daughter attributes like axis labels, tick labels, and line thickness.
• The following code shows how to obtain access to the axes for a plot :
fig = [Link]()
axes = fig.add_axes([0.1, 0.1, 0.8, 0.8])
# left, bottom, width, height (range 0 to
1) [Link](x, y, 'r')
axes.set_xlabel('x')
axes.set_ylabel('y')
axes.set_title('title');
# import required modules
import [Link] as plt
import numpy as np
import math
# assign coordinates
x = [Link](0, [Link]*2, 0.05)
y = [Link](x)
ax = [Link]()
[Link]("x-axis")
[Link]("y-axis")
# depict illustration
[Link](x, y, color="lime")
# setting ticks for x-axis
ax.set_xticks([0, 2, 4, 6])
# setting ticks for y-axis
ax.set_yticks([-1, 0, 1])
# setting label for y tick
ax.set_yticklabels(["sin(-90deg)", "sin(0deg)", "sin(90deg)"])
[Link]()
Scatter Plots
•A scatter plot is a visual representation of how two variables relate to each
other.
•we can use scatter plots to explore the relationship between two variables.
import matplotlib. pyplot as
plt #X axis values:
x = [2,3,7,29,8,5,13,11,22,33]
# Y axis values:
y = [4,7,55,43,2,4,11,22,33,44]
# Create scatter
plot: [Link](x, y)
[Link]()
Example: We can create a simple scatter plot in Python by passing
x and y values to [Link]():
# scatter_plotting.py
import matplotlib. pyplot as plt
plt. style. use('fivethirtyeight')
x = [2, 4, 6, 6, 9, 2, 7, 2, 6, 1,
8, 4, 5, 9, 1, 2, 3, 7, 5, 8, 1, 3]
y = [7, 8, 2, 4, 6, 4, 9, 5, 9, 3,
6, 7, 2, 4, 6, 7, 1, 9, 4, 3, 6, 9]
plt. scatter(x, y)
plt. show()
Creating Advanced
Scatterplots
•Scatterplots are especially important for data science because they can show data patterns that aren't
obvious when viewed in other ways.
import [Link] as plt
x_axis1 =[1, 2, 3, 4, 5, 6, 7, 8, 9,
10]
y_axis1 =[5, 16, 34, 56, 32, 56, 32,
12, 76, 89]
x_axis2 = [1, 2, 3, 4, 5, 6, 7, 8, 9,
10]
y_axis2 = [53, 6, 46, 36, 15, 64, 73,
25, 82, 9]
[Link]("Prices over 10 years")
[Link](x_axis1, y_axis1, color = 'darkblue', marker='x', label="item
1") [Link](x_axis2, y_axis2, color='darkred', marker='x', label="item
2") [Link]("Time (years)")
[Link]("Price (dollars)")
[Link](True)
Contour
Plots
Density and Contour Plots
•It is useful to display three-dimensional data in two dimensions using contours or
color- coded regions.
Three Matplotlib functions are used for this purpose. They are :
a) [Link] for contour plots,
b) [Link] for filled contour plots,
c) [Link] for showing images.
1. Contour plot
•A contour line or isoline of a function of two variables is a curve along which the
function has a constant value.
•It is a cross-section of the three-dimensional graph of the function f(x, y) parallel to the
x, y plane.
• Contour lines are used in Geography and Meteorology.
•In cartography, a contour line joins points of equal height above a given level, such
as mean sea level.
• A contour line of a function with two variables is a curve which connects points with the
same values.
import numpy as np
xlist = [Link](-3.0, 3.0, 3)
ylist = [Link](-3.0, 3.0, 4)
X, Y = [Link](xlist, ylist
print(xlist)
print(ylist)
print(X)
print(Y)
Changing the colours and the line style
import [Link] as plt
[Link]()
cp = [Link](X, Y, Z, colors='black', linestyles='dashed')
[Link](cp, inline=True,
fontsize=10)
[Link]('Contour Plot')
[Link]('x (cm))
[Link]('y (cm)')
[Link]()
•When creating a contour plot, we can also specify the color map. There are different classes of color
maps. Matplotlib gives the following guidance :
a)Sequential: Change in lightness and often saturation of color incrementally, often using a single hue;
should be used for representing information that has ordering.
b) Diverging: Change in lightness and possibly saturation of two different colors that meet in the middle at
an unsaturated color; should be used when the information being plotted has a critical middle value, such as
topography or when the data deviates around zero.
c)Cyclic : Change in lightness of two different colors that meet in the middle and beginning/end at
an unsaturated color; should be used for values that wrap around at the endpoints, such as phase angle,
wind
direction, or time of day.
d)Qualitative: Often are miscellaneous colors; should be used to represent information which does not
have ordering or relationships.
•This data has both positive and negative values, which zero representing a node for the wave function. There
are three important display options for contour plots: the undisplaced shape key, the scale factor, and the contour
scale.
Sequential Diverging
Cyclic Qualitative
Histogram
31
Histogram
•In a histogram, the data are grouped into ranges (e.g. 10 - 19, 20 - 29) and then plotted
as connected bars.
•Each bar represents a range of data.
•The width of each bar is proportional to the width of each category, and the height
is proportional to the frequency or percentage of that category.
•It provides a visual interpretation of numerical data by showing the number of data
points
that fall within a specified range of values called "bins".
32
•Histograms can display a large amount of
data and the frequency of the data values.
The median and distribution of the data can be
determined by a histogram.
In addition, it can show any outliers or gaps
in the data.
•Matplotlib provides a dedicated function
to compute and display histograms:
[Link]()
33
Code for Histogram
import numpy as np
import [Link] as plt
x = 40* [Link](50000)
[Link](x, 20, range=(-50, 50), histtype='stepfilled',
align='mid', color=‘g', label="Test Data')
[Link]()
[Link](' Histogram')
[Link]()
34
Matplotlib legend on bottom
import [Link] as
plt import numpy as np
y1 =
[2,4,6,8,10,12,14,16,18,20]
y2 =
[10,11,12,13,14,15,16,17,18,1
9]
x = [Link](10)
fig = [Link]()
ax =
[Link](111)
[Link](x, y, label='$y = numbers')
35
[Link](x, y2, label='$y2= = other numbers')
Subplots
36
Subplots
• Subplots mean groups of axes that can exist in a single matplotlib figure.
•subplots() function in the matplotlib library, helps in creating multiple layouts of
subplots.
•It provides control over all the individual plots that are created.
• subplots() without arguments returns a Figure and a single Axes.
•This is actually the simplest and recommended way of creating a single Figure and
Axes.
37
fig, ax = [Link]()
[Link](x,y)
ax.set_title('A single plot')
•There are 3 different ways (at least) to create plots (called axes) in
matplotlib. They are:
✓ [Link](),
✓ figure.add_axis() and
✓ [Link]()
•[Link](): The most basic method of creating an axes is to use the [Link] function. It takes optional argument for
figure coordinate system. These numbers represent [bottom, left, width, height] in the figure coordinate system, which ranges
from 0 at the bottom left of the figure to 1 at the top right of the figure.
• Plot just one figure with (x,y) coordinates: [Link](x, y).
•By calling subplot(n,m,k), we subdidive the figure into n rows and m columns and specify that plotting should be done
on the subplot number k. Subplots are numbered row by row, from left to right.
38
import [Link]
import numpy as np
from math import pi
[Link](figsize=(8,4)) # set dimensions of the
figure x=[Link] (0,2*pi,100)
for i in range(1,7):
[Link](2,3,i) # create subplots on a grid with 2 rows and 3
columns [Link]([]) # set no ticks on x-axis
[Link]([]) # set no ticks on y-
axis [Link]([Link](x), [Link](i*x))
[Link]('subplot'+'(2,3,' + str(i)+')')
[Link]()
39
Three Dimensional
Plotting
54
Three Dimensional Plotting
•Matplotlib is the most popular choice for data visualization.
•While initially developed for plotting 2-D charts like histograms, bar charts, scatter
plots, line plots, etc.,
•Matplotlib has extended its capabilities to offer 3D plotting modules as well.
First import the library :
import [Link] as plt
from mpl_toolkits.mplot3d import Axes3D
55
•The second import of the Axes3D class is required for enabling 3D
projections.
•It is, otherwise, not used anywhere else.
•Create figure and axes
fig = [Link](figsize=(4,4))
ax = fig.add_subplot(111, projection='3d')
56
Example :
import [Link] as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
fig=[Link](figsize=(8,8))
ax=[Link](projection='3d')
[Link]()
t=[Link](0,10*[Link], [Link]/50)
x=[Link](t)
y=[Link](t)
ax.plot3D(x,y,t)
ax.set_title('3D
Parametric
Plot')
# Set axes label
ax.set_xlabel('x',labelpad=20)
ax.set_ylabel('y', labelpad=20)
ax.set_zlabel('t', labelpad=20)
57
[Link]()