简体   繁体   English

如何在Python API中使用plotly在x轴范围中间位置绘制垂直线?

[英]How to plot a vertical line at the x-axis range median position using plotly in Python API?

I'm trying to plot a vertical line that's dynamically positioned so that when filtering happens, the line will move accordingly. 我正在尝试绘制一个动态定位的垂直线,以便在进行过滤时,线会相应移动。 For example, with the below code, I can plot a stationary vertical line at 25K which works with the full dataset as the median, but when the data is filtered to "Americas" only since the x-axis range is now 45K, the line is not at the median position anymore. 例如,使用下面的代码,我可以绘制一个25K的固定垂直线,它与完整数据集一起作为中位数,但是当数据被过滤到“美洲”时,因为x轴范围现在是45K,所以不再处于中间位置。

So how can I plot a vertical line that's positioned at the x-axis range's median position? 那么如何绘制位于x轴范围中间位置的垂直线? Thanks 谢谢

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)

在此输入图像描述

You need to add so called callbacks to your program, so that the whole figure is updated when the database changes. 您需要向程序添加所谓的callbacks ,以便在数据库更改时更新整个数字。 Then you include a mean() to the definition of your x1 and x0 shape definitions. 然后在x1x0形状定义的定义中包含mean() However this requires you to use dash . 但是,这需要您使用破折号

With the help of the @rpanai's answer and using plotly update buttons, the following solution is developed. 借助@ rpanai的答案并使用图表更新按钮,开发了以下解决方案。 Check this. 检查一下。

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)

在此输入图像描述

在此输入图像描述

This is not quite exactly what you asked. 这不完全是你问的。 As I doubt you can achieve to show the median of visible traces only without dash as correctly pointed out by Mike_H. 正如我怀疑你可以实现显示明显的痕迹的中位数只是没dash作为正确地Mike_H指出。 Anyway it could be useful if you want to use a plotly only solution. 无论如何,如果你想使用只有plotly解决方案,它会很有用。 So if you are happy with this outputs 所以如果你对这个输出感到满意的话 在此输入图像描述

在此输入图像描述

You can use the following code. 您可以使用以下代码。 Where the main differences is that we use traces for vertical lines instead of shapes and we play with legendgroup and showlegend parameters 主要区别在于我们使用垂直线而不是形状的轨迹,我们使用legendgroupshowlegend参数

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM