我正在尝试结合两个Python Plotly功能。其中一个是下拉菜单,用户可以在图表之间切换(link to examples)。另一个特征是子图。
我有使用下拉菜单的工作代码,但它没有子图。所以我查找了如何创建子图here,我认为我可以像处理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')
'scatter'中不允许使用'layout'
错误路径:['data'] [0] ['layout']
PlotlyError:'figure_or_data'参数无效。 Plotly不会是 能够正确解析生成的JSON。如果你想发送这个 无论如何,'figure_or_data'到Plotly(不推荐),你可以设置 'validate = False'作为情节选项。
以下是您看到此错误的原因:
'scatter'中不允许使用'layout'
...
注意:当我将validate = False作为绘图选项传递时,我看到的是一个带有下拉菜单功能的空图
答案 0 :(得分:2)
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对你的帮助太多了!编辑你的代码给了我答案。
答案 1 :(得分:1)
我认为你误解了子图的概念,你得到错误的原因是因为make_subplots()
函数会创建一个自己的layout
对象,因此你得到错误。< / p>
'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')
如果能解决您的问题,请告诉我们!