掌握這十個Python Plotly 技巧,輕松打造高顏值數(shù)據(jù)圖表
數(shù)據(jù)可視化在數(shù)據(jù)分析和探索中扮演著不可或缺的角色。它不僅僅是將數(shù)據(jù)以圖形形式呈現(xiàn),而是通過視覺手段揭示隱藏在數(shù)據(jù)背后的故事和洞察。無論是發(fā)現(xiàn)模式、趨勢,還是理解變量之間的關(guān)系,優(yōu)秀的數(shù)據(jù)可視化都能幫助我們更直觀地理解復(fù)雜的信息。
Plotly 是一個廣受歡迎且功能強(qiáng)大的 Python 庫,因其豐富的交互式和動態(tài)圖表功能而備受推崇。無論是基本的線圖和柱狀圖,還是更復(fù)雜的地理空間可視化和三維圖表,Plotly 都能輕松應(yīng)對。其靈活性和可定制性使得數(shù)據(jù)科學(xué)家、分析師和開發(fā)者能夠創(chuàng)建既美觀又實(shí)用的可視化。
接下來將分享一些 Plotly 代碼示例,幫助理解Plotly 的使用方式和特點(diǎn)。
安裝Plotly
pip install plotly
圖片
一、基本折線圖
這個示例使用 Plotly 創(chuàng)建一個簡單的折線圖。我們使用 NumPy 生成樣本數(shù)據(jù),并使用 Plotly 的 go.Scatter 來創(chuàng)建圖表。
import plotly.graph_objects as go
import numpy as np
x = np.linspace(0, 10, 100)
y = np.sin(x)
fig = go.Figure(data=go.Scatter(x=x, y=y, mode='lines'))
fig.update_layout(title='Basic Line Plot', xaxis_title='X-axis', yaxis_title='Y-axis')
fig.show()
圖片
二、彩色散點(diǎn)圖
在這個示例中,我們使用 Plotly Express 創(chuàng)建了一個帶有顏色漸變的散點(diǎn)圖。通過大小和顏色參數(shù)展示了第三維度的信息:
import plotly.express as px
import pandas as pd
np.random.seed(42)
df = pd.DataFrame({'X': np.random.rand(50), 'Y': np.random.rand(50),
'Size': np.random.rand(50) * 30})
fig = px.scatter(df, x='X', y='Y', size='Size', color='Size',
title='Scatter Plot with Color Gradient')
fig.show()
圖片
三、3D 表面圖
3D 表面圖顯示了三個變量在三維空間中的關(guān)系。數(shù)據(jù)點(diǎn)被映射到三維坐標(biāo)系統(tǒng)中的一個表面上,通過表面的形狀、高度或顏色展示特征和趨勢。
import plotly.graph_objects as go
import numpy as np
x = np.linspace(-5, 5, 100)
y = np.linspace(-5, 5, 100)
x, y = np.meshgrid(x, y)
z = np.sin(np.sqrt(x**2 + y**2))
fig = go.Figure(data=[go.Surface(z=z, x=x, y=y)])
fig.update_layout(title='3D Surface Plot',
scene=dict(xaxis_title='X-axis', yaxis_title='Y-axis', zaxis_title='Z-axis'))
fig.show()
圖片
四、氣泡圖
氣泡圖是散點(diǎn)圖的一種變體,其中每個點(diǎn)的大小反映了數(shù)據(jù)的第三維度。創(chuàng)建氣泡圖的示例代碼:
import plotly.express as px
df = px.data.gapminder().query("year == 2007")
fig = px.scatter_geo(df, locatinotallow='iso_alpha', size='pop',
hover_name='country', title='Bubble Map')
fig.show()
五、小提琴圖
小提琴圖是一種繪制數(shù)值數(shù)據(jù)的方法。它類似于箱線圖,但每側(cè)都有一個旋轉(zhuǎn)的核密度圖。創(chuàng)建小提琴圖的示例代碼:
import plotly.express as px
import seaborn as sns
tips = sns.load_dataset( 'tips' )
fig = px.violin(tips, y= 'total_bill' , x= 'day' , box= True ,
points= "all" , title= 'Violin Plot' )
fig.show()
圖片
六、旭日圖
旭日圖用于顯示分層數(shù)據(jù)。這種圖表通常是二維的,但也可以是三維的。旭日圖建立在一個圓圈上,層次結(jié)構(gòu)的每個級別都顯示為一個環(huán)。外環(huán)顯示匯總數(shù)據(jù),內(nèi)環(huán)顯示子類別信息。將鼠標(biāo)懸停在內(nèi)環(huán)上可顯示其包含的所有子集。以下是一個例子:
import plotly.express as px
df = px.data.tips()
fig = px.sunburst(df, path=[ 'sex' , 'day' , 'time' ], values= 'total_bill' ,
title= 'Sunburst Chart' )
fig.show()
圖片
七、熱圖
熱圖,也稱為相關(guān)矩陣,使用顏色編碼的方塊顯示變量之間的相關(guān)性。顏色強(qiáng)度表示相關(guān)性的強(qiáng)度。以下是一個例子:
import plotly.express as px
import numpy as np
np.random.seed( 42 )
corr_matrix = np.random.rand( 10 , 10 )
fig = px.imshow(corr_matrix,
labels= dict (x= "X-axis" , y= "Y-axis" , color= "Correlation" ),
title= 'Heatmap with Annotations' )
fig.show()
圖片
八、雷達(dá)圖
雷達(dá)圖顯示多維數(shù)據(jù)。它將多個數(shù)據(jù)點(diǎn)映射到從同一中心點(diǎn)開始到周長結(jié)束的軸上。連接這些點(diǎn)形成雷達(dá)圖。以下是一個例子:
import plotly.graph_objects as go
import pandas as pd
categories = [ '速度' , '可靠性' , '舒適度' , '安全性' , '效率' ]
values = [ 90 , 60 , 85 , 70 , 80 ]
fig = go.Figure()
fig.add_trace(go.Scatterpolar(
r=values, theta=categories,
fill= 'toself' , name= '產(chǎn)品 A'
))
fig.update_layout(title= '雷達(dá)圖' )
fig.show()
圖片
九、3D 散點(diǎn)圖
3D 散點(diǎn)圖顯示三維數(shù)據(jù)點(diǎn)。每個點(diǎn)都繪制在具有 X、Y 和 Z 軸的 3D 坐標(biāo)系中。以下是一個例子:
import plotly.graph_objects as go
import numpy as np
np.random.seed( 42 )
x = np.random.rand( 100 )
y = np.random.rand( 100 )
z = np.random.rand( 100 )
fig = go.Figure(data=[go.Scatter3d(x=x, y=y, z=z, mode= 'markers' , marker= dict (size= 8 , color=z, colorscale= 'Viridis' ))])
fig.update_layout(title= '3D Scatter Plot' , scene= dict (xaxis_title= 'X-axis' , yaxis_title= 'Y-axis' , zaxis_title= 'Z-axis' ))
fig.show()
圖片
十、漏斗圖
漏斗圖用于單流程分析,展示多個階段的業(yè)務(wù)流程,使用梯形面積將每個階段的業(yè)務(wù)數(shù)據(jù)與上一個階段的業(yè)務(wù)數(shù)據(jù)進(jìn)行比較。以下是一個例子:
import plotly.graph_objects as go
values = [ 500 , 450 , 350 , 300 , 200 ]
fig = go.Figure(go.Funnel(y=[ 'Stage 1' , 'Stage 2' , 'Stage 3' , 'Stage 4' , 'Stage 5' ], x=values, textinfo= 'value+percent initial' ))
fig.update_layout(title= 'Funnel Chart' )
fig.show()
圖片
Plotly 是一個功能強(qiáng)大、用途廣泛的 Python 數(shù)據(jù)可視化庫。本文展示了各種基本示例,演示了不同的繪圖類型和交互功能。通過這些代碼示例來探索 Plotly 的功能并提高您的數(shù)據(jù)可視化技能。