<small id='AJmlF'></small><noframes id='AJmlF'>

      <i id='AJmlF'><tr id='AJmlF'><dt id='AJmlF'><q id='AJmlF'><span id='AJmlF'><b id='AJmlF'><form id='AJmlF'><ins id='AJmlF'></ins><ul id='AJmlF'></ul><sub id='AJmlF'></sub></form><legend id='AJmlF'></legend><bdo id='AJmlF'><pre id='AJmlF'><center id='AJmlF'></center></pre></bdo></b><th id='AJmlF'></th></span></q></dt></tr></i><div id='AJmlF'><tfoot id='AJmlF'></tfoot><dl id='AJmlF'><fieldset id='AJmlF'></fieldset></dl></div>
        <bdo id='AJmlF'></bdo><ul id='AJmlF'></ul>

    1. <tfoot id='AJmlF'></tfoot>
    2. <legend id='AJmlF'><style id='AJmlF'><dir id='AJmlF'><q id='AJmlF'></q></dir></style></legend>

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

        How to plot a vertical line at the x-axis range median position using plotly in Python API?(如何在 Python API 中使用 plotly 在 x 轴范围中位数位置绘制垂直线?)

        <i id='nbZwh'><tr id='nbZwh'><dt id='nbZwh'><q id='nbZwh'><span id='nbZwh'><b id='nbZwh'><form id='nbZwh'><ins id='nbZwh'></ins><ul id='nbZwh'></ul><sub id='nbZwh'></sub></form><legend id='nbZwh'></legend><bdo id='nbZwh'><pre id='nbZwh'><center id='nbZwh'></center></pre></bdo></b><th id='nbZwh'></th></span></q></dt></tr></i><div id='nbZwh'><tfoot id='nbZwh'></tfoot><dl id='nbZwh'><fieldset id='nbZwh'></fieldset></dl></div>

            <tbody id='nbZwh'></tbody>

        1. <small id='nbZwh'></small><noframes id='nbZwh'>

          <legend id='nbZwh'><style id='nbZwh'><dir id='nbZwh'><q id='nbZwh'></q></dir></style></legend>

              <tfoot id='nbZwh'></tfoot>
                  <bdo id='nbZwh'></bdo><ul id='nbZwh'></ul>
                  本文介绍了如何在 Python API 中使用 plotly 在 x 轴范围中位数位置绘制垂直线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试绘制一条动态定位的垂直线,以便在进行过滤时,该线会相应移动.例如,使用下面的代码,我可以在 25K 处绘制一条固定的垂直线,它使用完整的数据集作为中位数,但是当数据被过滤到美洲"时,因为 x 轴范围现在是 45K,这条线不再处于中间位置.

                  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.

                  那么我如何绘制一条位于 x 轴范围中间位置的垂直线?谢谢

                  So how can I plot a vertical line that's positioned at the x-axis range's median position? 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)
                  

                  推荐答案

                  在@rpanai 的回答和使用情节更新按钮的帮助下,开发了以下解决方案.检查一下.

                  With the help of the @rpanai's answer and using plotly update buttons, the following solution is developed. 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)
                  

                  这篇关于如何在 Python API 中使用 plotly 在 x 轴范围中位数位置绘制垂直线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Adding config modes to Plotly.Py offline - modebar(将配置模式添加到 Plotly.Py 离线 - 模式栏)
                  Plotly: How to style a plotly figure so that it doesn#39;t display gaps for missing dates?(Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙?)
                  python save plotly plot to local file and insert into html(python将绘图保存到本地文件并插入到html中)
                  Plotly: What color cycle does plotly express follow?(情节:情节表达遵循什么颜色循环?)
                  How to save plotly express plot into a html or static image file?(如何将情节表达图保存到 html 或静态图像文件中?)
                  Plotly: How to make a line plot from a pandas dataframe with a long or wide format?(Plotly:如何使用长格式或宽格式的 pandas 数据框制作线图?)

                    1. <legend id='5Y8PU'><style id='5Y8PU'><dir id='5Y8PU'><q id='5Y8PU'></q></dir></style></legend>
                        • <tfoot id='5Y8PU'></tfoot>
                            <bdo id='5Y8PU'></bdo><ul id='5Y8PU'></ul>
                            <i id='5Y8PU'><tr id='5Y8PU'><dt id='5Y8PU'><q id='5Y8PU'><span id='5Y8PU'><b id='5Y8PU'><form id='5Y8PU'><ins id='5Y8PU'></ins><ul id='5Y8PU'></ul><sub id='5Y8PU'></sub></form><legend id='5Y8PU'></legend><bdo id='5Y8PU'><pre id='5Y8PU'><center id='5Y8PU'></center></pre></bdo></b><th id='5Y8PU'></th></span></q></dt></tr></i><div id='5Y8PU'><tfoot id='5Y8PU'></tfoot><dl id='5Y8PU'><fieldset id='5Y8PU'></fieldset></dl></div>

                          • <small id='5Y8PU'></small><noframes id='5Y8PU'>

                              <tbody id='5Y8PU'></tbody>