[英]How to plot a vertical line at the x-axis range median position using plotly in Python API?
我正在嘗試繪制一個動態定位的垂直線,以便在進行過濾時,線會相應移動。 例如,使用下面的代碼,我可以繪制一個25K的固定垂直線,它與完整數據集一起作為中位數,但是當數據被過濾到“美洲”時,因為x軸范圍現在是45K,所以不再處於中間位置。
那么如何繪制位於x軸范圍中間位置的垂直線? 謝謝
import pandas as pd
import plotly.graph_objs as go
from plotly.offline import init_notebook_mode, iplot
init_notebook_mode(connected=True)
df = pd.read_csv('https://raw.githubusercontent.com/yankev/test/master/life-expectancy-per-GDP-2007.csv')
americas = df[(df.continent=='Americas')]
europe = df[(df.continent=='Europe')]
trace_comp0 = go.Scatter(
x=americas.gdp_percap,
y=americas.life_exp,
mode='markers',
marker=dict(size=12,
line=dict(width=1),
color="navy"
),
name='Americas',
text=americas.country,
)
trace_comp1 = go.Scatter(
x=europe.gdp_percap,
y=europe.life_exp,
mode='markers',
marker=dict(size=12,
line=dict(width=1),
color="red"
),
name='Europe',
text=europe.country,
)
data_comp = [trace_comp0, trace_comp1]
layout_comp = go.Layout(
title='Life Expectancy v. Per Capita GDP, 2007',
hovermode='closest',
xaxis=dict(
title='GDP per capita (2000 dollars)',
ticklen=5,
zeroline=False,
gridwidth=2,
range=[0, 50_000],
),
yaxis=dict(
title='Life Expectancy (years)',
ticklen=5,
gridwidth=2,
range=[0, 90],
),
shapes=[
{
'type': 'line',
'x0': 25000,
'y0': 0,
'x1': 25000,
'y1': 85,
'line': {
'color': 'black',
'width': 1
}
}
]
)
fig_comp = go.Figure(data=data_comp, layout=layout_comp)
iplot(fig_comp)
您需要向程序添加所謂的callbacks
,以便在數據庫更改時更新整個數字。 然后在x1
和x0
形狀定義的定義中包含mean()
。 但是,這需要您使用破折號 。
借助@ rpanai的答案並使用圖表更新按鈕,開發了以下解決方案。 檢查一下。
import pandas as pd
import plotly.graph_objs as go
from plotly.offline import init_notebook_mode, iplot
init_notebook_mode(connected=True)
df = pd.read_csv('https://raw.githubusercontent.com/yankev/test/master/life-expectancy-per-GDP-2007.csv')
americas = df[(df.continent=='Americas')]
europe = df[(df.continent=='Europe')]
# med_eur = europe["gdp_percap"].median()
# med_ame = americas["gdp_percap"].median()
# med_total=pd.DataFrame(list(europe["gdp_percap"])+list(americas["gdp_percap"])).median()[0]
med_eur = europe["gdp_percap"].max()/2
med_ame = americas["gdp_percap"].max()/2
med_total=25000
trace_median0 = go.Scatter(x=[med_total, med_total],
y=[0,85],
mode="lines",
legendgroup="a",
showlegend=False,
marker=dict(size=12,
line=dict(width=0.8),
color="green"
),
name="Median Total"
)
trace_comp1 = go.Scatter(
x=americas.gdp_percap,
y=americas.life_exp,
mode='markers',
marker=dict(size=12,
line=dict(width=1),
color="navy"
),
name='Americas',
text=americas.country
)
trace_median1 = go.Scatter(x=[med_ame, med_ame],
y=[0,90],
mode="lines",
legendgroup="a",
showlegend=False,
marker=dict(size=12,
line=dict(width=0.8),
color="navy"
),
name="Median Americas",
visible=False
)
trace_comp2 = go.Scatter(
x=europe.gdp_percap,
y=europe.life_exp,
mode='markers',
marker=dict(size=12,
line=dict(width=1),
color="red"
),
name='Europe',
text=europe.country,
)
trace_median2 = go.Scatter(x=[med_eur, med_eur],
y=[0,90],
mode="lines",
legendgroup="b",
showlegend=False,
marker=dict(size=12,
line=dict(width=0.8),
color="red"
),
name="Median Europe",
visible=False
)
data_comp = [trace_comp1,trace_median1]+[trace_comp2,trace_median2]+[trace_median0]
layout_comp = go.Layout(
title='Life Expectancy v. Per Capita GDP, 2007',
hovermode='closest',
xaxis=dict(
title='GDP per capita (2000 dollars)',
ticklen=5,
zeroline=False,
gridwidth=2,
range=[0, 50_000],
),
yaxis=dict(
title='Life Expectancy (years)',
ticklen=5,
gridwidth=2,
range=[0, 90],
),
showlegend=False
)
updatemenus = list([
dict(type="buttons",
active=-1,
buttons=list([
dict(label = 'Total Dataset ',
method = 'update',
args = [{'visible': [True,False,True,False,True]},
{'title': 'Life Expectancy v. Per Capita GDP, 2007'}]),
dict(label = 'Americas',
method = 'update',
args = [{'visible': [True,True, False, False,False]},
{'title': 'Americas'}]),
dict(label = 'Europe',
method = 'update',
args = [{'visible': [False, False,True,True,False]},
{'title': 'Europe'}])
]),
)
])
annotations = list([
dict(text='Trace type:', x=0, y=1.085, yref='paper', align='left', showarrow=False)
])
layout_comp['updatemenus'] = updatemenus
layout_comp['annotations'] = annotations
fig_comp = go.Figure(data=data_comp, layout=layout_comp)
iplot(fig_comp)
這不完全是你問的。 正如我懷疑你可以實現顯示明顯的痕跡的中位數只是沒dash
作為正確地Mike_H指出。 無論如何,如果你想使用只有plotly
解決方案,它會很有用。 所以如果你對這個輸出感到滿意的話
您可以使用以下代碼。 主要區別在於我們使用垂直線而不是形狀的軌跡,我們使用legendgroup
和showlegend
參數
import pandas as pd
import plotly.graph_objs as go
from plotly.offline import init_notebook_mode, iplot
init_notebook_mode(connected=True)
df = pd.read_csv('https://raw.githubusercontent.com/yankev/test/master/life-expectancy-per-GDP-2007.csv')
americas = df[(df.continent=='Americas')]
europe = df[(df.continent=='Europe')]
med_eur = europe["gdp_percap"].median()
med_ame = americas["gdp_percap"].median()
trace_comp0 = go.Scatter(
x=americas.gdp_percap,
y=americas.life_exp,
mode='markers',
marker=dict(size=12,
line=dict(width=1),
color="navy"
),
name='Americas',
text=americas.country,
legendgroup="a",
)
trace_median0 = go.Scatter(x=[med_ame, med_ame],
y=[0,90],
mode="lines",
legendgroup="a",
showlegend=False,
marker=dict(size=12,
line=dict(width=0.8),
color="navy"
),
name="Median Americas",
)
trace_comp1 = go.Scatter(
x=europe.gdp_percap,
y=europe.life_exp,
mode='markers',
marker=dict(size=12,
line=dict(width=1),
color="red"
),
name='Europe',
text=europe.country,
legendgroup="b",
)
trace_median1 = go.Scatter(x=[med_eur, med_eur],
y=[0,90],
mode="lines",
legendgroup="b",
showlegend=False,
marker=dict(size=12,
line=dict(width=0.8),
color="red"
),
name="Median Europe",
)
data_comp = [trace_comp0, trace_median0,
trace_comp1, trace_median1]
layout_comp = go.Layout(
title='Life Expectancy v. Per Capita GDP, 2007',
hovermode='closest',
xaxis=dict(
title='GDP per capita (2000 dollars)',
ticklen=5,
zeroline=False,
gridwidth=2,
range=[0, 50_000],
),
yaxis=dict(
title='Life Expectancy (years)',
ticklen=5,
gridwidth=2,
range=[0, 90],
),
)
fig_comp = go.Figure(data=data_comp, layout=layout_comp)
iplot(fig_comp)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.