import matplotlib.pyplot as plt
= [1, 2, 3, 4, 5, 6, 7, 8, 9]
x = [1, 3, 5, 3, 1, 3, 5, 3, 1]
y1 = [2, 4, 6, 4, 2, 4, 6, 4, 2]
y2 ="line L")
plt.plot(x, y1, label="line H")
plt.plot(x, y2, label
plt.plot()
"x axis")
plt.xlabel("y axis")
plt.ylabel("Line Graph Example")
plt.title(
plt.legend() plt.show()
Visualizations
A common use for notebooks is data visualization using charts. Colaboratory makes this easy with several charting tools available as Python imports.
Matplotlib
Matplotlib is the most common charting package, see its documentation for details, and its examples for inspiration.
Line Plots
Bar Plots
import matplotlib.pyplot as plt
# Look at index 4 and 6, which demonstrate overlapping cases.
= [1, 3, 4, 5, 6, 7, 9]
x1 = [4, 7, 2, 4, 7, 8, 3]
y1
= [2, 4, 6, 8, 10]
x2 = [5, 6, 2, 6, 2]
y2
# Colors: https://matplotlib.org/api/colors_api.html
="Blue Bar", color='b')
plt.bar(x1, y1, label="Green Bar", color='g')
plt.bar(x2, y2, label
plt.plot()
"bar number")
plt.xlabel("bar height")
plt.ylabel("Bar Chart Example")
plt.title(
plt.legend() plt.show()
Histograms
import matplotlib.pyplot as plt
import numpy as np
# Use numpy to generate a bunch of random data in a bell curve around 5.
= 5 + np.random.randn(1000)
n
= [m for m in range(len(n))]
m
plt.bar(m, n)"Raw Data")
plt.title(
plt.show()
=20)
plt.hist(n, bins"Histogram")
plt.title(
plt.show()
=True, bins=20)
plt.hist(n, cumulative"Cumulative Histogram")
plt.title( plt.show()
Scatter Plots
import matplotlib.pyplot as plt
= [2, 3, 4]
x1 = [5, 5, 5]
y1
= [1, 2, 3, 4, 5]
x2 = [2, 3, 2, 3, 4]
y2 = [6, 8, 7, 8, 7]
y3
# Markers: https://matplotlib.org/api/markers_api.html
plt.scatter(x1, y1)='v', color='r')
plt.scatter(x2, y2, marker='^', color='m')
plt.scatter(x2, y3, marker'Scatter Plot Example')
plt.title( plt.show()
Stack Plots
import matplotlib.pyplot as plt
= [ 1, 2, 3, 4, 5, 6, 7, 8, 9]
idxes = [23, 40, 28, 43, 8, 44, 43, 18, 17]
arr1 = [17, 30, 22, 14, 17, 17, 29, 22, 30]
arr2 = [15, 31, 18, 22, 18, 19, 13, 32, 39]
arr3
# Adding legend for stack plots is tricky.
='r', label = 'D 1')
plt.plot([], [], color='g', label = 'D 2')
plt.plot([], [], color='b', label = 'D 3')
plt.plot([], [], color
= ['r', 'g', 'b'])
plt.stackplot(idxes, arr1, arr2, arr3, colors'Stack Plot Example')
plt.title(
plt.legend() plt.show()
Pie Charts
import matplotlib.pyplot as plt
= 'S1', 'S2', 'S3'
labels = [56, 66, 24]
sections = ['c', 'g', 'y']
colors
=labels, colors=colors,
plt.pie(sections, labels=90,
startangle= (0, 0.1, 0),
explode = '%1.2f%%')
autopct
'equal') # Try commenting this out.
plt.axis('Pie Chart Example')
plt.title( plt.show()
fill_between and alpha
import matplotlib.pyplot as plt
import numpy as np
= 200 + np.random.randn(100)
ys = [x for x in range(len(ys))]
x
'-')
plt.plot(x, ys, 195, where=(ys > 195), facecolor='g', alpha=0.6)
plt.fill_between(x, ys,
"Fills and Alpha Example")
plt.title( plt.show()
Subplotting using Subplot2grid
import matplotlib.pyplot as plt
import numpy as np
def random_plots():
= []
xs = []
ys
for i in range(20):
= i
x = np.random.randint(10)
y
xs.append(x)
ys.append(y)
return xs, ys
= plt.figure()
fig = plt.subplot2grid((5, 2), (0, 0), rowspan=1, colspan=2)
ax1 = plt.subplot2grid((5, 2), (1, 0), rowspan=3, colspan=2)
ax2 = plt.subplot2grid((5, 2), (4, 0), rowspan=1, colspan=1)
ax3 = plt.subplot2grid((5, 2), (4, 1), rowspan=1, colspan=1)
ax4
= random_plots()
x, y
ax1.plot(x, y)
= random_plots()
x, y
ax2.plot(x, y)
= random_plots()
x, y
ax3.plot(x, y)
= random_plots()
x, y
ax4.plot(x, y)
plt.tight_layout() plt.show()
Plot styles
Colaboratory charts use Seaborn’s custom styling by default. To customize styling further please see the matplotlib docs.
3D Graphs
3D Scatter Plots
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import axes3d
= plt.figure()
fig = fig.add_subplot(111, projection = '3d')
ax
= [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
x1 = np.random.randint(10, size=10)
y1 = np.random.randint(10, size=10)
z1
= [-1, -2, -3, -4, -5, -6, -7, -8, -9, -10]
x2 = np.random.randint(-10, 0, size=10)
y2 = np.random.randint(10, size=10)
z2
='b', marker='o', label='blue')
ax.scatter(x1, y1, z1, c='g', marker='D', label='green')
ax.scatter(x2, y2, z2, c
'x axis')
ax.set_xlabel('y axis')
ax.set_ylabel('z axis')
ax.set_zlabel("3D Scatter Plot Example")
plt.title(
plt.legend()
plt.tight_layout() plt.show()
3D Bar Plots
import matplotlib.pyplot as plt
import numpy as np
= plt.figure()
fig = fig.add_subplot(111, projection = '3d')
ax
= [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
x = np.random.randint(10, size=10)
y = np.zeros(10)
z
= np.ones(10)
dx = np.ones(10)
dy = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
dz
='g')
ax.bar3d(x, y, z, dx, dy, dz, color
'x axis')
ax.set_xlabel('y axis')
ax.set_ylabel('z axis')
ax.set_zlabel("3D Bar Chart Example")
plt.title(
plt.tight_layout() plt.show()
Wireframe Plots
import matplotlib.pyplot as plt
= plt.figure()
fig = fig.add_subplot(111, projection = '3d')
ax
= axes3d.get_test_data()
x, y, z
= 2, cstride = 2)
ax.plot_wireframe(x, y, z, rstride
"Wireframe Plot Example")
plt.title(
plt.tight_layout() plt.show()
Seaborn
There are several libraries layered on top of Matplotlib that you can use in Colab. One that is worth highlighting is Seaborn:
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
# Generate some random data
= 20
num_points # x will be 5, 6, 7... but also twiddled randomly
= 5 + np.arange(num_points) + np.random.randn(num_points)
x # y will be 10, 11, 12... but twiddled even more randomly
= 10 + np.arange(num_points) + 5 * np.random.randn(num_points)
y
sns.regplot(x, y) plt.show()
That’s a simple scatterplot with a nice regression line fit to it, all with just one call to Seaborn’s regplot.
Here’s a Seaborn heatmap:
import matplotlib.pyplot as plt
import numpy as np
# Make a 10 x 10 heatmap of some random data
= 10
side_length # Start with a 10 x 10 matrix with values randomized around 5
= 5 + np.random.randn(side_length, side_length)
data # The next two lines make the values larger as we get closer to (9, 9)
+= np.arange(side_length)
data += np.reshape(np.arange(side_length), (side_length, 1))
data # Generate the heatmap
sns.heatmap(data) plt.show()
Altair
Altair is a declarative visualization library for creating interactive visualizations in Python, and is installed and enabled in Colab by default.
For example, here is an interactive scatter plot:
import altair as alt
from vega_datasets import data
= data.cars()
cars
alt.Chart(cars).mark_point().encode(='Horsepower',
x='Miles_per_Gallon',
y='Origin',
color ).interactive()
For more examples of Altair plots, see the Altair snippets notebook or the external Altair Example Gallery.
Plotly
Sample
from plotly.offline import iplot
import plotly.graph_objs as go
= [
data
go.Contour(=[[10, 10.625, 12.5, 15.625, 20],
z5.625, 6.25, 8.125, 11.25, 15.625],
[2.5, 3.125, 5., 8.125, 12.5],
[0.625, 1.25, 3.125, 6.25, 10.625],
[0, 0.625, 2.5, 5.625, 10]]
[
)
] iplot(data)
Bokeh
Sample
import numpy as np
from bokeh.plotting import figure, show
from bokeh.io import output_notebook
# Call once to configure Bokeh to display plots inline in the notebook.
output_notebook()
= 4000
N = np.random.random(size=N) * 100
x = np.random.random(size=N) * 100
y = np.random.random(size=N) * 1.5
radii = ["#%02x%02x%02x" % (r, g, 150) for r, g in zip(np.floor(50+2*x).astype(int), np.floor(30+2*y).astype(int))]
colors
= figure()
p =radii, fill_color=colors, fill_alpha=0.6, line_color=None)
p.circle(x, y, radius show(p)