我想一次绘制2个图形。 我像这样在Jupiter笔记本中写代码
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import plotly
import plotly.graph_objs as go
import plotly.offline as offline
plotly.offline.init_notebook_mode(connected=False)
x = "test.csv"
df = pd.read_csv(x)
a = df["A"].values.tolist()
b = df["B"].values.tolist()
a = pd.Series(a)
b = pd.Series(b)
data = [
plotly.graph_objs.Scatter(y = a, mode = 'lines', name = 'A')
plotly.graph_objs.Scatter(y = b, mode = 'lines', name = 'B', yaxis="y2")
]
layout = plotly.graph_objs.Layout(
title="A&B",
xaxis={"title":"Date"},
yaxis={"title":"Data-a"},
yaxis2={"title":"Data-b", "overlaying":"y", "side":"right"},
)
fig = plotly.graph_objs.Figure(data=data, layout=layout)
plotly.offline.iplot(fig)
运行它时,出现此错误:
plotly.graph_objs.Scatter(y = b, mode = 'lines', name = ‘B’, yaxis="y2")
^
SyntaxError: invalid syntax
我认为代码中没有语法错误,所以我真的不明白为什么会发生这种错误。 我的代码有什么问题?应该如何解决?
答案 0 :(得分:2)
这些行末尾缺少逗号:
data = [
plotly.graph_objs.Scatter(y = a, mode = 'lines', name = 'A'),
plotly.graph_objs.Scatter(y = b, mode = 'lines', name = 'B', yaxis="y2"),
]