如何在不必重复添加“ fig.add_trace”的情况下在同一图形上绘制所有10个图形?
import plotly.graph_objects as go
# Create random data with numpy
import numpy as np
random_x = np.linspace(0, 499, 500)
output = np.vstack([random.sample(range(0, 40), 40) for x in range(10)])
# Create traces
fig = go.Figure()
fig.add_trace(go.Scatter(x=random_x, y=output[1,:],
line=dict(color='Blue'),
mode='lines',
name='position 0'))
fig.add_trace(go.Scatter(x=random_x, y=output[2,:],
line=dict(color='Blue'),
mode='lines',
name='position 2'))
fig.add_trace(go.Scatter(x=random_x, y=output[3,:],
line=dict(color='Green'),
mode='lines',
name='position 3'))
fig.add_trace(go.Scatter(x=random_x, y=output[4,:],
line=dict(color='Red'),
mode='lines',
name='position 4'))
fig.show()
上面的代码在绘图上显示4条线
我刚刚添加了一个for循环。
import random
# Create random data with numpy
import numpy as np
random_x = np.linspace(0, 499, 500)
output = np.vstack([random.sample(range(0, 40), 40) for x in range(10)])
# Create traces
fig = go.Figure()
# Increase this to 10 colours
color = ['Blue', 'Red', 'Green', 'Yellow', 'Black', 'Grey']
for i in range(6):
fig.add_trace(go.Scatter(x=random_x, y=output[i+1,:],
line=dict(color=color[i]),
mode='lines',
name='position' + str(i)))
fig.show()