<bdo id='NyW6i'></bdo><ul id='NyW6i'></ul>

  1. <legend id='NyW6i'><style id='NyW6i'><dir id='NyW6i'><q id='NyW6i'></q></dir></style></legend>

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

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

      在 plotly 中添加索引作为下拉菜单

      Add index as dropdown menu in plotly(在 plotly 中添加索引作为下拉菜单)
      • <bdo id='vCPpM'></bdo><ul id='vCPpM'></ul>

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

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

                  <tbody id='vCPpM'></tbody>
                <tfoot id='vCPpM'></tfoot>
              2. <i id='vCPpM'><tr id='vCPpM'><dt id='vCPpM'><q id='vCPpM'><span id='vCPpM'><b id='vCPpM'><form id='vCPpM'><ins id='vCPpM'></ins><ul id='vCPpM'></ul><sub id='vCPpM'></sub></form><legend id='vCPpM'></legend><bdo id='vCPpM'><pre id='vCPpM'><center id='vCPpM'></center></pre></bdo></b><th id='vCPpM'></th></span></q></dt></tr></i><div id='vCPpM'><tfoot id='vCPpM'></tfoot><dl id='vCPpM'><fieldset id='vCPpM'></fieldset></dl></div>
                本文介绍了在 plotly 中添加索引作为下拉菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在使用以下代码绘制图表:

                fig = px.line(df, x='Time', y=['one','two'], color= df.index)fig['layout']['xaxis']['autorange'] = "reversed"fig.update_layout(legend_title=价格")图.show()

                奖励:如果您认为这种方法很乏味,并且滑动条就可以完成这项工作,那么 Plotly 支持条形图的动画.以下是您可以使用的代码:

                fig = px.bar(df, x='Time', y=['one','two'], animation_frame=df.index)fig.update_layout(title='Data', barmode='group')图.show()

                这是结果图:

                I am plotting chart using below code:

                fig = px.line(df, x='Time', y=['one','two'], color= df.index)
                fig['layout']['xaxis']['autorange'] = "reversed"
                fig.update_layout(legend_title="Price")
                fig.show()
                

                Dataframe i am working with like is below:

                      Time   one    two
                100   9:30   129    243
                110  10:30   234    453
                120  11:00   155    234
                

                Want to add dropdown menu to select from index and show one row at a time in chart. example if i select 110 from drop down it should only show chart for that row. Is there any easy fix for it. Thank you in adavance.

                解决方案

                Here's my solution:
                In order to set the proper options for the dropdown menu, it would be helpful to have a function that creates the list of options (shown below)

                # Create proper buttons list
                def makeButtonsList(idxs):
                    buttons = []
                    for i, idx in enumerate(idxs):
                        visibleArr = np.full((2*df.index.shape[0],), 
                                             False, dtype=bool)       # 2x number of booleans since one/two vals are separate plots
                        visibleArr[2*i] = True                        # Set two booleans next to each other (representing one & two) to true
                        visibleArr[(2*i)+1] = True
                        buttons.append(dict(label=str(idx),
                                            method='update',
                                            args=[{'visible': list(visibleArr)}]))    # 'Visible' arg determines which plots are shown 
                                                                                      # depending on which dropdown is selected 
                    return buttons
                

                Next create the traces for the data (with your sample data, I created a bar chart but you could easily modify this)

                traces = []
                for i in range(df.Time.shape[0]):
                    rowData = df.iloc[i, :]
                    time = rowData.Time
                    one = rowData.one
                    two = rowData.two
                    traces.append(go.Bar(x=[time], y=[one], name='One'))
                    traces.append(go.Bar(x=[time], y=[two], name='Two'))
                

                where df is the dataframe you are working with.

                Finally put it all together and create the Plotly plot!

                # Import packages
                import pandas as pd
                import numpy as np
                
                import plotly.graph_objs as go
                import plotly.express as px
                
                # Create proper buttons list
                def makeButtonsList(idxs):
                    buttons = []
                    for i, idx in enumerate(idxs):
                        visibleArr = np.full((2*df.index.shape[0],), 
                                             False, dtype=bool)       # 2x number of booleans since one/two vals are separate plots
                        visibleArr[2*i] = True                        # Set two booleans next to each other (representing one & two) to true
                        visibleArr[(2*i)+1] = True
                        buttons.append(dict(label=str(idx),
                                            method='update',
                                            args=[{'visible': list(visibleArr)}]))    # 'Visible' arg determines which plots are shown 
                                                                                      # depending on which dropdown is selected 
                    return buttons
                
                # Create traces
                traces = []
                for i in range(df.Time.shape[0]):
                    rowData = df.iloc[i, :]
                    time = rowData.Time
                    one = rowData.one
                    two = rowData.two
                    traces.append(go.Bar(x=[time], y=[one], name='One'))
                    traces.append(go.Bar(x=[time], y=[two], name='Two'))
                
                # Create figure
                fig = go.Figure(data=traces)
                
                # Add dropdown options
                fig.update_layout(
                    updatemenus=[
                        dict(
                            buttons=makeButtonsList(df.index),
                            direction="down",
                            pad={"r": 10, "t": 10},
                            showactive=True,
                            x=0.55,
                            xanchor="left",
                            y=1.2,
                            yanchor="top"
                        ),
                    ]
                )
                
                
                # Add annotation for index selected
                fig.update_layout(
                    annotations=[
                        dict(text="Index:", showarrow=False,
                        x=0, y=1.15, yref="paper", align="left")
                    ],
                    xaxis_title = 'Time',
                    yaxis_title = 'Value',
                )
                
                # Show the plot
                fig.show()
                

                Here is a sample plot:

                BONUS: If you think this method is tedious, and a slider bar would do the job just fine, Plotly supports animation of bar charts. Here is the following code you could use:

                fig = px.bar(df, x='Time', y=['one','two'], animation_frame=df.index)
                fig.update_layout(title='Data', barmode='group')
                fig.show()
                

                Here is the resulting plot:

                这篇关于在 plotly 中添加索引作为下拉菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                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 数据框制作线图?)

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

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

                      1. <tfoot id='M47lN'></tfoot>
                          <bdo id='M47lN'></bdo><ul id='M47lN'></ul>