Artificial Intelligence - 14 - Data Visualization With Python
Artificial Intelligence - 14 - Data Visualization With Python
SCHOLARSHIP
2019
14. Data Visualization with Python
digitalent.kominfo.go.id
Outline
Module 1 - Introduction to Module 3 - Specialized Module 5 - Creating Maps and
Visualization Tools Visualization Tools Visualizing Geospatial Data
• Introduction to Data Visualization • Pie Charts • Introduction to Folium
• Introduction to Matplotlib • Box Plots • Maps with Markers
• Basic Plotting with Matplotlib • Scatter Plots • Choropleth Maps
• Line Plots • Bubble Plots
data = np.arange(10)
plt.plot(data)
Module 1 - Introduction to Visualization Tools
Basic Plotting with Matplotlib
fig = plt.figure()
ax1 = fig.add_subplot(2, 2, 1)
ax2 = fig.add_subplot(2, 2, 2)
ax3 = fig.add_subplot(2, 2, 3)
plt.plot(np.random.randn(50).cumsum(), 'k--')
ax1.hist(np.random.randn(100), bins=20, color='k', alpha=0.3)
ax2.scatter(np.arange(30), np.arange(30) + 3 * np.random.randn(30))
Module 1 - Introduction to Visualization Tools
Basic Plotting with Matplotlib
• pyplot.subplots options :
Module 1 - Introduction to Visualization Tools
Line Plots
import numpy as np
import matplotlib.pyplot as plt
def f(t):
return np.exp(-t) * np.cos(2*np.pi*t)
plt.figure(1)
plt.subplot(211)
plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k')
plt.subplot(212)
plt.plot(t2, np.cos(2*np.pi*t2), 'r--')
plt.show()
Module 1 - Introduction to Visualization Tools
Line Plots
Module 2 - Basic Visualization Tools
Area Plots
fig, (ax1, ax2, ax3) = plt.subplots(3, 1, sharex=True)
ax1.fill_between(x, 0, y1)
ax1.set_ylabel('between y1 and 0')
ax2.fill_between(x, y1, 1)
ax2.set_ylabel('between y1 and 1')
N_points = 100000
n_bins = 20
# We'll color code by height, but you could use any scalar
fracs = N / N.max()
# we need to normalize the data to 0..1 for the full range of the colormap
norm = colors.Normalize(fracs.min(), fracs.max())
# Now, we'll loop through our objects and set the color of each accordingly
for thisfrac, thispatch in zip(fracs, patches):
color = plt.cm.viridis(norm(thisfrac))
thispatch.set_facecolor(color)
plt.show()
Module 2 - Basic Visualization Tools
Bar Charts
Sample bar charts 1
import matplotlib.pyplot as plt; plt.rcdefaults()
import numpy as np
import matplotlib.pyplot as plt
plt.show()
Module 2 - Basic Visualization Tools
Bar Charts
Sample bar charts 1
Module 2 - Basic Visualization Tools
Bar Charts
Sample bar charts 2
import matplotlib.pyplot as plt; plt.rcdefaults()
import numpy as np
import matplotlib.pyplot as plt
plt.show()
Module 2 - Basic Visualization Tools
Bar Charts
Sample bar charts 2
Module 2 - Basic Visualization Tools
Bar Charts
Sample bar charts 3
import numpy as np
import matplotlib.pyplot as plt rects2 = plt.bar(index + bar_width,
means_guido, bar_width,
# data to plot alpha=opacity,
n_groups = 4 color='g',
means_frank = (90, 55, 40, 65) label='Guido')
means_guido = (85, 62, 54, 20)
plt.xlabel('Person')
# create plot plt.ylabel('Scores')
fig, ax = plt.subplots() plt.title('Scores by person')
index = np.arange(n_groups) plt.xticks(index + bar_width, ('A',
bar_width = 0.35 'B', 'C', 'D'))
opacity = 0.8 plt.legend()
# Data to plot
labels = 'Python', 'C++', 'Ruby', 'Java'
sizes = [215, 130, 245, 210]
colors = ['gold', 'yellowgreen', 'lightcoral',
'lightskyblue']
explode = (0.1, 0, 0, 0) # explode 1st slice
# Plot
plt.pie(sizes, explode=explode, labels=labels,
colors=colors,
autopct='%1.1f%%', shadow=True, startangle=140)
plt.axis('equal')
plt.show()
Module 3 - Specialized Visualization Tools
Pie Charts
Sample Pie charts 1
Module 3 - Specialized Visualization Tools
Pie Charts
Sample Pie charts 2
import matplotlib.pyplot as plt
# Create data
N = 500
x = np.random.rand(N)
y = np.random.rand(N)
colors = (0,0,0)
area = np.pi*3
# Plot
plt.scatter(x, y, s=area, c=colors, alpha=0.5)
plt.title('Scatter plot pythonspot.com')
plt.xlabel('x')
plt.ylabel('y')
plt.show()
Module 3 - Specialized Visualization Tools
Scatter Plots
Sample Scatter Plots 1
Module 3 - Specialized Visualization Tools
Scatter Plots
Sample Scatter Plots 2
rng = np.random.RandomState(0)
for marker in ['o', '.', ',', 'x', '+', 'v', '^', '<', '>', 's', 'd']:
plt.plot(rng.rand(5), rng.rand(5), marker,
label="marker='{0}'".format(marker))
plt.legend(numpoints=1)
plt.xlim(0, 1.8);
Module 3 - Specialized Visualization Tools
Scatter Plots
Sample Scatter Plots 2
Module 3 - Specialized Visualization Tools
Bubble Plots
Sample Bubble Plots 1
import numpy as np
import matplotlib.pyplot as plt
N = 50
x = np.random.rand(N)
y = np.random.rand(N)
colors = np.random.rand(N)
area = (30 * np.random.rand(N))**2 # 0 to 15 point radii
plt.xlabel(iris.feature_names[0])
plt.ylabel(iris.feature_names[1]);
Module 3 - Specialized Visualization Tools
Bubble Plots
Sample Bubble Plots 2
Module 3 - Specialized Visualization Tools
Bubble Plots
Sample Bubble Plots 3 (in polar coordinates)
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111, projection='polar')
c = ax.scatter(theta, r, c=colors, s=area, cmap='hsv', alpha=0.75)
Module 3 - Specialized Visualization Tools
Bubble Plots
Sample Bubble Plots 3
Module 4 - Advanced Visualization Tools
Waffle Charts
Require pywaffle library
Installation : pip install pywaffle
Links : https://pypi.org/project/pywaffle/
Module 4 - Advanced Visualization Tools
Waffle Charts
Sample Waffle Plots 1
import matplotlib.pyplot as plt
from pywaffle import Waffle
# The values are rounded to 10 *
5 blocks
fig = plt.figure(
FigureClass=Waffle,
rows=5,
columns=10,
values=[48, 46, 3]
)
plt.show()
Module 4 - Advanced Visualization Tools
Waffle Charts
Sample Waffle Plots 2
import matplotlib.pyplot as plt
from pywaffle import Waffle
data = {'Democratic': 48, 'Republican': 46, 'Libertarian': 3}
fig = plt.figure(
FigureClass=Waffle,
rows=5,
values=data,
legend={'loc': 'upper left', 'bbox_to_anchor': (1.1, 1)}
)
plt.show()
Module 4 - Advanced Visualization Tools
Waffle Charts
Sample Waffle Plots 3
data = {'Democratic': 48, 'Republican': 46, 'Libertarian': 3}
fig = plt.figure(
FigureClass=Waffle,
rows=5,
values=data,
colors=("#983D3D", "#232066", "#DCB732"),
title={'label': 'Vote Percentage in 2016 US Presidential Election',
'loc': 'left'},
labels=["{0} ({1}%)".format(k, v) for k, v in data.items()],
legend={'loc': 'lower left', 'bbox_to_anchor': (0, -0.4), 'ncol':
len(data), 'framealpha': 0},
plot_direction='NW'
)
fig.gca().set_facecolor('#EEEEEE')
fig.set_facecolor('#EEEEEE')
plt.show()
Module 4 - Advanced Visualization Tools
Word Clouds
Require matplotlib, pandas, wordcloud, pillow
Instalation
pip install matplotlib
pip install pandas
pip install wordcloud
Pip install Pillow
Module 4 - Advanced Visualization Tools
Word Clouds
Sample Word Clouds1
# Libraries
from wordcloud import WordCloud
import matplotlib.pyplot as plt
digitalent.kominfo
digitalent.kominfo
DTS_kominfo
Digital Talent Scholarship 2019
digitalent.kominfo.go.id