簡體   English   中英

Plotly plot 帶多個邊緣

[英]Plotly plot with multiple marginal

我正在用 Plotly 做一些 ECDF 圖。 將示例視為 MWE:

import plotly.express as px
df = px.data.tips()
fig = px.ecdf(df, x="total_bill", color="sex", markers=True, lines=False, marginal="histogram")
fig.show()

這里的邊緣 plot 是直方圖,我們也可以將其更改為marginal='rug' 我想同時擁有它們兩個,比如marginal=['histogram','rug']來產生這個:

在此處輸入圖像描述

可能嗎?

您可以首先創建兩個不同的圖形來保存histogram marginal plotrug marginal plot 但是,如果您想組合它們的軌跡,最簡單的方法可能是使用子圖,這樣您就可以放置地毯 plot 的軌跡,而不會與直方圖 plot 的軌跡重疊。

我不知道如何將兩個圖形的布局合並到子圖中,所以在將軌跡放在子圖中之后,我還必須手動 label 軸。

例如:

from plotly.subplots import make_subplots
import plotly.graph_objects as go
import plotly.express as px
df = px.data.tips()

fig = make_subplots(rows=3, cols=1, row_heights=[0.25, 0.25, 0.5])

fig1 = px.ecdf(df, x="total_bill", color="sex", markers=True, lines=False, marginal="histogram")
fig2 = px.ecdf(df, x="total_bill", color="sex", markers=True, lines=False, marginal="rug")

## rug plot components
fig.add_trace(fig2.data[1],row=1, col=1)
fig.add_trace(fig2.data[3],row=1, col=1)

## histogram components 
fig.add_trace(fig1.data[1], row=2, col=1)
fig.add_trace(fig1.data[3], row=2, col=1)

## cumulative distribution scatter
fig.add_trace(fig1.data[0], row=3, col=1)
fig.add_trace(fig1.data[2], row=3, col=1)

fig['layout']['barmode'] = 'overlay'
fig['layout']['xaxis3']['title'] = 'total_bill'
fig['layout']['yaxis3']['title'] = 'probability'
fig.show()

在此處輸入圖像描述

暫無
暫無

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

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