以下代码用于获取上述内容from random import randintimport plotly.graph_objects as gox1 = [randint(0, 9) for p in range(0, 10)]x2 = [randint(0, 9) for p in range(0, 10)]x3 = [randint(0, 9) for p in range(0, 10)]time=list(range(0, len(x1))) fig = go.Figure()fig.add_trace(go.Scatter(x=time, y=x1, mode='lines', name='X1'))fig.add_trace(go.Scatter(x=time, y=x2, mode='lines', name='X2'))fig.add_trace(go.Scatter(x=time, y=x3, mode='lines', name='X3'))但是,为了使代码更高效,我尝试了从这个链接获得的类似这样的东西data=[x1,x2,x3] ##这可能是错误的 for i in range(0,2): trace = go.Scatter( x = times, y = data[:, i], mode = 'markers+lines' )每当我尝试使用 for 循环使用 plotly 绘制图形时,什么都没有出现。感谢任何帮助。谢谢
1 回答
杨魅力
TA贡献1811条经验 获得超6个赞
我不知道为什么第一个版本没有显示fig.show()但没有显示但主要使用matplotliborplotly或其他模块(在脚本、python 的 shell 或笔记本中)我必须使用fig.show()(或plt.show()for matplotlib)来查看情节。
在更改了一些错误之后 -y = data[i]而不是 -y = data[:, i]而range(3)不是range(0, 2)- 此代码适用于我notebook
from random import randint
import plotly.graph_objects as go
x1 = [randint(0, 9) for p in range(10)]
x2 = [randint(0, 9) for p in range(10)]
x3 = [randint(0, 9) for p in range(10)]
time = list(range(len(x1)))
data = [x1, x2, x3]
fig = go.Figure()
for i in range(3):
fig.add_trace(go.Scatter(x=time, y=data[i], mode='markers+lines', name='X{}'.format(i)))
fig.show()
添加回答
举报
0/150
提交
取消