簡體   English   中英

如何在圖 Plotly、Python 的一側獲得分布?

[英]How to get distribution on side of graph Plotly, Python?

所以我正在繪制一些時間序列數據的模擬,我希望能夠可視化 Python 中結束值的分布。 給定模擬路徑的 plot,我希望能夠獲取最后的值(時間 = T)並使用顯示在圖表右側的分布圖(只是線,而不是框)顯示偏斜。 我在這里提供了一個圖像示例: 在此處輸入圖像描述

我正在使用 plotly 生成圖表,因為我希望能夠在每個時間點獲得 hover 數據。 我使用的數據是通用的,因此任何時間范圍內的任何形式的模擬路徑都有效。 我當前的圖形代碼如下:

import plotly.express as px
import plotly.graph_objects as go
from plotly.graph_objs.scatter.marker import Line

fig = go.Figure(go.Scatter(
x=y_testing.index,
y=y_testing.iloc[:, 0]
))

for i in range(1, len(y_testing.columns)-1):
    fig.add_trace(go.Scatter(
        x=y_testing.index,
        y=y_testing.iloc[:, i]
    ))

fig.show()
  • y_testing.index 是時間段
  • y_testing.iloc[:, 0] 是實際數據
  • y_testing.iloc[:, i] 是各自的模擬路徑

您可以使用子圖並將單面小提琴 plot 放在go.Scatter軌跡旁邊。 為了使該圖更易於閱讀,我添加了輔助y 軸,以便小提琴 plot 的 y 軸刻度線不在散點圖和小提琴 plot 之間,並且我減小了兩個圖之間的間距。

一個有用的提示是,在 plotly 中,默認的 y 軸范圍是[min-range/16, max+range/16] ,我曾經手動設置小提琴 plot 的范圍,使其與散點匹配。

import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots

from plotly.graph_objs.scatter.marker import Line
import numpy as np

## use a sample timeseries data set
df = px.data.stocks()

y_testing = df.set_index('date')
y_testing.drop(columns=['AAPL', 'AMZN', 'FB', 'NFLX', 'MSFT'], inplace=True)

np.random.seed(42)
for i in range(20):
    y_testing[f'GOOG_{i}'] = y_testing['GOOG'] + np.random.normal(loc=0.0, scale=0.05, size=len(y_testing))


fig = make_subplots(rows=1, cols=2, column_widths=[0.8, 0.2], horizontal_spacing=0.01, specs=[[{"secondary_y": False}, {"secondary_y": True}]])

fig.add_trace(go.Scatter(
    x=y_testing.index,
    y=y_testing.iloc[:, 0]
    ),row=1, col=1
)

for i in range(1, len(y_testing.columns)-1):
    fig.add_trace(go.Scatter(
        x=y_testing.index,
        y=y_testing.iloc[:, i]
    ),row=1, col=1
)

fig.add_trace(go.Violin(
        y=y_testing.iloc[-1],
        side='positive'
    ),row=1, col=2, secondary_y=True,
)

## determine yaxis range for the scatter
y_testing_min = y_testing.min().min()
y_testing_max = y_testing.max().max()
y_testing_range = y_testing_max - y_testing_min
y_range = [y_testing_min - y_testing_range/16, y_testing_max + y_testing_range/16]
fig.update_yaxes(range=y_range, secondary_y=True)

fig.show()

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM