我正在尝试使用字典绘制多个y轴图,并得到此错误TypeError:'NoneType'对象不可调用。我尝试了多次尝试,但无法绘制图形。我正在使用2.7.0版
这是我完整的脚本
import plotly.graph_objs as go
params_list = {
"spd": {
"plot-graph": [
{
"x-axis": [1,6,12],
"y-axis": [.5,3,6],
"y-axis-title": "y 0",
"layout" : {
"title" : "sample 1",
"titlefont": "",
"tickfont": "",
"side": "",
"overlaying": ""}
},
{
"x-axis": [3,9,14],
"y-axis": [4,2,9],
"y-axis-title": "sample 2",
"layout" : {
"title": "y 2",
"titlefont": "rgb(120, 113, 159)",
"tickfont": "rgb(130, 103, 189)",
"side": "right",
"overlaying": "y"}
},
{
"x-axis": [12, 10, 5],
"y-axis": [8, 4, 11],
"y-axis-title": "sample 3",
"layout" : {
"title": "y3",
"titlefont": "rgb(112, 103, 189)",
"tickfont": "rgb(110, 103, 189)",
"side": "right",
"overlaying": "y"}
}
]
}
}
plot_graph = []
listTrace = []
cnt = 0;
#graph with multiple y axes
layout_kwargs = {'title' : "My TEST Graph"}
graph_list = params_list['spd']['plot-graph']
graph_list = [{ k: v for k, v in d.items() if v } for d in graph_list]
cnt = 0
for params in graph_list:
trace = go.Scatter(x=params['x-axis'],y = params["y-axis"], name = params["y-axis-title"], yaxis='y' + str(cnt + 1) * (cnt > 0))
y_axis_name = 'yaxis' + str(cnt + 1) * (cnt > 0)
layout = {k: v for k, v in params['layout'].items() if v }
if layout.has_key('titlefont'):
layout['titlefont'] = dict(color = layout['titlefont'])
if layout.has_key("tickfont"):
layout["tickfont"] = dict(color = layout['tickfont'])
layout_kwargs[y_axis_name] = layout
listTrace.append(trace)
cnt = cnt + 1
fig = go.Figure(data=listTrace, layout=go.Layout(**layout_kwargs))
fig.show()
以前的错误是在go.Figure方法中,但现在在fig.show()中。
答案 0 :(得分:0)
fig.show()
是在Plotly版本4中引入的,但如上所述,您在版本2中。