第 11 屆 iThome 鐵人賽

DAY 11
AI & Data

Hands on Data Cleaning and Scraping 資料清理與爬蟲實作系列 第 11

Day11 Data Visualization Tools: Plotly 視覺化資料工具:Plotly

Plotly was built using Python and the Django framework, with a front end using JavaScript and the visualization library D3.js, HTML and CSS. Plotly is a graphing library for making interactive graphs.


Import Data (in this example is a csv hosted on github) and display it in a table and a basic plot using Plotly.

from plotly.offline import iplot
import plotly.figure_factory as ff
import pandas as pd

df = pd.read_csv("")

table = ff.create_table(df)
iplot(table, filename='jupyter-table1')

import plotly.graph_objects as go

schools = df.School
data = [go.Bar(x=df.School,

iplot(data, filename='jupyter-basic_bar')

繪製3D地表地形圖 Topographical 3D Surface Plot

We can make sophisticated interactive 3D plots using Numpy and Plotly.

import plotly.graph_objects as go
import pandas as pd

# Read data from a csv
z_data = pd.read_csv('')

fig = go.Figure(data=[go.Surface(z=z_data.values)])

fig.update_layout(title='Mt Bruno Elevation', autosize=False,
                  width=500, height=500,
                  margin=dict(l=65, r=50, b=65, t=90))

功能按鈕 Plot Controls & IPython widgets

We can also add plot controls and IPython widgets through Plotly, such as sliders or buttons.

import numpy as np

data = [dict(
        visible = False,
        line=dict(color='#00CED1', width=6),
        name = '? = '+str(step),
        x = np.arange(0,10,0.01),
        y = np.sin(step*np.arange(0,10,0.01))) for step in np.arange(0,5,0.1)]
data[10]['visible'] = True

steps = []
for i in range(len(data)):
    step = dict(
        method = 'restyle',
        args = ['visible', [False] * len(data)],
    step['args'][1][i] = True # Toggle i'th trace to "visible"

sliders = [dict(
    active = 10,
    currentvalue = {"prefix": "Frequency: "},
    pad = {"t": 50},
    steps = steps

layout = dict(sliders=sliders)
fig = dict(data=data, layout=layout)

iplot(fig, filename='Sine Wave Slider')

其他物件嵌入 Additional Embed Features

from IPython.display import YouTubeVideo # 嵌入youtube影片 embed youtube video

本篇程式碼請參考Github。The code is available on Github.

Please let me know if there’s any mistake in this article. Thanks for reading.

Reference 參考資料:

[1] 第二屆機器學習百日馬拉松內容

[2] Visualization

[3] Plotly Python Open Source Graphing Library

[4] 給工程師的統計學及資料分析

[5] Jupyter Notebook Tutorial in Python

[6] Plotly

Day10 Data Visualization Tools: Matplotlib 視覺化資料工具:Matplotlib
Day12 Data Visualization Tools: Seaborn 視覺化資料工具:Seaborn
Hands on Data Cleaning and Scraping 資料清理與爬蟲實作30