[英]Python Plotly - Multiple dropdown plots, each of which have subplots
我正在尝试结合两个Python Plotly功能。 其中之一是下拉菜单,用户可以在其中切换绘图( 链接到示例 )。 另一个功能是子图。
我有使用下拉菜单的工作代码,但没有子图。 因此,我在这里查找了如何创建子图,并且我认为我可以使用tools.make_subplots
图,就像处理go.Box
图一样。 很抱歉,如果这看起来很幼稚,我实际上对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')
“散布”中不允许使用“布局”
错误路径:['数据'] [0] ['布局']
PlotlyError:参数'figure_or_data'无效。 密谋不会
能够正确解析生成的JSON。 如果仍要将此“ figure_or_data”发送给Plotly(不推荐),则可以将“ validate = False”设置为绘图选项。这就是您看到此错误的原因:
“散布”中不允许使用“布局”
...
注意:当我通过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的帮助! 稍微编辑一下代码就给了我答案。
我认为您make_subplots()
图的概念有误解,得到错误的原因是因为make_subplots()
函数将创建其自己的layout
对象,因此您将得到错误。
'layout' is not allowed in 'scatter'
Path To Error: ['data'][0]['layout']
修改子图布局的正确方法是创建子图并访问各个对象属性并进行设置,如下所示。
updatemenus = list([
dict(active=-1,
x=-0.15,
buttons=buttons
)
])
fig['layout']['title'] = 'Title'
fig['layout']['showlegend'] = False
fig['layout']['updatemenus'] = updatemenus
另外,每次运行for循环时,您都在创建一个新的子图对象,这是错误的 。 我说的是下一行。
traces = []
for data in datas:
fig = tools.make_subplots(rows=1, cols=2)
您只需要分配一次,请参考以下工作示例,并尝试对用例实施相同的方法。
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)
df = pd.DataFrame([[1, 2], [3, 4], [5, 6], [7, 8]], columns=["A", "B"])
labels = ["A", "B"]
datas = [df, df]
### Create individual figures
# START
fig = tools.make_subplots(rows=2, cols=2)
for i, data in enumerate(datas):
trace1 = go.Bar(
x=data.head(10).A,
y=data.head(10).B,
showlegend=False
)
trace2 = go.Bar(
x=data.tail(10).A,
y=data.tail(10).B,
showlegend=False
)
fig.append_trace(trace1, i + 1, 1)
fig.append_trace(trace2, i + 1, 2)
### 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')
请告诉我是否可以解决您的问题!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.