用Plotly畫出炫酷的數(shù)據(jù)可視化圖表

pip install 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()

使用起來非常的方便,和matplotlylib畫圖步驟很像。
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自帶數(shù)據(jù)集
fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species",
size='petal_length', hover_data=['petal_width'])
fig.show()

折線圖
折線圖可以顯示隨時(shí)間(根據(jù)常用比例設(shè)置)而變化的連續(xù)數(shù)據(jù),因此非常適用于顯示在相等時(shí)間間隔下數(shù)據(jù)的趨勢(shì)。比如我們經(jīng)??吹降谋O(jiān)控?cái)?shù)據(jù)圖,一般都是折線圖。
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的自帶數(shù)據(jù)集,類型:DataFramedf = px.data.gapminder().query("year == 2007").query("continent == 'Europe'")
df.loc[df['pop'] < 2.e6, 'country'] = 'Other countries' # Represent only large countries
fig = px.pie(df, values='pop', names='country', title='Population of European continent')
fig.show()

矩形樹圖適合展現(xiàn)具有層級(jí)關(guān)系的數(shù)據(jù),能夠直觀體現(xiàn)同級(jí)之間的比較。一個(gè)Tree狀結(jié)構(gòu)轉(zhuǎn)化為平面空間矩形的狀態(tài),就像一張地圖,指引我們發(fā)現(xiàn)探索數(shù)據(jù)背后的故事。
矩形樹圖采用矩形表示層次結(jié)構(gòu)里的節(jié)點(diǎn),父子節(jié)點(diǎn)之間的層次關(guān)系用矩形之間的相互嵌套隱喻來表達(dá)。從根節(jié)點(diǎn)開始,屏幕空間根據(jù)相應(yīng)的子節(jié)點(diǎn)數(shù)目被分為多個(gè)矩形,矩形的面積大小通常對(duì)應(yīng)節(jié)點(diǎn)的屬性。每個(gè)矩形又按照相應(yīng)節(jié)點(diǎn)的子節(jié)點(diǎn)遞歸的進(jìn)行分割,知道葉子節(jié)點(diǎn)為止。
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
# 修改水平參數(shù)即可
fig = go.Figure(go.Bar(
x=[20, 14, 23],
y=['giraffes', 'orangutans', 'monkeys'],
orientation='h'))
fig.show()

箱型圖
箱形圖(Box-plot)又稱為盒式圖或箱線圖,是一種用作顯示一組數(shù)據(jù)分散情況資料的統(tǒng)計(jì)圖。因形狀如箱子而得名。在各種領(lǐng)域也經(jīng)常被使用,常見于品質(zhì)管理。它主要用于反映原始數(shù)據(jù)分布的特征,還可以進(jìn)行多組數(shù)據(jù)分布特征的比較。
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()

漏斗圖
一般表述轉(zhuǎn)化率(如營(yíng)銷客戶轉(zhuǎn)化),由上而下代表不同層級(jí),轉(zhuǎn)化率逐級(jí)降低并形成漏斗形狀。
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()


以上便是對(duì) Plotly 的簡(jiǎn)單介紹。
如果文章對(duì)你有幫助,歡迎轉(zhuǎn)發(fā)/點(diǎn)贊/收藏!
_往期文章推薦_
