Data Visualization
Prof. Dr. Noman Islam
Introduction
• matplotlib is a desktop plotting package
designed for creating (mostly twodimensional)
publication-quality plots.
• Over time, matplotlib has spawned a number
of add-on toolkits for data visualization that
use matplotlib for their underlying plotting.
One of these is seaborn
Preparing for visualization
• import matplotlib as mpl
• Import [Link] as plt
• [Link]('default')
• %matplotlib inline
Plotting sin function
• x = [Link](0, [Link]*2, 0.05)
• y = [Link](x)
• [Link]("angle")
• [Link]("sine")
• [Link]('sine wave')
• [Link](True, which='both')
• [Link](x,y)
Plotting lines
• x = [Link](0, 20)
• [Link](x, .5 + x)
• [Link](x, 1 + 2 * x, '--‘)
Customizing plots
• [Link](x, .5 + x, color='blue') # specify color by name
• [Link](x, 1 + 2 * x, '--', color='#FFDD44') # Hex code (RRGGBB
from 00 to FF)
• [Link](x, [Link](x), (1.0,0.2,0.3)) # RGB tuple, values 0 and 1
Customizing plots
• x = [Link](0, 10, 1000)
• [Link](x, [Link](x), linestyle='solid')
• [Link](x, [Link](x), linestyle='dotted')
Specifying limits
• [Link](x, [Link](x))
• [Link](-1, 11)
• [Link](-1.5, 1.5);
Labeling axis
• [Link]("titlte")
• [Link]("x-axis")
• [Link]("y-axis")
Plotting legends
• [Link](x, [Link](x), '-g', label='sin(x)')
• [Link](x, [Link](x), ':b', label='cos(x)')
• [Link]()
Scatter plot
• from [Link] import load_iris
• iris = load_iris()
• features = [Link].T
• [Link](features[0], features[1], alpha=0.2,
s=100*features[3], c=[Link], cmap='viridis')
• [Link](iris.feature_names[0])
• [Link](iris.feature_names[1])
Bar plot
• import [Link] as plt
• %matplotlib inline
• [Link]('ggplot')
• x = ['Viettel', 'VNPT', 'Mobiphone']
• revenue = (37600000, 6445000, 6045000)
• x_pos = [i for i, _ in enumerate(x)]
• [Link](x_pos, revenue, color='green')
• [Link]("Telco")
• [Link]("VND")
• [Link]("Telecom service revenues in Vietnam")
• [Link](x_pos, x)
• [Link]()
Plotting histogram
• import numpy as np
• import [Link] as plt
• X = [Link](1000)
• [Link](X, bins = 20)
• [Link]()
Box plot
• import numpy as np
• import [Link] as plt
• data = [Link](100)
• [Link](data)
• [Link]()
Violin plot
Plotting pie chart
• import [Link] as plt
• telcos = 'Viettel', 'VNPT', 'Mobiphone',
'Vietnammobile', 'Gtel'
• data = [46.7, 22.2, 26.1, 2.9, 2.1]
• fig1, ax1 = [Link]()
• [Link](data, labels=telcos, autopct='%1.1f%%',
shadow=True, startangle=90)
• [Link]('equal')
• [Link]()
Saving figure
• [Link]('my_figure.png')
• [Link]('my_figure.png',
transparent=True)
Figures and subplot
• Plots in matplotlib reside within a Figure
object
– fig = [Link]()
• You can’t make a plot with a blank figure. You
have to create one or more subplots using
add_subplot:
– ax1 = fig.add_subplot(2, 2, 1)
– [Link]([Link](50).cumsum(), 'k--')
• The 'k--' is a style option instructing matplotlib
to plot a black dashed line.
• The objects returned by fig.add_subplot here
are AxesSubplot objects, on which you can
directly plot on the other empty subplots by
calling each one’s instance method
• x = [Link](0, 2*[Link], 400)
• y = [Link](x**2)
• f, (ax1, ax2) = [Link](1, 2)
• [Link](x, y)
• ax1.set_title('Sharing Y axis')
• [Link](x, y)
• fig, axes = [Link](2, 3)
• [Link](x, y, 'g--')
• [Link](x, y, linestyle='--', color='g')
• [Link](randn(30).cumsum(), 'ko--')
Plotting with pandas
• s = [Link]([Link](10).cumsum(),
index=[Link](0, 100, 10))
• [Link]()
• df = [Link]([Link](10,
4).cumsum(0), columns=['A', 'B', 'C', 'D'],
index=[Link](0, 100, 10))
• [Link]()
• [Link]()
Seaborn
• import seaborn as sns
• [Link]()
• [Link](x, y)
• [Link]('ABCDEF', ncol=2, loc='upper left')
Plotting histogram
• data = [Link].multivariate_normal([0, 0],
[[5, 2], [2, 2]], size=2000)
• data = [Link](data, columns=['x', 'y'])
• for col in 'xy':
– [Link](data[col], normed=True, alpha=0.5)
Kernel density plot
• for col in 'xy':
– [Link](data[col], shade=True)
Distplot
• [Link](data['x'])
• [Link](data['y']);
Pair plot
• iris = sns.load_dataset("iris")
• [Link]()
• [Link](iris, hue='species', size=2.5);
Factor plot
• tips = sns.load_dataset('tips')
• [Link]()
• with sns.axes_style(style='ticks'):
– g = [Link]("day", "total_bill", "sex",
data=tips, kind="box")
– g.set_axis_labels("Day", "Total Bill");
Scatter plot
• [Link](x="Views", y="Upvotes", data = df)
• [Link](x="Views", y="Upvotes", hue =
"Tag", data = df)
• [Link](x="Views", y="Upvotes", hue =
"Answers", data = df);
Box plot
• [Link](x="education",
y="avg_training_score", kind = "box",
data=df2)
Violin plot
• [Link](x="education",
y="avg_training_score", hue = "is_promoted",
kind = "violin", data=df2)
Heatmaps
• corrmat = [Link]()
• f, ax = [Link](figsize=(9, 6))
• [Link](corrmat, vmax=.8, square=True)
Other tools
• As is common with open source, there are a
plethora of options for creating graphics in
Python
• With tools like Bokeh and Plotly, it’s now
possible to specify dynamic, interactive
graphics in Python that are destined for a web
browser