欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python-Plotly庫(kù)實(shí)現(xiàn)數(shù)據(jù)動(dòng)態(tài)可視化的示例代碼

 更新時(shí)間:2025年04月28日 10:38:24   作者:啊阿貍不會(huì)拉桿  
Plotly是一個(gè)強(qiáng)大的Python可視化庫(kù),支持創(chuàng)建高質(zhì)量的靜態(tài)、動(dòng)態(tài)和交互式圖表,本文主要介紹了Python-Plotly庫(kù)實(shí)現(xiàn)數(shù)據(jù)動(dòng)態(tài)可視化的示例代碼,具有一定的參考價(jià)值,感興趣的可以了解一下

Plotly是一個(gè)強(qiáng)大的Python可視化庫(kù),支持創(chuàng)建高質(zhì)量的靜態(tài)、動(dòng)態(tài)和交互式圖表。它特別擅長(zhǎng)于繪制三維圖形,能夠直觀地展示復(fù)雜的數(shù)據(jù)關(guān)系。本文將介紹如何使用Plotly庫(kù)實(shí)現(xiàn)函數(shù)的二維和三維可視化,并提供一些優(yōu)美的三維函數(shù)示例。資源綁定附上完整資源供讀者參考學(xué)習(xí)!

一、庫(kù)的介紹

Plotly提供了兩種主要的API:

  • Plotly Express:簡(jiǎn)化的API,適合快速創(chuàng)建常見(jiàn)圖表。

  • Plotly Graph Objects:功能更強(qiáng)大和靈活,適用于復(fù)雜的自定義圖表。

安裝Plotly庫(kù):

pip install plotly

二、常見(jiàn)函數(shù)示例

1. 二維函數(shù)示例

示例1:正弦函數(shù)

import plotly.graph_objects as go
import numpy as np

x = np.linspace(0, 2 * np.pi, 100)
y = np.sin(x)

fig = go.Figure(data=go.Scatter(x=x, y=y, mode='lines'))
fig.update_layout(title='正弦函數(shù)', xaxis_title='x', yaxis_title='y')
fig.show()

效果展示

示例2:拋物線函數(shù)

import plotly.graph_objects as go
import numpy as np

y = np.linspace(-10, 10, 100)
x= y**2

fig = go.Figure(data=go.Scatter(x=x, y=y, mode='lines'))
fig.update_traces(line=dict(color='red'))
fig.update_layout(title='拋物線函數(shù)', xaxis_title='x', yaxis_title='y')
fig.show()

效果展示

2. 三維函數(shù)示例

示例1:三維曲面

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='三維曲面', scene=dict(xaxis_title='x', yaxis_title='y', zaxis_title='z'))
fig.show()

效果展示

示例2:三維曲線

import plotly.graph_objects as go
import numpy as np
theta = np.linspace(-4 * np.pi, 4 * np.pi, 100)
z = np.linspace(-2, 2, 100)
x = z * np.sin(theta)
y = z * np.cos(theta)

fig = go.Figure(data=go.Scatter3d(x=x, y=y, z=z, mode='lines'))
fig.update_traces(line=dict(color='orange', width=2))
fig.update_layout(title='三維曲線', scene=dict(xaxis_title='x', yaxis_title='y', zaxis_title='z'))
fig.show()

效果展示

三、優(yōu)美的三維函數(shù)示例

示例1:球面函數(shù)

import plotly.graph_objects as go
import numpy as np

u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(0, np.pi, 100)
x = np.outer(np.cos(u), np.sin(v))
y = np.outer(np.sin(u), np.sin(v))
z = np.outer(np.ones(np.size(u)), np.cos(v))

fig = go.Figure(data=go.Surface(z=z, x=x, y=y, colorscale='Reds'))
fig.update_layout(title='球面', scene=dict(xaxis_title='x', yaxis_title='y', zaxis_title='z'))
fig.show()

效果展示

示例2:莫比烏斯帶

import plotly.graph_objects as go
import numpy as np

u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(-0.5, 0.5, 100)
u, v = np.meshgrid(u, v)
x = (1 + v * np.cos(u / 2)) * np.cos(u)
y = (1 + v * np.cos(u / 2)) * np.sin(u)
z = v * np.sin(u / 2)

fig = go.Figure(data=go.Surface(z=z, x=x, y=y, colorscale='rainbow'))
fig.update_layout(title='莫比烏斯帶', scene=dict(xaxis_title='x', yaxis_title='y', zaxis_title='z'))
fig.show()

效果展示

四、動(dòng)態(tài)和交互式圖表示例

示例1:交互式按鈕

import plotly.graph_objects as go

fig = go.Figure()

# 添加多條折線
fig.add_trace(go.Scatter(y=[2, 1, 3], name="Line 1"))
fig.add_trace(go.Scatter(y=[3, 2, 1], name="Line 2"))

