gpt4 book ai didi

Python Plotly - 多个下拉图,每个都有子图

转载 作者:太空宇宙 更新时间:2023-11-04 00:17:06 26 4
gpt4 key购买 nike

问题

我正在尝试结合两个 Python Plotly 功能。其中之一是下拉菜单,用户可以在其中切换绘图 (link to examples)。另一个特征是子图。

我的尝试

我有使用下拉菜单的工作代码,但它没有子图。所以我查找了如何创建子图 here我认为我可以像对待 go.Box 图一样对待 tools.make_subplots 图。如果这看起来很幼稚,我很抱歉,我实际上是 Plotly 的新手。

但是,这种尝试根本不起作用,我看到两个异常上升,我已将它们放在最底部。

我的工作代码(无子图)

# 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

'layout' is not allowed in 'scatter'

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

错误2

PlotlyError: Invalid 'figure_or_data' argument. Plotly will not be
able to properly parse the resulting JSON. If you want to send this 'figure_or_data' to Plotly anyway (not recommended), you can set 'validate=False' as a plot option.

Here's why you're seeing this error:

'layout' is not allowed in 'scatter'

...

注意:当我将 validate=False 作为绘图选项传递时,我看到的只是一个带有下拉菜单功能的空绘图

最佳答案

Naren 是对的,只需要制作一个子图。而要创建下拉菜单效果,您只需要像这样在同一位置添加两条走线..

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 的帮助!稍微编辑您的代码给了我答案。

我正在寻找创造这个.. enter image description here enter image description here

但是你的代码给了我这些 plotly ...... enter image description here enter image description here

关于Python Plotly - 多个下拉图,每个都有子图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50518090/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com