Data visualization
# -*- coding: utf-8 -*-
"""Charts Colab
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1dvI0ZL3vntg9DbjaCHB-Y23Ic4ihzr0T
# Charting in Colaboratory
A common use for notebooks is data visualization using charts. Colaboratory makes this easy with several charting tools available as Python imports.
## Matplotlib
[Matplotlib](http://matplotlib.org/) is the most common charting package, see its [documentation](http://matplotlib.org/api/pyplot_api.html) for details, and its [examples](http://matplotlib.org/gallery.html#statistics) for inspiration.
### Line Plots
"""
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5, 6, 7, 8, 9]
y1 = [1, 3, 5, 3, 1, 3, 5, 3, 1]
y2 = [2, 4, 6, 4, 2, 4, 6, 4, 2]
plt.plot(x, y1, label="line L")
plt.plot(x, y2, label="line H")
plt.plot()
plt.xlabel("x axis")
plt.ylabel("y axis")
plt.title("Line Graph Example")
plt.legend()
plt.show()
"""### Bar Plots"""
import matplotlib.pyplot as plt
plt.style.use('default')
plt.rcParams['figure.figsize'] = (6, 5)
plt.rcParams['font.size'] = 12
x = [1, 2, 3]
y = [1, 2, 3]
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)
ax1.bar(x, y, color='aquamarine', edgecolor='black', hatch='/')
ax2.bar(x, y, color='salmon', edgecolor='black', hatch='\\')
ax3.bar(x, y, color='navajowhite', edgecolor='black', hatch='+')
ax4.bar(x, y, color='lightskyblue', edgecolor='black', hatch='*')
plt.tight_layout()
plt.show()
import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse, Polygon
fig = plt.figure()
ax1 = fig.add_subplot(111)
# draw hatch
ax1.bar(range(1, 5), range(1, 5), color='none', edgecolor='red', hatch="/", lw=1., zorder = 0)
# draw edge
ax1.bar(range(1, 5), range(1, 5), color='none', edgecolor='k', zorder=1, lw=2.)
#ax1.set_xticks([1.5, 2.5, 3.5, 4.5])
plt.show()
import pandas as pd
import matplotlib.pyplot as plt
employees=["Rudra","Alok","Prince","Nayan","Reman"]
earnings={
"January":[0,20,15,18,14],
"February":[0,13,10,18,15],
"March":[40,20,10,15,18],
}
df=pd.DataFrame(earnings,index=employees)
df.plot(kind="barh",stacked=True,figsize=(10,8))
plt.legend(loc="lower left",bbox_to_anchor=(0.8,1.0))
fig, ax = plt.subplots(1,1,figsize=(10,5))
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
#ax.spines['left'].set_visible(False)
ax.spines['bottom'].set_visible(False)
plt.show()
import numpy as np
import matplotlib.pyplot as plt
labels = ['Detail of TLSRPT using HTTPS','HTTPS','Detail of TLSRPT using Email','Email', 'Wellformed', 'Normal Response']
mine = [0,10,0,722, 730, 747]
other_1 = [0, 0, 692, 0,0,0]
other_2 = [0, 0, 27, 0,0,0]
others = [0,0,720,0,0,0]
other_3 = [0, 0, 3, 0,0,0]
others2 = [0,0,723,0,0,0]
other_4 = [3, 0, 3, 0,0,0]
HTTPS = [8, 0, 0, 0,0,0]
width = 0.3
fig, ax = plt.subplots(1,1,figsize=(10,5))
ax.xaxis.tick_top()
bar = ax.barh(labels, mine, width, color='none',hatch='xxxxx',edgecolor='k', lw=1, zorder = 0.3)
for rect in bar:
witdh = rect.get_width()
posx = witdh * 1.01
posy = rect.get_y() + rect.get_height() * 0.5 -0.04
if rect.get_width() != 0:
ax.text(posx, posy, ' %d (%.2f%%)' % (witdh,witdh/747*100), rotation=0, ha='left', va='center')
ax.barh(labels, other_1, width, left = mine, label ='Counts of using One-Email Address (Ratio): 693 (92.77%)',color='none',hatch='...',edgecolor='dodgerblue', lw=1, zorder = 0.3)
ax.barh(labels, other_2, width, left = other_1, label ='Counts of using Two-Email Address (Ratio): 27 (3.61%)')
ax.barh(labels, other_3, width, left = others, label ='Counts of using Three-Email Address (Ratio): 1 (0.13%)')
ax.barh(labels, other_4, width, left = others2, label ='Counts of using Email with HTTPS (Ratio): 2 (0.27%)')
ax.barh(labels, HTTPS, width, left = other_4, label ='Counts of using HTTPS (Ratio): 7 (1.07%)')
ax.legend()
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
#ax.spines['left'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.set_title('Analysis the method of TLSRPT')
box = ax.get_position() # 범례를 그래프상자 밖에 그리기위해 상자크기를 조절
ax.set_position([box.x0, box.y0, box.width, box.height])
ax.legend(frameon=False, bbox_to_anchor=(1,0), shadow=True, ncol=1)
plt.show()
import numpy as np
import matplotlib.pyplot as plt
h = [8,2,1,27, 693, 721, 731,747]
h1 = h[:4]
h2 = h[4:]
r = ['8 (1.07%)','2 (0.27%)','1 (0.13%)', '27 (3.61%)', '693 (92.77%)', '721 (96.52%)', '731 (97.86%)','747 (100.00%)']
r1 = r[:4]
r2 = r[4:]
fig, ax = plt.subplots(1,1,figsize=(10,5))
h = np.array(h)
plt.gca().use_sticky_edges = False
plt.barh(r, width=h, left=(h.max() - h) / 2, align='center', color='white',edgecolor='black')
for ri in r2:
plt.text(h.max() /2, ri, ri, ha='center', va='center', color='black', size=10)
for ri in r1:
plt.text(h.max() /2, ri, ri, ha='center', va='center', color='black', size=10)
plt.yticks(np.arange(8),['HTTPS','Email-with-HTTPS','Three email address','Two email address','One email address','Email','Wellformed','Normal Response']) # optionally remove standard y ticks and their labels
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
#ax.spines['left'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.get_xaxis().set_visible(False)
plt.show()
import numpy as np
import matplotlib.pyplot as plt
women_pop = np.array([5., 30., 45., 22.])
men_pop = np.array( [5., 25., 50., 20.])
X = np.arange(4)
plt.barh(X, women_pop, color = 'r')
plt.barh(X, -men_pop, color = 'b')
plt.show()
import numpy as np
def compute_pos(yticks, height, i, models):
index = np.arange(len(yticks))
n = len(models)
correction = i - 0.5*(n-1)
return index + height * correction
import matplotlib.pyplot as plt
models = ['Scanned', 'model B', 'model C']
yticks = ['STARTTLS', 'DNSSEC']
data = {
'Scanned':[0.21, 0],
'model D':[0.65, 895540],
'model B':[0.61, 747],
'model C':[0.55, 894793]
}
height = 0.15
fig, ax = plt. subplots(1,1,figsize=(10,3))
ax.xaxis.tick_top()
ax.set_yticks(range(len(yticks)))
ax.set_yticklabels(yticks, fontsize=10)
pos0 = compute_pos(yticks, height, 0, models)
print(pos0)
bar = ax.barh(pos0, data['model D'], height=height*0.95, label='model D', color='none',hatch='xxxxx',edgecolor='dodgerblue', lw=3., zorder = 1)
present_width(ax, bar) # bar너비 출력
pos1 = compute_pos(yticks, height, 1, models)
bar = ax.barh(pos1, data['model C'], height=height*0.95, label='model D', color='none',hatch='xxxxx',edgecolor='dodgerblue', lw=3., zorder = 1)
present_width(ax, bar) # bar너비 출력
pos2 = compute_pos(yticks, height, 2, models)
bar = ax.barh(pos2, data['model B'], height=height*0.95, label='model D', color='none',hatch='xxxxx',edgecolor='dodgerblue', lw=3., zorder = 1)
present_width(ax, bar) # bar너비 출력
print(pos0,pos1,pos2)
# for i, model in enumerate(models):
# pos = compute_pos(yticks, height, i, models)
# bar = ax.barh(pos, data[model], height=height*0.95, label=model, color='none',hatch='xxxxx',edgecolor='dodgerblue', lw=3., zorder = 1)
# present_width(ax, bar) # bar너비 출력
box = ax.get_position() # 범례를 그래프상자 밖에 그리기위해 상자크기를 조절
ax.set_position([box.x0, box.y0, box.width, box.height])
ax.legend(frameon=False, bbox_to_anchor=(0.65,0), shadow=True, ncol=3)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.set_axisbelow(True)
ax.xaxis.grid(True, color='gray', linestyle=(0, (5, 10)), linewidth=0.5)
import numpy as np
def compute_pos(yticks, height, i, models):
index = np.arange(len(yticks))
n = len(models)
correction = i - 0.5*(n-1)
return index + height * correction
import matplotlib.pyplot as plt
yticks = ['STARTTLS', 'DNSSEC', 'Request: _smtp.tls.domain']
data = {
'model D':[0.65, 1, 1],
'model C':[0.55, 1, 1],
'model B':[0.61, 2, 1]
}
height = 2
fig, ax = plt. subplots(1,1,figsize=(10,2))
ax.xaxis.tick_top()
#ax.xaxis.set_label_position('top')
ax.set_yticks(range(len(yticks)))
ax.set_yticklabels(yticks, fontsize=10)
#pos_2 = compute_pos(yticks, height, 10, 'model D')
bar = ax.barh(5, data['model D'], height=2, label='Count of Scanned Domain', color='none',hatch='xxxxx',edgecolor='dodgerblue', lw=1, zorder = 0.3)
present_width(ax, bar) # bar너비 출력
#pos_1 = compute_pos(yticks, height, 5, 'model C')
bar = ax.barh(3, data['model C'], height=1, label='No Response (Ratio)', color='none',hatch='....',edgecolor='red', lw=1, zorder = 0.3)
present_width(ax, bar) # bar너비 출력
#pos_0 = compute_pos(yticks, height, 0, 'model B')
bar = ax.barh(1, data['model B'], height=0, label='Yes Response (Ratio)', color='none',hatch='xxxxx',edgecolor='green', lw=1., zorder = 0.3)
present_width(ax, bar) # bar너비 출력
box = ax.get_position() # 범례를 그래프상자 밖에 그리기위해 상자크기를 조절
ax.set_position([box.x0, box.y0, box.width , box.height])
ax.legend(frameon=False, bbox_to_anchor=(0.65,0), shadow=True, ncol=3)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.set_axisbelow(True)
ax.xaxis.grid(True, color='gray', linestyle=(0, (5, 10)), linewidth=0.5)
def compute_pos(yticks, height, i, models):
index = np.arange(len(yticks))
n = len(models)
correction = i - 0.2*(n-1)
return index + height * correction
def present_width(ax, bar):
for rect in bar:
witdh = rect.get_width()
posx = witdh*1.01
posy = rect.get_y()+rect.get_height()*0.5
ax.text(posx, posy, '%.3d' % witdh, rotation=0, ha='left', va='center')
import matplotlib.pyplot as plt
yticks = ['STARTTLS', 'DNSSEC', 'Request: _smtp.tls.domain']
data = {
'model D':[0.65, 0.71, 895540],
'model C':[0.55, 0.66, 894793],
'model B':[0.61, 0.65, 747]
}
height = 0.15
fig, ax = plt. subplots(1,1,figsize=(10,5))
ax.xaxis.tick_top()
#ax.xaxis.set_label_position('top')
ax.set_yticks(range(len(yticks)))
ax.set_yticklabels(yticks, fontsize=10)
#for i, model in enumerate(models):
pos_2 = compute_pos(yticks, height, 2, 'model D')
bar = ax.barh(pos_2, data['model D'], height=height*0.65, label='Count of Scanned Domain', color='none',hatch='xxxxx',edgecolor='dodgerblue', lw=1, zorder = 0.3)
present_width(ax, bar) # bar너비 출력
pos_1 = compute_pos(yticks, height, 1, 'model C')
bar = ax.barh(pos_1, data['model C'], height=height*0.65, label='No Response (Ratio)', color='none',hatch='....',edgecolor='red', lw=1, zorder = 0.3)
present_width(ax, bar) # bar너비 출력
pos_0 = compute_pos(yticks, height, 0, 'model B')
bar = ax.barh(pos_0, data['model B'], height=height*0.65, label='Yes Response (Ratio)', color='none',hatch='xxxxx',edgecolor='green', lw=1., zorder = 0.3)
present_width(ax, bar) # bar너비 출력
box = ax.get_position() # 범례를 그래프상자 밖에 그리기위해 상자크기를 조절
ax.set_position([box.x0, box.y0, box.width * 0.9, box.height])
ax.legend(frameon=False, bbox_to_anchor=(1,0), shadow=True, ncol=3)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.set_axisbelow(True)
ax.xaxis.grid(True, color='gray', linestyle=(0, (5, 10)), linewidth=0.5)
import matplotlib.pyplot as plt
import numpy as np
# plt.rcParams['xtick.bottom'] = plt.rcParams['xtick.labelbottom'] = False
# plt.rcParams['xtick.top'] = plt.rcParams['xtick.labeltop'] = True
x = np.arange(10)
fig, ax = plt.subplots()
ax.plot(x)
#ax.set_xlabel('xlabel top') # Note title moves to make room for ticks
secax = ax.secondary_xaxis('top')
secax.set_xlabel('new label top')
plt.show()
import matplotlib.pyplot as plt
import numpy as np
def main():
#### 1. bar plot으로 나타낼 데이터 입력
models = ['model A', 'model B', 'model C', 'model D']
yticks = ['Breast', 'Liver', 'Brain']
data = {'model A':[0.65, 0.71, 0.69],
'model B':[0.61, 0.65, 0.64],
'model C':[0.55, 0.66, 0.60],
'model D':[0.60, 0.62, 0.57]}
#### 2. matplotlib의 figure 및 axis 설정
fig, ax = plt.subplots(1,1,figsize=(7,5)) # 1x1 figure matrix 생성, 가로(7인치)x세로(5인치) 크기지정
colors = ['salmon', 'orange', 'cadetblue', 'skyblue']
height = 0.15
#### 3. bar 그리기
for i, model in enumerate(models):
pos = compute_pos(yticks, height, i, models)
bar = ax.barh(pos, data[model], height=height*0.95, label=model, color='none',hatch='xxxxx',edgecolor='dodgerblue', lw=1., zorder = 0)
bar = ax.barh(pos, data[model], height=height*0.95, label=model, color='none', edgecolor='k', zorder=1, lw=2.)
present_width(ax, bar) # bar너비 출력
#### 4. x축 세부설정
ax.set_xlim([0.5,0.76])
ax.set_xticks([0.5, 0.55, 0.6, 0.65, 0.7, 0.75])
ax.xaxis.set_tick_params(labelsize=10)
# secax = ax.secondary_xaxis('top')
# secax.set_xlabel('new label top')
#ax.set_xlabel('Prediction Accuracy', fontsize=14)
#### 5. y축 세부설정
ax.set_yticks(range(len(yticks)))
ax.set_yticklabels(yticks, fontsize=10)
#ax.set_ylabel('Cancer type', fontsize=14)
#### 6. 범례 나타내기
box = ax.get_position() # 범례를 그래프상자 밖에 그리기위해 상자크기를 조절
ax.set_position([box.x0, box.y0, box.width * 0.9, box.height])
ax.legend(loc='center left', bbox_to_anchor=(1,0.5), shadow=True, ncol=1)
#### 7. 보조선(눈금선) 나타내기
ax.set_axisbelow(True)
ax.xaxis.grid(True, color='gray', linestyle=(0, (5, 10)), linewidth=0.5)
#### 8. 그래프 저장하고 출력하기
plt.savefig('ex_barhplot.png', format='png', dpi=300)
plt.show()
def compute_pos(yticks, height, i, models):
index = np.arange(len(yticks))
n = len(models)
correction = i - 0.5*(n-1)
return index + height * correction
if __name__=='__main__':
main()
import matplotlib.pyplot as plt
# Look at index 4 and 6, which demonstrate overlapping cases.
x1 = [1, 3, 4, 5, 6, 7, 9]
y1 = [4, 7, 2, 4, 7, 8, 3]
x2 = [2, 4, 6, 8, 10]
y2 = [5, 6, 2, 6, 2]
# Colors: https://matplotlib.org/api/colors_api.html
plt.bar(x1, y1, label="Blue Bar", color='b')
plt.bar(x2, y2, label="Green Bar", color='g')
plt.plot()
plt.xlabel("bar number")
plt.ylabel("bar height")
plt.title("Bar Chart Example")
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.
n = 5 + np.random.randn(1000)
m = [m for m in range(len(n))]
plt.bar(m, n)
plt.title("Raw Data")
plt.show()
plt.hist(n, bins=20)
plt.title("Histogram")
plt.show()
plt.hist(n, cumulative=True, bins=20)
plt.title("Cumulative Histogram")
plt.show()
"""### Scatter Plots"""
import matplotlib.pyplot as plt
x1 = [2, 3, 4]
y1 = [5, 5, 5]
x2 = [1, 2, 3, 4, 5]
y2 = [2, 3, 2, 3, 4]
y3 = [6, 8, 7, 8, 7]
# Markers: https://matplotlib.org/api/markers_api.html
plt.scatter(x1, y1)
plt.scatter(x2, y2, marker='v', color='r')
plt.scatter(x2, y3, marker='^', color='m')
plt.title('Scatter Plot Example')
plt.show()
"""### Stack Plots"""
import matplotlib.pyplot as plt
idxes = [ 1, 2, 3, 4, 5, 6, 7, 8, 9]
arr1 = [23, 40, 28, 43, 8, 44, 43, 18, 17]
arr2 = [17, 30, 22, 14, 17, 17, 29, 22, 30]
arr3 = [15, 31, 18, 22, 18, 19, 13, 32, 39]
# Adding legend for stack plots is tricky.
plt.plot([], [], color='r', label = 'D 1')
plt.plot([], [], color='g', label = 'D 2')
plt.plot([], [], color='b', label = 'D 3')
plt.stackplot(idxes, arr1, arr2, arr3, colors= ['r', 'g', 'b'])
plt.title('Stack Plot Example')
plt.legend()
plt.show()
"""### Pie Charts"""
import matplotlib.pyplot as plt
labels = 'S1', 'S2', 'S3'
sections = [56, 66, 24]
colors = ['c', 'g', 'y']
plt.pie(sections, labels=labels, colors=colors,
startangle=90,
explode = (0, 0.1, 0),
autopct = '%1.2f%%')
plt.axis('equal') # Try commenting this out.
plt.title('Pie Chart Example')
plt.show()
"""### fill_between and alpha"""
import matplotlib.pyplot as plt
import numpy as np
ys = 200 + np.random.randn(100)
x = [x for x in range(len(ys))]
plt.plot(x, ys, '-')
plt.fill_between(x, ys, 195, where=(ys > 195), facecolor='g', alpha=0.6)
plt.title("Fills and Alpha Example")
plt.show()
"""### Subplotting using Subplot2grid"""
import matplotlib.pyplot as plt
import numpy as np
def random_plots():
xs = []
ys = []
for i in range(20):
x = i
y = np.random.randint(10)
xs.append(x)
ys.append(y)
aa
return xs, ys
fig = plt.figure()
ax1 = plt.subplot2grid((5, 2), (0, 0), rowspan=1, colspan=2)
ax2 = plt.subplot2grid((5, 2), (1, 0), rowspan=3, colspan=2)
ax3 = plt.subplot2grid((5, 2), (4, 0), rowspan=1, colspan=1)
ax4 = plt.subplot2grid((5, 2), (4, 1), rowspan=1, colspan=1)
x, y = random_plots()
ax1.plot(x, y)
x, y = random_plots()
ax2.plot(x, y)
x, y = random_plots()
ax3.plot(x, y)
x, y = random_plots()
ax4.plot(x, y)
plt.tight_layout()
plt.show()
"""## Plot styles
Colaboratory charts use [Seaborn's](https://seaborn.pydata.org) custom styling by default. To customize styling further please see the [matplotlib docs](https://matplotlib.org/users/style_sheets.html).
## 3D Graphs
### 3D Scatter Plots
"""
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import axes3d
fig = plt.figure()
ax = fig.add_subplot(111, projection = '3d')
x1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y1 = np.random.randint(10, size=10)
z1 = np.random.randint(10, size=10)
x2 = [-1, -2, -3, -4, -5, -6, -7, -8, -9, -10]
y2 = np.random.randint(-10, 0, size=10)
z2 = np.random.randint(10, size=10)
ax.scatter(x1, y1, z1, c='b', marker='o', label='blue')
ax.scatter(x2, y2, z2, c='g', marker='D', label='green')
ax.set_xlabel('x axis')
ax.set_ylabel('y axis')
ax.set_zlabel('z axis')
plt.title("3D Scatter Plot Example")
plt.legend()
plt.tight_layout()
plt.show()
"""### 3D Bar Plots"""
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111, projection = '3d')
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y = np.random.randint(10, size=10)
z = np.zeros(10)
dx = np.ones(10)
dy = np.ones(10)
dz = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
ax.bar3d(x, y, z, dx, dy, dz, color='g')
ax.set_xlabel('x axis')
ax.set_ylabel('y axis')
ax.set_zlabel('z axis')
plt.title("3D Bar Chart Example")
plt.tight_layout()
plt.show()
"""### Wireframe Plots"""
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111, projection = '3d')
x, y, z = axes3d.get_test_data()
ax.plot_wireframe(x, y, z, rstride = 2, cstride = 2)
plt.title("Wireframe Plot Example")
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](http://seaborn.pydata.org):
"""
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
# Generate some random data
num_points = 20
# x will be 5, 6, 7... but also twiddled randomly
x = 5 + np.arange(num_points) + np.random.randn(num_points)
# y will be 10, 11, 12... but twiddled even more randomly
y = 10 + np.arange(num_points) + 5 * np.random.randn(num_points)
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](http://seaborn.pydata.org/generated/seaborn.regplot.html#seaborn.regplot).
Here's a Seaborn [heatmap](https://seaborn.pydata.org/generated/seaborn.heatmap.html):
"""
import matplotlib.pyplot as plt
import numpy as np
# Make a 10 x 10 heatmap of some random data
side_length = 10
# Start with a 10 x 10 matrix with values randomized around 5
data = 5 + np.random.randn(side_length, side_length)
# The next two lines make the values larger as we get closer to (9, 9)
data += np.arange(side_length)
data += np.reshape(np.arange(side_length), (side_length, 1))
# Generate the heatmap
sns.heatmap(data)
plt.show()
"""## Altair
[Altair](http://altair-viz.github.io) 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
cars = data.cars()
alt.Chart(cars).mark_point().encode(
x='Horsepower',
y='Miles_per_Gallon',
color='Origin',
).interactive()
"""For more examples of Altair plots, see the [Altair snippets notebook](/notebooks/snippets/altair.ipynb) or the external [Altair Example Gallery](https://altair-viz.github.io/gallery/).
## Plotly
### Sample
"""
from plotly.offline import iplot
import plotly.graph_objs as go
data = [
go.Contour(
z=[[10, 10.625, 12.5, 15.625, 20],
[5.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()
N = 4000
x = np.random.random(size=N) * 100
y = np.random.random(size=N) * 100
radii = np.random.random(size=N) * 1.5
colors = ["#%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))]
p = figure()
p.circle(x, y, radius=radii, fill_color=colors, fill_alpha=0.6, line_color=None)
p.show()