簡體   English   中英

Plotly 圖形對象設置可視大小

[英]Plotly Graph Objects set viewable size

我正在嘗試在 ploty 應用程序中繪制一些正方形。 我意識到 ploty 有時會根據其中的對象決定可視區域的大小(這是有道理的),但有時它只是固定的,而不管繪制的是什么。 我想覆蓋它,這樣我就可以設置一些形狀而不會使它們變形。

我找到了 fig.update_layout但它似乎更新了實際的“畫布”大小,但沒有更新內部顯示內容的尺寸,也沒有更新邊界

在我的示例中,我有兩個矩形,它們的角之間的距離相同,因此它們看起來應該像正方形; 我還想將頂角設置得更遠(這意味着里面的方塊看起來會更小

這是我的簡化示例:

代碼

import plotly.graph_objects as go

fig = go.Figure()

fig.update_layout(
    autosize=False,
    width=500,
    height=500,
    paper_bgcolor='rgba(0,0,0,0)',
    plot_bgcolor='rgba(0,0,0,0)'
    # I'd like to set the corner to be 10,10
)

# Set axes properties
fig.update_xaxes(showgrid = False, zeroline = False, visible = False)
fig.update_yaxes(showgrid = False, zeroline = False, visible = False)

fig.add_shape(type="rect",
    x0=0, y0=0, x1=4, y1=4, # this should look like a big square going a little before the middle of the screen
    line=dict(color="Red"),
)
fig.add_shape(type="rect",
    x0=1, y0=1, x1=2, y1=2, # this should look like a small square inside the first one
    line=dict(color="Red"),
)

fig.show()

獲得的結果

在此處輸入圖像描述

預期結果

在此處輸入圖像描述

有機會回應,我會舉一些例子來說明軸的局限性。 首先是設置比例錨。 參考

fig.update_layout(yaxis=dict(scaleanchor='x'))

在此處輸入圖像描述

上面的例子等價於下面的代碼。 fig.update_layout(yaxis=dict(scaleanchor='x', scaleratio=1.0))可以通過改變比例來改變縱橫比。

fig.update_layout(yaxis=dict(scaleanchor='x', scaleratio=2.0))

在此處輸入圖像描述

我評論說,在問題中的圖形示例中,我認為在軸的附加限制下繪制圖形會更容易。

fig.update_xaxes(range=[0,4], showgrid=False, zeroline=False, visible=False)
fig.update_yaxes(range=[0,4], showgrid=False, zeroline=False, visible=False)

在此處輸入圖像描述

暫無
暫無

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

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