在談及數據可視化的時候,我們通常都會使用到matplotlylib,pyecharts這些可視化的手段。但是,今天我主要來介紹Plotly這款可視化的庫。大家參考開源項目地址:
https://github.com/plotly/plotly.js這個庫是使用js寫的前端,所以畫出來的圖非常的漂亮,不像matplotlylib畫出來的那麼生硬。plotly提供了Python的支持庫,使用pip直接安裝就可以:在python裡面使用plotly畫圖非常的簡單,我們先來看一個簡單的柱狀圖例子:import plotly.graph_objects as go
animals=['giraffes', 'orangutans', 'monkeys']
fig = go.Figure(data=[
go.Bar(name='SF Zoo', x=animals, y=[20, 14, 23]),
go.Bar(name='LA Zoo', x=animals, y=[12, 18, 29])
])
# Change the bar mode
fig.update_layout(barmode='group')
fig.show()
import plotly.express as px
data_canada = px.data.gapminder().query("country == 'Canada'")
fig = px.bar(data_canada, x='year', y='pop')
fig.show()
import plotly.express as px
data = px.data.gapminder()
data_canada = data[data.country == 'Canada']
fig = px.bar(data_canada, x='year', y='pop',
hover_data=['lifeExp', 'gdpPercap'], color='lifeExp',
labels={'pop':'population of Canada'}, height=400)
fig.show()
import plotly.express as px
fig = px.scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 4, 9, 16])
fig.show()
import plotly.express as px
df = px.data.iris() # px自帶數據集
fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species",
size='petal_length', hover_data=['petal_width'])
fig.show()
折線圖
折線圖可以顯示隨時間(根據常用比例設置)而變化的連續數據,因此非常適用於顯示在相等時間間隔下數據的趨勢。比如我們經常看到的監控數據圖,一般都是折線圖。
import plotly.graph_objects as go
animals = ['giraffes', 'orangutans', 'monkeys']
fig = go.Figure(data=[
go.Scatter(name='SF Zoo', x=animals, y=[20, 14, 23]),
go.Scatter(name='LA Zoo', x=animals, y=[12, 18, 29])
])
fig.show()
import plotly.graph_objects as go
import numpy as np
np.random.seed(1)
N = 100
random_x = np.linspace(0, 1, N)
random_y0 = np.random.randn(N) + 5
random_y1 = np.random.randn(N)
random_y2 = np.random.randn(N) - 5
# Create traces
fig = go.Figure()
fig.add_trace(go.Scatter(x=random_x, y=random_y0,
mode='lines',
name='lines'))
fig.add_trace(go.Scatter(x=random_x, y=random_y1,
mode='lines+markers',
name='lines+markers'))
fig.add_trace(go.Scatter(x=random_x, y=random_y2,
mode='markers', name='markers'))
fig.show()
import plotly.express as px
# This dataframe has 244 lines, but 4 distinct values for `day`
df = px.data.tips()
fig = px.pie(df, values='tip', names='day')
fig.show()
import plotly.express as px# plotly的自帶數據集,類型:DataFrame
df = px.data.gapminder().query("year == 2007").query("continent == 'Europe'")矩形樹圖適合展現具有層級關係的數據,能夠直觀體現同級之間的比較。一個Tree狀結構轉化為平面空間矩形的狀態,就像一張地圖,指引我們發現探索數據背後的故事。
矩形樹圖採用矩形表示層次結構裡的節點,父子節點之間的層次關係用矩形之間的相互嵌套隱喻來表達。從根節點開始,屏幕空間根據相應的子節點數目被分為多個矩形,矩形的面積大小通常對應節點的屬性。每個矩形又按照相應節點的子節點遞歸的進行分割,知道葉子節點為止。
import plotly.express as px
fig = px.treemap(
names = ["Eve","Cain", "Seth", "Enos", "Noam", "Abel", "Awan", "Enoch", "Azura"],
parents = ["", "Eve", "Eve", "Seth", "Seth", "Eve", "Eve", "Awan", "Eve"]
)
fig.show()
import plotly.graph_objects as go
# 修改水平參數即可
fig = go.Figure(go.Bar(
x=[20, 14, 23],
y=['giraffes', 'orangutans', 'monkeys'],
orientation='h'))
fig.show()
箱型圖
箱形圖(Box-plot)又稱為盒式圖或箱線圖,是一種用作顯示一組數據分散情況資料的統計圖。因形狀如箱子而得名。在各種領域也經常被使用,常見於品質管理。它主要用於反映原始數據分布的特徵,還可以進行多組數據分布特徵的比較。
import plotly.express as px
df = px.data.tips()
fig = px.box(df, x="time", y="total_bill")
fig.show()
import plotly.graph_objects as go
fig = go.Figure(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]]
))
fig.show()
import plotly.express as px
data=[[1, 25, 30, 50, 1], [20, 1, 60, 80, 30], [30, 60, 1, 5, 20]]
fig = px.imshow(data,
labels=dict(x="Day of Week", y="Time of Day", color="Productivity"),
x=['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'],
y=['Morning', 'Afternoon', 'Evening']
)
fig.update_xaxes(side="top")
fig.show()
import plotly.express as px
df = px.data.election()
fig = px.scatter_ternary(df, a="Joly", b="Coderre", c="Bergeron")
fig.show()
import plotly.express as px
import pandas as pd
df = pd.DataFrame(dict(
r=[1, 5, 2, 2, 3],
theta=['processing cost','mechanical properties','chemical stability',
'thermal stability', 'device integration']))
fig = px.line_polar(df, r='r', theta='theta', line_close=True)
fig.show()
import plotly.express as px
df = px.data.wind()
fig = px.scatter_polar(df, r="frequency", theta="direction")
fig.show()
import plotly.graph_objects as go
fig = go.Figure(go.Waterfall(
name = "20", orientation = "v",
measure = ["relative", "relative", "total", "relative", "relative", "total"],
x = ["Sales", "Consulting", "Net revenue", "Purchases", "Other expenses", "Profit before tax"],
textposition = "outside",
text = ["+60", "+80", "", "-40", "-20", "Total"],
y = [60, 80, 0, -40, -20, 0],
connector = {"line":{"color":"rgb(63, 63, 63)"}},
))
fig.update_layout(
title = "Profit and loss statement 2020",
showlegend = True
)
fig.show()
漏鬥圖
一般表述轉化率(如營銷客戶轉化),由上而下代表不同層級,轉化率逐級降低並形成漏鬥形狀。
import plotly.express as px
data = dict(
number=[39, 27.4, 20.6, 11, 2],
stage=["Website visit", "Downloads", "Potential customers", "Requested price", "invoice sent"])
fig = px.funnel(data, x='number', y='stage')
fig.show()
氣泡分布圖(含配置底圖)
import plotly.express as px
df = px.data.gapminder().query("year==2007")
fig = px.scatter_geo(df, locations="iso_alpha", color="continent",
hover_name="country", size="pop",
projection="natural earth")
fig.show()