本文介绍了Python Plotly-多个下拉图,每个图都有子图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试结合两个Python Plotly功能.其中之一是下拉菜单,用户可以在其中切换绘图(链接至示例).另一个功能是子图.

I'm trying to combine two Python Plotly features. One of them is a drop down menu where a user can switch between plots (link to examples). The other feature is subplots.

我有使用下拉菜单的工作代码,但没有子图.因此,我在此处中查找了如何创建子图,并且我认为我可以将tools.make_subplots数字我对待go.Box数字的方式相同.很抱歉,如果这看起来很幼稚,我实际上对Plotly还是陌生的.

I have working code that uses the dropdown menu, but it doesn't have subplots. So I looked up how to create subplots here and I thought I could treat a tools.make_subplots figure the same way I treated a go.Box figure. I'm sorry if this seems naive, I'm actually fairly new to Plotly.

但是,这种尝试根本没有用,我看到了两个出现在底部的异常.

However, this attempt is not working at all, I'm seeing two exceptions rise which I've put at the very bottom.

# NOTE: This code goes in between 'START' and 'END' below
traces = []
for data in datas:
    traces.append(go.Box(
                            x=data.index,
                            y=data.values,
                            showlegend=False
                        ))

我的子图代码

import plotly.offline as py
import plotly.graph_objs as go
from plotly.offline import init_notebook_mode, iplot, plot
from plotly import tools
init_notebook_mode(connected=True)

### Create individual figures
# START
traces = []
for data in datas:
    fig = tools.make_subplots(rows=1, cols=2)

    trace1 = go.Box(
                    x=data.head(10).index,
                    y=data.head(10).values,
                    showlegend=False
                )
    trace2 = go.Box(
                    x=data.tail(10).index,
                    y=data.tail(10).values,
                    showlegend=False
                )
    fig.append_trace(trace1, 1, 1)
    fig.append_trace(trace2, 1, 2)
    traces.append(fig)
# END

### Create buttons for drop down menu
buttons = []
for i, label in enumerate(labels):
    visibility = [i==j for j in range(len(labels))]
    button = dict(
                 label =  label,
                 method = 'update',
                 args = [{'visible': visibility},
                     {'title': label}])
    buttons.append(button)

updatemenus = list([
    dict(active=-1,
         x=-0.15,
         buttons=buttons
    )
])

layout = dict(title='Title',
              showlegend=False,
              updatemenus=updatemenus)

fig = dict(data=traces, layout=layout)

iplot(fig, filename='dropdown')

错误1

错误的路径:['数据'] [0] ['布局']

Path To Error: ['data'][0]['layout']

错误2

这就是为什么您看到此错误的原因:

Here's why you're seeing this error:

散布"中不允许使用布局"

'layout' is not allowed in 'scatter'

...

注意:当我通过validate = False作为绘图选项时,我看到的只是带有下拉菜单功能的空白绘图

NOTE: When I pass validate=False as a plot option, all I see is an empty plot with drop down menu functionality

推荐答案

Naren是正确的,只需要绘制一个子图即可.而要创建下拉菜单效果,只需将两条迹线添加到同一位置即可.

Naren is right, only one subplots figure needs to be made. And to create the drop down menu effect, you just need to add two traces to the same position like this..

fig.append_trace(trace1, 1, 1)
fig.append_trace(trace2, 1, 1)

工作代码

import plotly.offline as py
import plotly.graph_objs as go
from plotly.offline import init_notebook_mode, iplot, plot
from plotly import tools
init_notebook_mode(connected=True)

x = [i for i in range(100)]
df_1 = pd.DataFrame([(i, 1+i) for i in range(100)], columns=["X", "Y"])
df_2 = pd.DataFrame([(i, i*i) for i in range(100)], columns=["X", "Y"])
labels = ["Plus one", "Square"]

### Create individual figures
# START
fig = tools.make_subplots(rows=1, cols=2)

trace1 = go.Bar(
                x=df_1.head(10).X,
                y=df_1.head(10).Y,
                showlegend=False
            )
trace2 = go.Bar(
                x=df_2.head(10).X,
                y=df_2.head(10).Y,
                showlegend=False
            )

fig.append_trace(trace1, 1, 1)
fig.append_trace(trace2, 1, 1)

trace1 = go.Bar(
                x=df_1.tail(10).X,
                y=df_1.tail(10).Y,
                showlegend=False
            )
trace2 = go.Bar(
                x=df_2.tail(10).X,
                y=df_2.tail(10).Y,
                showlegend=False
            )
fig.append_trace(trace1, 1, 2)
fig.append_trace(trace2, 1, 2)
# END

### Create buttons for drop down menu
buttons = []
for i, label in enumerate(labels):
    visibility = [i==j for j in range(len(labels))]
    button = dict(
                 label =  label,
                 method = 'update',
                 args = [{'visible': visibility},
                     {'title': label}])
    buttons.append(button)

updatemenus = list([
    dict(active=-1,
         x=-0.15,
         buttons=buttons
    )
])

fig['layout']['title'] = 'Title'
fig['layout']['showlegend'] = False
fig['layout']['updatemenus'] = updatemenus

iplot(fig, filename='dropdown')

谢谢

非常感谢 Naren Murali 为您提供帮助!稍微编辑一下代码就给了我答案.

Thank you

So much to Naren Murali for your help! Editing your code a bit gave me the answer.

我一直想创建这个.

I was looking to create this..

但是您的代码给了我这些图...

However your code was giving me these plots...

这篇关于Python Plotly-多个下拉图,每个图都有子图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 16:09