2 回答

TA貢獻2012條經(jīng)驗 獲得超12個贊
由于似乎沒有辦法調(diào)整bar_polar
Plotly 中繪圖的起始半徑值,因此最好的選擇是嘗試找到一些解決方法。
誠然,這是一個非常老套的解決方案,但為了確保圖表不太接近原點,我們可以創(chuàng)建一個New_Frequency
列,只需向該Frequency
列添加任意值即可。我選擇了 30。請注意,這樣做會扭曲條形之間的相對距離(如果這是一個問題,則需要以某種方式修改解決方案)。
然后我們將 bar_polar 方法中的參數(shù)設(shè)置r
為這個新列,并修改懸停數(shù)據(jù),使其顯示原始的頻率列。
現(xiàn)在我們可以畫一個帶有文本“Calm”的圓圈,該圓圈覆蓋與 值對應(yīng)的內(nèi)部條形0-2 MPH
。添加形狀時,您需要指定笛卡爾坐標(biāo),盡管這是極坐標(biāo)圖。經(jīng)過一番嘗試和錯誤后,似乎 x=0.5、y=0.5 是 bar_極坐標(biāo)圖的中心,并且從 x0=0.4、y0=0.4、x1=0.6、y1=0.6 出發(fā)的圓覆蓋了內(nèi)部條形。我不知道是否有這方面的文檔。
如果您只是追求圖像,那么希望它看起來不錯。請注意,在圖例中切換不同的風(fēng)速會扭曲繪圖,并且圓圈將不再覆蓋內(nèi)部條形,因此您會失去繪圖的一些交互性。
我已包含以下代碼:
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
df = pd.read_csv("windrose.csv")
## this adds 30 to all frequencies, so 0-2 becomes 30-32
df["New_Frequency"] = df["Frequency"] + 30
fig = px.bar_polar(df, r="New_Frequency", theta="Direction",
color="Strength", template="none",
labels={"Strength": "Wind Speed in MPH"},
hover_data={"Strength":True, "New_Frequency":False, "Frequency":True ,"Direction":True}
)
fig.add_shape(
type="circle",
xref="paper",
yref="paper",
x0=0.4,
y0=0.4,
x1=0.6,
y1=0.6,
line_color="Black",
fillcolor="White",
# text="Calm"
)
fig.add_annotation( # add a text callout with arrow
text="Calm", x=0.5, y=0.5, showarrow=False
)
fig.update_layout(width=800, height=800)
fig.update_layout(legend=dict(
orientation="h",
yanchor="top",
y=-0.1,
xanchor="center",
x=0.5),
polar=dict(radialaxis=dict(showticklabels=False, ticks='', linewidth=0)
)
)
fig.show()

TA貢獻1802條經(jīng)驗 獲得超10個贊
我找到了另一種解決方案,通過在 update_layout 中使用“hole”在極坐標(biāo)條形圖的中間添加一個圓圈。代碼附在下面:
fig2 = px.bar_polar(df, r="Frequency", theta="Direction",
color="Strength", template="none",
labels={"Strength": "Wind Speed <br> in MPH:"
}
)
fig2.add_annotation(
text="Calm<br>" + " " + percent + "%", x=0.5, y=0.5, showarrow=False
)
fig2.add_annotation(
text="Calm Values are for <br> Wind Speed <2.0 MPH <br />", x=0.93, y=-0.13, showarrow=False
)
fig2.update_layout(legend=dict(
orientation="h",
yanchor="middle",
y=-0.1,
xanchor="center",
x=0.5,
font=dict(
size=14)),
polar=dict(hole=0.1, radialaxis=dict(showticklabels=False, ticks='', linewidth=0)
),
margin=dict(t=110),
title=dict(
text=title,
xanchor='center',
yanchor='top'
)
)
輸出:
由于 0-2 MPH 在中間顯示為圓圈,因此在我的數(shù)據(jù)框中,我將所有 0-2 MPH 的值設(shè)置為零,這確保不顯示 0-2 bin。
添加回答
舉報