问题
我正在尝试组合两个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”
错误路径:'data''layout‘
错误2
PlotlyError:无效的'figure_or_data‘参数。Plotly将不会
能够正确解析生成的JSON。如果您想要将此'figure_or_data‘发送到Plotly (不推荐),您可以将'validate=False’设置为绘图选项。
这就是你看到这个错误的原因:
“scatter”中不允许“‘layout”
..。
注意:当我将validate=False作为plot选项传递时,我看到的只是一个带有下拉菜单功能的空图
发布于 2018-05-29 02:22:42
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对您的帮助!稍微编辑一下你的代码就给了我答案。
我正在寻找创造这个..
但是你的代码给了我这些图。
发布于 2018-05-28 14:12:53
我认为你误解了子图的概念,你得到错误的原因是因为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
此外,每次运行循环时都会创建一个新的子图对象,这是错误的。我说的是下面这条线。
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')
请一定要让我知道,如果这解决了你的问题!
https://stackoverflow.com/questions/50518090
复制相似问题