# 配置交互按鈕
fig.update_layout(
    updatemenus=[
        dict(
            type="buttons",
            direction="right",
            buttons=[
                dict(label="Show All", method="update", args=[{"visible": [True, True]}]),
                dict(label="Show Line 1", method="update", args=[{"visible": [True, False]}]),
                dict(label="Show Line 2", method="update", args=[{"visible": [False, True]}]),
            ],
        )
    ]
)

fig.show()

效果展示

示例2:WebGL加速的大數(shù)據(jù)渲染

import plotly.express as px
import numpy as np

# 模擬大數(shù)據(jù)
np.random.seed(42)
x = np.random.rand(100_000)
y = np.random.rand(100_000)

# 使用WebGL加速的散點(diǎn)圖
fig = px.scatter(x=x, y=y, render_mode='webgl', title="WebGL Accelerated Scatter Plot")
fig.show()

效果展示

示例3:實(shí)時(shí)疫情數(shù)據(jù)儀表盤

from dash import Dash, dcc, html, Input, Output
import plotly.graph_objects as go
import pandas as pd

# 模擬疫情數(shù)據(jù)
data = {
    "date": pd.date_range(start="2020-01-01", periods=100),
    "cases": [i ** 2 for i in range(100)],
    "deaths": [i * 5 for i in range(100)]
}
df = pd.DataFrame(data)

app = Dash(__name__)

app.layout = html.Div([
    dcc.Graph(id="graph"),
    dcc.Slider(
        id="year-slider",
        min=0,
        max=len(df) - 1,
        value=0,
        marks={i: str(df["date"][i].date()) for i in range(0, len(df), 10)},
        step=None
    )
])


@app.callback(Output("graph", "figure"), [Input("year-slider", "value")])
def update_graph(selected_index):
    filtered_df = df.iloc[:selected_index + 1]

    # 使用go.Scatter同時(shí)顯示折線和數(shù)據(jù)點(diǎn)
    fig = go.Figure()
    for col in ["cases", "deaths"]:
        fig.add_trace(go.Scatter(
            x=filtered_df["date"],
            y=filtered_df[col],
            mode='lines+markers',
            marker=dict(size=8, color='red'),
            name=col
        ))

    fig.update_layout(title="Covid-19 Cases Over Time", xaxis_title="date", yaxis_title="value")
    return fig


if __name__ == "__main__":
    app.run(debug=True)

效果展示

五、Plotly與Matplotlib的比較

Plotly和Matplotlib都是Python中廣泛使用的可視化庫(kù),但它們?cè)谀承┓矫嬗兴煌?/p>

特性PlotlyMatplotlib
交互性生成交互式圖表,支持縮放、懸停生成靜態(tài)圖表
易用性API較為復(fù)雜,但提供了豐富的功能API簡(jiǎn)單直觀,適合初學(xué)者
適用場(chǎng)景適合創(chuàng)建動(dòng)態(tài)和交互式圖表,如Web應(yīng)用適合創(chuàng)建靜態(tài)圖表,如科學(xué)論文
學(xué)習(xí)曲線較陡峭,需要時(shí)間熟悉API較平緩,易于上手

通過(guò)這些比較,可以看出Plotly和Matplotlib各有優(yōu)缺點(diǎn),選擇哪個(gè)庫(kù)取決于你的具體需求。

六、函數(shù)參數(shù)用法總結(jié)

以下是Plotly中常用函數(shù)的參數(shù)用法總結(jié):

函數(shù)參數(shù)描述
Scatterx, y繪制二維散點(diǎn)圖或折線圖
Scatter3dx, y, z繪制三維散點(diǎn)圖或曲線
Surfacex, y, z繪制三維曲面圖
Bar3dx, y, z, dx, dy, dz繪制三維條形圖
updatemenusbuttons, direction, type配置交互按鈕
render_modewebgl使用WebGL加速渲染

通過(guò)這些函數(shù),你可以輕松實(shí)現(xiàn)各種數(shù)學(xué)函數(shù)的可視化,為數(shù)據(jù)分析和建模提供強(qiáng)大的工具。

到此這篇關(guān)于Python-Plotly庫(kù)實(shí)現(xiàn)數(shù)據(jù)動(dòng)態(tài)可視化的示例代碼的文章就介紹到這了,更多相關(guān)Python Plotly庫(kù)數(shù)據(jù)動(dòng)態(tài)可視化內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python實(shí)現(xiàn)希爾排序算法的原理與用法實(shí)例分析

    Python實(shí)現(xiàn)希爾排序算法的原理與用法實(shí)例分析

    這篇文章主要介紹了Python實(shí)現(xiàn)希爾排序算法,簡(jiǎn)單講述了希爾排序的原理并結(jié)合具體實(shí)例形式分析了Python希爾排序的具體實(shí)現(xiàn)方法與使用技巧,需要的朋友可以參考下
    2017-11-11
  • python如何判斷IP地址合法性

    python如何判斷IP地址合法性

    這篇文章主要為大家詳細(xì)介紹了python如何判斷IP地址合法性,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • 最新評(píng)論