[英]Plotly: legend is not visible
這是我擁有的 CDF 可視化:
fig_cdf = px.ecdf(df['Timespan'], color_discrete_sequence=['blue'],ecdfnorm='probability', orientation='h')
fig_cdf.add_hline(y=90, line_width=2, line_color="red", name='90%', visible=True)
fig_cdf.add_hline(y=30, line_width=2, line_color="red", name='75%', visible=True)
fig_cdf.update_layout(width=500, height=500)
這里的問題是我希望水平線的名稱可見並顯示為第二和第三個圖例。 為此,我嘗試添加可見 = True。 但是,它似乎不起作用。 怎么了?
這是我對這張圖的第一次體驗,但要將其添加到圖例中,您可以使用散點圖的線條模式。 所以我取了第一張圖中使用的最大 x 軸值,並使用適當的 y 軸值設置了圖例名稱 Average。 本例取自官方參考。
import plotly.express as px
import plotly.graph_objects as go
df = px.data.tips()
fig = px.ecdf(df, x=["total_bill", "tip"])
xmax = max(fig.data[0]['x'])
#print(xmax)
fig.add_trace(go.Scatter(
x=[0,xmax],
y=[0.6,0.6],
mode='lines',
line_color='red',
name='mean',
showlegend=True
))
fig.show()
這是一種方法...
color_discrete_sequence
來識別你想要的顏色我正在使用一些隨機的虛擬數據,你可以用你的數據替換import plotly.express as px
df = pd.DataFrame({'firstline': random.sample(range(1, 500), 20),'myX' : range(20)}) #My dummy data
#Add the two lines to dataframe
df['90%'] = [90] * 20
df['75%'] = [75] * 20
fig = px.line(df,
y = ['firstline', '90%', '75%'], x= 'myX', color_discrete_sequence=["blue", "red", "red"])
fig.update_layout(legend_title_text='Legend Heading') #Update Legend header if you dont like 'variable'
fig.show()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.