Plotly:如何在绘制多条轨迹时设置 x_axis 范围?
Plotly: How to set x_axis range while plotting multiple traces?
我实际上是在尝试用一条 x_axis 绘制多条轨迹。
我用于绘图的代码是:
import plotly.graph_objects as go
import datetime
import numpy
x1= numpy.array([datetime.time(4, 45), datetime.time(5, 15), datetime.time(5, 30),datetime.time(5, 45), datetime.time(6, 0), datetime.time(6, 15),
datetime.time(6, 30), datetime.time(6, 45), datetime.time(7, 0),datetime.time(7, 15), datetime.time(7, 30), datetime.time(7, 45),
datetime.time(8, 0), datetime.time(8, 15), datetime.time(8, 30),datetime.time(8, 45), datetime.time(9, 0), datetime.time(9, 15),
datetime.time(9, 30), datetime.time(9, 45), datetime.time(10, 0),datetime.time(10, 15), datetime.time(10, 30),datetime.time(10, 45), datetime.time(11, 0), datetime.time(11, 15),datetime.time(11, 30), datetime.time(11, 45), datetime.time(12, 0),
datetime.time(12, 15), datetime.time(12, 30),datetime.time(12, 45), datetime.time(13, 0), datetime.time(13, 15),datetime.time(13, 30), datetime.time(13, 45), datetime.time(14, 0),datetime.time(14, 15), datetime.time(14, 30),datetime.time(14, 45), datetime.time(15, 0), datetime.time(15, 15),datetime.time(15, 30), datetime.time(15, 45), datetime.time(16, 0),datetime.time(16, 15), datetime.time(16, 30),
datetime.time(16, 45), datetime.time(17, 0), datetime.time(17, 15),datetime.time(17, 30), datetime.time(17, 45), datetime.time(18, 0),
datetime.time(18, 15), datetime.time(18, 30),datetime.time(18, 45), datetime.time(19, 0), datetime.time(19, 15),datetime.time(19, 30), datetime.time(19, 45), datetime.time(20, 0),datetime.time(20, 15), datetime.time(20, 30),
datetime.time(20, 45), datetime.time(21, 0), datetime.time(21, 15),datetime.time(22, 15), datetime.time(22, 45),datetime.time(23, 15), datetime.time(23, 30),datetime.time(23, 45)], dtype=object)
y1= numpy.array([ 13., 16., 8., 28., 45., 61., 74., 133., 191., 177., 249.,219., 249., 197., 89., 94., 38., 16., 4., 4., 12., 24.,
15., 34., 19., 23., 93., 30., 40., 30., 54., 41., 27.,23., 13., 31., 15., 19., 18., 14., 32., 19., 14., 23.,102., 30., 17., 18., 105., 67., 55., 56., 106., 50., 25.,17., 12., 8., 17., 12., 15., 8., 7., 6., 1., 10.,9., 5., 1., 1., 1.])
x2 = numpy.array(
[datetime.time(0, 15), datetime.time(4, 45), datetime.time(5, 15),datetime.time(5, 30), datetime.time(5, 45), datetime.time(6, 0),datetime.time(6, 15), datetime.time(6, 30), datetime.time(6, 45),datetime.time(7, 0), datetime.time(7, 15), datetime.time(7, 30),
datetime.time(7, 45), datetime.time(8, 0), datetime.time(8, 15),datetime.time(8, 30), datetime.time(8, 45), datetime.time(9, 0),datetime.time(9, 15), datetime.time(9, 30), datetime.time(9, 45),datetime.time(10, 0), datetime.time(10, 15), datetime.time(10, 30),datetime.time(10, 45), datetime.time(11, 0), datetime.time(11, 15),
datetime.time(11, 30), datetime.time(11, 45), datetime.time(12, 0),datetime.time(12, 15), datetime.time(12, 30),datetime.time(12, 45), datetime.time(13, 0), datetime.time(13, 15),datetime.time(13, 30), datetime.time(13, 45), datetime.time(14, 0),datetime.time(14, 15), datetime.time(14, 30),
datetime.time(14, 45), datetime.time(15, 0), datetime.time(15, 15),datetime.time(15, 30), datetime.time(15, 45), datetime.time(16, 0),datetime.time(16, 15), datetime.time(16, 30),datetime.time(16, 45), datetime.time(17, 0), datetime.time(17, 15),
datetime.time(17, 30), datetime.time(17, 45), datetime.time(18, 0),datetime.time(18, 15), datetime.time(18, 30),datetime.time(18, 45), datetime.time(19, 0), datetime.time(19, 15),datetime.time(19, 30), datetime.time(19, 45), datetime.time(20, 0),datetime.time(20, 15), datetime.time(20, 30),datetime.time(20, 45), datetime.time(21, 0), datetime.time(21, 15),
datetime.time(21, 30), datetime.time(21, 45), datetime.time(22, 0),datetime.time(22, 15), datetime.time(22, 30),datetime.time(22, 45), datetime.time(23, 0), datetime.time(23, 15),datetime.time(23, 30)], dtype=object)
y2 = numpy.array([ 1., 16., 15., 7., 27., 27., 86., 39., 92., 157., 257.,
278., 253., 273., 217., 200., 94., 86., 54., 42., 24., 37.,34., 38., 34., 35., 26., 51., 29., 35., 30., 62., 27.,30., 19., 19., 24., 23., 19., 16., 13., 33., 36., 20.,
35., 127., 50., 22., 28., 103., 84., 43., 41., 100., 33.,33., 21., 20., 19., 20., 13., 8., 12., 8., 11., 2.,
14., 3., 4., 4., 7., 1., 3., 1., 2., 1.])
fig = go.Figure(data=[
go.Scatter(
x= x1,
y= y1,
mode = 'markers'
),
go.Scatter(
x= x2,
y= y2,
mode = 'markers'
)])
fig.show()
剧情:
正如您从图中看到的那样,结果给出了一个奇怪的行为,我的两条轨迹都是从 datetime.time(4, 45) 开始的,但事实并非如此。
注意到我的 x_Axis 范围在变化,我认为这是主要问题,
事实上,我有多个样本数据,其中 x_axis 是一个 datetime.time,其中每个样本数据 (Xn,Yn) 都有自己的间隔和值的长度,但我知道我的 x_axis 值从作为最小值的 datetime.time(0,0) 的间隔到作为最大值的 datetime.time(23,45) 的间隔变化,步长为 15 分钟。
所以我的每个 x1 , x2 …,xn 都在这个区间内取值,所以我想用这个全局区间修复 x_axis(我在下一个代码),然后在上面绘制我的任何示例数据。
range_data = pd.DataFrame()
range_data['range_data'] = pd.date_range("00:00", "23:45", freq="15min").time
range_data = range_data.set_index('range_data')
谢谢,
import plotly.graph_objects as go
import numpy
import pandas as pd
import datetime
# generate the data
x1 = numpy.array([datetime.time(4, 45), datetime.time(5, 15), datetime.time(5, 30), datetime.time(5, 45), datetime.time(6, 0), datetime.time(6, 15), datetime.time(6, 30), datetime.time(6, 45), datetime.time(7, 0), datetime.time(7, 15), datetime.time(7, 30), datetime.time(7, 45),
datetime.time(8, 0), datetime.time(8, 15), datetime.time(8, 30), datetime.time(8, 45), datetime.time(9, 0), datetime.time(9, 15), datetime.time(9, 30), datetime.time(9, 45), datetime.time(10, 0), datetime.time(10, 15), datetime.time(10, 30), datetime.time(10, 45),
datetime.time(11, 0), datetime.time(11, 15), datetime.time(11, 30), datetime.time(11, 45), datetime.time(12, 0), datetime.time(12, 15), datetime.time(12, 30), datetime.time(12, 45), datetime.time(13, 0), datetime.time(13, 15), datetime.time(13, 30), datetime.time(13, 45), datetime.time(14, 0), datetime.time(14, 15), datetime.time(14, 30),datetime.time(14, 45), datetime.time(15, 0), datetime.time(15, 15),
datetime.time(15, 30), datetime.time(15, 45), datetime.time(16, 0), datetime.time(16, 15), datetime.time(16, 30), datetime.time(16, 45), datetime.time(17, 0), datetime.time(17, 15), datetime.time(17, 30), datetime.time(17, 45), datetime.time(18, 0), datetime.time(18, 15),
datetime.time(18, 30), datetime.time(18, 45), datetime.time(19, 0), datetime.time(19, 15), datetime.time(19, 30), datetime.time(19, 45), datetime.time(20, 0), datetime.time(20, 15), datetime.time(20, 30), datetime.time(20, 45), datetime.time(21, 0), datetime.time(21, 15),
datetime.time(22, 15), datetime.time(22, 45), datetime.time(23, 15), datetime.time(23, 30), datetime.time(23, 45)], dtype=object)
y1 = numpy.array([13., 16., 8., 28., 45., 61., 74., 133., 191., 177., 249.,219., 249., 197., 89., 94., 38., 16., 4., 4., 12., 24., 15., 34., 19., 23., 93., 30., 40., 30., 54., 41., 27., 23., 13., 31., 15., 19., 18., 14., 32., 19., 14., 23.,
102., 30., 17., 18., 105., 67., 55., 56., 106., 50., 25., 17., 12., 8., 17., 12., 15., 8., 7., 6., 1., 10., 9., 5., 1., 1., 1.])
x2 = numpy.array([datetime.time(0, 15), datetime.time(4, 45), datetime.time(5, 15), datetime.time(5, 30), datetime.time(5, 45), datetime.time(6, 0), datetime.time(6, 15), datetime.time(6, 30), datetime.time(6, 45), datetime.time(7, 0), datetime.time(7, 15), datetime.time(7, 30), datetime.time(7, 45), datetime.time(8, 0),
datetime.time(8, 15), datetime.time(8, 30), datetime.time(8, 45), datetime.time(9, 0), datetime.time(9, 15), datetime.time(9, 30), datetime.time(9, 45), datetime.time(10, 0), datetime.time(10, 15), datetime.time(10, 30), datetime.time(10, 45), datetime.time(11, 0), datetime.time(11, 15), datetime.time(11, 30),
datetime.time(11, 45), datetime.time(12, 0), datetime.time(12, 15), datetime.time(12, 30), datetime.time(12, 45), datetime.time(13, 0), datetime.time(13, 15), datetime.time(13, 30), datetime.time(13, 45), datetime.time(14, 0), datetime.time(14, 15), datetime.time(14, 30), datetime.time(14, 45), datetime.time(15, 0),
datetime.time(15, 15), datetime.time(15, 30), datetime.time(15, 45), datetime.time(16, 0), datetime.time(16, 15), datetime.time(16, 30), datetime.time(16, 45), datetime.time(17, 0), datetime.time(17, 15), datetime.time(17, 30), datetime.time(17, 45), datetime.time(18, 0), datetime.time(18, 15), datetime.time(18, 30),
datetime.time(18, 45), datetime.time(19, 0), datetime.time(19, 15), datetime.time(19, 30), datetime.time(19, 45), datetime.time(20, 0), datetime.time(20, 15), datetime.time(20, 30), datetime.time(20, 45), datetime.time(21, 0), datetime.time(21, 15), datetime.time(21, 30), datetime.time(21, 45), datetime.time(22, 0),
datetime.time(22, 15), datetime.time(22, 30), datetime.time(22, 45), datetime.time(23, 0), datetime.time(23, 15), datetime.time(23, 30)], dtype=object)
y2 = numpy.array([1., 16., 15., 7., 27., 27., 86., 39., 92., 157., 257., 278., 253., 273., 217., 200., 94., 86., 54., 42., 24., 37., 34., 38., 34., 35., 26., 51., 29., 35., 30., 62., 27., 30., 19., 19., 24., 23., 19., 16., 13., 33., 36., 20.,
35., 127., 50., 22., 28., 103., 84., 43., 41., 100., 33.,33., 21., 20., 19., 20., 13., 8., 12., 8., 11., 2., 14., 3., 4., 4., 7., 1., 3., 1., 2., 1.])
df1 = pd.DataFrame(dict(x1=x1, y1=y1))
df2 = pd.DataFrame(dict(x2=x2, y2=y2))
# organize the data in a unique data frame
# with a common time axis
df3 = pd.DataFrame()
df3['range_data'] = pd.date_range('00:00', '23:45', freq='15min').time
df3 = pd.merge(left=df3, right=df1, left_on='range_data', right_on='x1', how='left')
df3 = pd.merge(left=df3, right=df2, left_on='range_data', right_on='x2', how='left')
df3 = df3[['range_data', 'y1', 'y2']]
# plot the data
data = [go.Scatter(x=df3['range_data'], y=df3['y1'], name='y1', mode='lines+markers', connectgaps=True),
go.Scatter(x=df3['range_data'], y=df3['y2'], name='y2', mode='lines+markers', connectgaps=True)]
layout = dict(xaxis=dict(range=[df3['range_data'].min(), df3['range_data'].max()]))
fig = go.Figure(data=data, layout=layout)
fig.show()
我不确定发生这种情况的确切原因,但我可以提供一个解决方案。
原因之一似乎是您的 x 变量长度不同。如果您仔细查看您的绘图,您会发现当您通过单击图例中的名称 select 其中一条时,两条线都显示得很好。 可能有一个或多个错误的时间戳,但我没能找到。那么,解决方案是什么?在 pandas 数据框中组织数据,执行 outer
连接并使用 'ffill'
和
插入缺失值
剧情:
代码:
import plotly.graph_objects as go
import datetime
import numpy
import pandas as pd
x1= numpy.array([datetime.time(4, 45), datetime.time(5, 15), datetime.time(5, 30),datetime.time(5, 45), datetime.time(6, 0), datetime.time(6, 15),
datetime.time(6, 30), datetime.time(6, 45), datetime.time(7, 0),datetime.time(7, 15), datetime.time(7, 30), datetime.time(7, 45),
datetime.time(8, 0), datetime.time(8, 15), datetime.time(8, 30),datetime.time(8, 45), datetime.time(9, 0), datetime.time(9, 15),
datetime.time(9, 30), datetime.time(9, 45), datetime.time(10, 0),datetime.time(10, 15), datetime.time(10, 30),datetime.time(10, 45), datetime.time(11, 0), datetime.time(11, 15),datetime.time(11, 30), datetime.time(11, 45), datetime.time(12, 0),
datetime.time(12, 15), datetime.time(12, 30),datetime.time(12, 45), datetime.time(13, 0), datetime.time(13, 15),datetime.time(13, 30), datetime.time(13, 45), datetime.time(14, 0),datetime.time(14, 15), datetime.time(14, 30),datetime.time(14, 45), datetime.time(15, 0), datetime.time(15, 15),datetime.time(15, 30), datetime.time(15, 45), datetime.time(16, 0),datetime.time(16, 15), datetime.time(16, 30),
datetime.time(16, 45), datetime.time(17, 0), datetime.time(17, 15),datetime.time(17, 30), datetime.time(17, 45), datetime.time(18, 0),
datetime.time(18, 15), datetime.time(18, 30),datetime.time(18, 45), datetime.time(19, 0), datetime.time(19, 15),datetime.time(19, 30), datetime.time(19, 45), datetime.time(20, 0),datetime.time(20, 15), datetime.time(20, 30),
datetime.time(20, 45), datetime.time(21, 0), datetime.time(21, 15),datetime.time(22, 15), datetime.time(22, 45),datetime.time(23, 15), datetime.time(23, 30),datetime.time(23, 45)], dtype=object)
y1= numpy.array([ 13., 16., 8., 28., 45., 61., 74., 133., 191., 177., 249.,219., 249., 197., 89., 94., 38., 16., 4., 4., 12., 24.,
15., 34., 19., 23., 93., 30., 40., 30., 54., 41., 27.,23., 13., 31., 15., 19., 18., 14., 32., 19., 14., 23.,102., 30., 17., 18., 105., 67., 55., 56., 106., 50., 25.,17., 12., 8., 17., 12., 15., 8., 7., 6., 1., 10.,9., 5., 1., 1., 1.])
x2 = numpy.array(
[datetime.time(0, 15), datetime.time(4, 45), datetime.time(5, 15),datetime.time(5, 30), datetime.time(5, 45), datetime.time(6, 0),datetime.time(6, 15), datetime.time(6, 30), datetime.time(6, 45),datetime.time(7, 0), datetime.time(7, 15), datetime.time(7, 30),
datetime.time(7, 45), datetime.time(8, 0), datetime.time(8, 15),datetime.time(8, 30), datetime.time(8, 45), datetime.time(9, 0),datetime.time(9, 15), datetime.time(9, 30), datetime.time(9, 45),datetime.time(10, 0), datetime.time(10, 15), datetime.time(10, 30),datetime.time(10, 45), datetime.time(11, 0), datetime.time(11, 15),
datetime.time(11, 30), datetime.time(11, 45), datetime.time(12, 0),datetime.time(12, 15), datetime.time(12, 30),datetime.time(12, 45), datetime.time(13, 0), datetime.time(13, 15),datetime.time(13, 30), datetime.time(13, 45), datetime.time(14, 0),datetime.time(14, 15), datetime.time(14, 30),
datetime.time(14, 45), datetime.time(15, 0), datetime.time(15, 15),datetime.time(15, 30), datetime.time(15, 45), datetime.time(16, 0),datetime.time(16, 15), datetime.time(16, 30),datetime.time(16, 45), datetime.time(17, 0), datetime.time(17, 15),
datetime.time(17, 30), datetime.time(17, 45), datetime.time(18, 0),datetime.time(18, 15), datetime.time(18, 30),datetime.time(18, 45), datetime.time(19, 0), datetime.time(19, 15),datetime.time(19, 30), datetime.time(19, 45), datetime.time(20, 0),datetime.time(20, 15), datetime.time(20, 30),datetime.time(20, 45), datetime.time(21, 0), datetime.time(21, 15),
datetime.time(21, 30), datetime.time(21, 45), datetime.time(22, 0),datetime.time(22, 15), datetime.time(22, 30),datetime.time(22, 45), datetime.time(23, 0), datetime.time(23, 15),datetime.time(23, 30)], dtype=object)
y2 = numpy.array([ 1., 16., 15., 7., 27., 27., 86., 39., 92., 157., 257.,
278., 253., 273., 217., 200., 94., 86., 54., 42., 24., 37.,34., 38., 34., 35., 26., 51., 29., 35., 30., 62., 27.,30., 19., 19., 24., 23., 19., 16., 13., 33., 36., 20.,
35., 127., 50., 22., 28., 103., 84., 43., 41., 100., 33.,33., 21., 20., 19., 20., 13., 8., 12., 8., 11., 2.,
14., 3., 4., 4., 7., 1., 3., 1., 2., 1.])
# Organize data in pandas dataframes
df1 = pd.DataFrame({'x':x1, 'y1':y1})
df2 = pd.DataFrame({'x':x2, 'y2':y2})
# merge data
df = pd.merge(df2, df1, how='outer', on='x')
df = df.fillna(method='ffill')
df = df.fillna(method='bfill')
# set up plotly figure
fig = go.Figure()
# add trace for y1
fig.add_traces(go.Scatter(
x= df['x'],
y= df['y1'],
mode = 'lines'
))
# add trace for y2
fig.add_traces(go.Scatter(
x= df['x'],
y= df['y2'],
mode = 'lines'
))
fig.show()
我实际上是在尝试用一条 x_axis 绘制多条轨迹。
我用于绘图的代码是:
import plotly.graph_objects as go
import datetime
import numpy
x1= numpy.array([datetime.time(4, 45), datetime.time(5, 15), datetime.time(5, 30),datetime.time(5, 45), datetime.time(6, 0), datetime.time(6, 15),
datetime.time(6, 30), datetime.time(6, 45), datetime.time(7, 0),datetime.time(7, 15), datetime.time(7, 30), datetime.time(7, 45),
datetime.time(8, 0), datetime.time(8, 15), datetime.time(8, 30),datetime.time(8, 45), datetime.time(9, 0), datetime.time(9, 15),
datetime.time(9, 30), datetime.time(9, 45), datetime.time(10, 0),datetime.time(10, 15), datetime.time(10, 30),datetime.time(10, 45), datetime.time(11, 0), datetime.time(11, 15),datetime.time(11, 30), datetime.time(11, 45), datetime.time(12, 0),
datetime.time(12, 15), datetime.time(12, 30),datetime.time(12, 45), datetime.time(13, 0), datetime.time(13, 15),datetime.time(13, 30), datetime.time(13, 45), datetime.time(14, 0),datetime.time(14, 15), datetime.time(14, 30),datetime.time(14, 45), datetime.time(15, 0), datetime.time(15, 15),datetime.time(15, 30), datetime.time(15, 45), datetime.time(16, 0),datetime.time(16, 15), datetime.time(16, 30),
datetime.time(16, 45), datetime.time(17, 0), datetime.time(17, 15),datetime.time(17, 30), datetime.time(17, 45), datetime.time(18, 0),
datetime.time(18, 15), datetime.time(18, 30),datetime.time(18, 45), datetime.time(19, 0), datetime.time(19, 15),datetime.time(19, 30), datetime.time(19, 45), datetime.time(20, 0),datetime.time(20, 15), datetime.time(20, 30),
datetime.time(20, 45), datetime.time(21, 0), datetime.time(21, 15),datetime.time(22, 15), datetime.time(22, 45),datetime.time(23, 15), datetime.time(23, 30),datetime.time(23, 45)], dtype=object)
y1= numpy.array([ 13., 16., 8., 28., 45., 61., 74., 133., 191., 177., 249.,219., 249., 197., 89., 94., 38., 16., 4., 4., 12., 24.,
15., 34., 19., 23., 93., 30., 40., 30., 54., 41., 27.,23., 13., 31., 15., 19., 18., 14., 32., 19., 14., 23.,102., 30., 17., 18., 105., 67., 55., 56., 106., 50., 25.,17., 12., 8., 17., 12., 15., 8., 7., 6., 1., 10.,9., 5., 1., 1., 1.])
x2 = numpy.array(
[datetime.time(0, 15), datetime.time(4, 45), datetime.time(5, 15),datetime.time(5, 30), datetime.time(5, 45), datetime.time(6, 0),datetime.time(6, 15), datetime.time(6, 30), datetime.time(6, 45),datetime.time(7, 0), datetime.time(7, 15), datetime.time(7, 30),
datetime.time(7, 45), datetime.time(8, 0), datetime.time(8, 15),datetime.time(8, 30), datetime.time(8, 45), datetime.time(9, 0),datetime.time(9, 15), datetime.time(9, 30), datetime.time(9, 45),datetime.time(10, 0), datetime.time(10, 15), datetime.time(10, 30),datetime.time(10, 45), datetime.time(11, 0), datetime.time(11, 15),
datetime.time(11, 30), datetime.time(11, 45), datetime.time(12, 0),datetime.time(12, 15), datetime.time(12, 30),datetime.time(12, 45), datetime.time(13, 0), datetime.time(13, 15),datetime.time(13, 30), datetime.time(13, 45), datetime.time(14, 0),datetime.time(14, 15), datetime.time(14, 30),
datetime.time(14, 45), datetime.time(15, 0), datetime.time(15, 15),datetime.time(15, 30), datetime.time(15, 45), datetime.time(16, 0),datetime.time(16, 15), datetime.time(16, 30),datetime.time(16, 45), datetime.time(17, 0), datetime.time(17, 15),
datetime.time(17, 30), datetime.time(17, 45), datetime.time(18, 0),datetime.time(18, 15), datetime.time(18, 30),datetime.time(18, 45), datetime.time(19, 0), datetime.time(19, 15),datetime.time(19, 30), datetime.time(19, 45), datetime.time(20, 0),datetime.time(20, 15), datetime.time(20, 30),datetime.time(20, 45), datetime.time(21, 0), datetime.time(21, 15),
datetime.time(21, 30), datetime.time(21, 45), datetime.time(22, 0),datetime.time(22, 15), datetime.time(22, 30),datetime.time(22, 45), datetime.time(23, 0), datetime.time(23, 15),datetime.time(23, 30)], dtype=object)
y2 = numpy.array([ 1., 16., 15., 7., 27., 27., 86., 39., 92., 157., 257.,
278., 253., 273., 217., 200., 94., 86., 54., 42., 24., 37.,34., 38., 34., 35., 26., 51., 29., 35., 30., 62., 27.,30., 19., 19., 24., 23., 19., 16., 13., 33., 36., 20.,
35., 127., 50., 22., 28., 103., 84., 43., 41., 100., 33.,33., 21., 20., 19., 20., 13., 8., 12., 8., 11., 2.,
14., 3., 4., 4., 7., 1., 3., 1., 2., 1.])
fig = go.Figure(data=[
go.Scatter(
x= x1,
y= y1,
mode = 'markers'
),
go.Scatter(
x= x2,
y= y2,
mode = 'markers'
)])
fig.show()
剧情:
正如您从图中看到的那样,结果给出了一个奇怪的行为,我的两条轨迹都是从 datetime.time(4, 45) 开始的,但事实并非如此。 注意到我的 x_Axis 范围在变化,我认为这是主要问题, 事实上,我有多个样本数据,其中 x_axis 是一个 datetime.time,其中每个样本数据 (Xn,Yn) 都有自己的间隔和值的长度,但我知道我的 x_axis 值从作为最小值的 datetime.time(0,0) 的间隔到作为最大值的 datetime.time(23,45) 的间隔变化,步长为 15 分钟。
所以我的每个 x1 , x2 …,xn 都在这个区间内取值,所以我想用这个全局区间修复 x_axis(我在下一个代码),然后在上面绘制我的任何示例数据。
range_data = pd.DataFrame()
range_data['range_data'] = pd.date_range("00:00", "23:45", freq="15min").time
range_data = range_data.set_index('range_data')
谢谢,
import plotly.graph_objects as go
import numpy
import pandas as pd
import datetime
# generate the data
x1 = numpy.array([datetime.time(4, 45), datetime.time(5, 15), datetime.time(5, 30), datetime.time(5, 45), datetime.time(6, 0), datetime.time(6, 15), datetime.time(6, 30), datetime.time(6, 45), datetime.time(7, 0), datetime.time(7, 15), datetime.time(7, 30), datetime.time(7, 45),
datetime.time(8, 0), datetime.time(8, 15), datetime.time(8, 30), datetime.time(8, 45), datetime.time(9, 0), datetime.time(9, 15), datetime.time(9, 30), datetime.time(9, 45), datetime.time(10, 0), datetime.time(10, 15), datetime.time(10, 30), datetime.time(10, 45),
datetime.time(11, 0), datetime.time(11, 15), datetime.time(11, 30), datetime.time(11, 45), datetime.time(12, 0), datetime.time(12, 15), datetime.time(12, 30), datetime.time(12, 45), datetime.time(13, 0), datetime.time(13, 15), datetime.time(13, 30), datetime.time(13, 45), datetime.time(14, 0), datetime.time(14, 15), datetime.time(14, 30),datetime.time(14, 45), datetime.time(15, 0), datetime.time(15, 15),
datetime.time(15, 30), datetime.time(15, 45), datetime.time(16, 0), datetime.time(16, 15), datetime.time(16, 30), datetime.time(16, 45), datetime.time(17, 0), datetime.time(17, 15), datetime.time(17, 30), datetime.time(17, 45), datetime.time(18, 0), datetime.time(18, 15),
datetime.time(18, 30), datetime.time(18, 45), datetime.time(19, 0), datetime.time(19, 15), datetime.time(19, 30), datetime.time(19, 45), datetime.time(20, 0), datetime.time(20, 15), datetime.time(20, 30), datetime.time(20, 45), datetime.time(21, 0), datetime.time(21, 15),
datetime.time(22, 15), datetime.time(22, 45), datetime.time(23, 15), datetime.time(23, 30), datetime.time(23, 45)], dtype=object)
y1 = numpy.array([13., 16., 8., 28., 45., 61., 74., 133., 191., 177., 249.,219., 249., 197., 89., 94., 38., 16., 4., 4., 12., 24., 15., 34., 19., 23., 93., 30., 40., 30., 54., 41., 27., 23., 13., 31., 15., 19., 18., 14., 32., 19., 14., 23.,
102., 30., 17., 18., 105., 67., 55., 56., 106., 50., 25., 17., 12., 8., 17., 12., 15., 8., 7., 6., 1., 10., 9., 5., 1., 1., 1.])
x2 = numpy.array([datetime.time(0, 15), datetime.time(4, 45), datetime.time(5, 15), datetime.time(5, 30), datetime.time(5, 45), datetime.time(6, 0), datetime.time(6, 15), datetime.time(6, 30), datetime.time(6, 45), datetime.time(7, 0), datetime.time(7, 15), datetime.time(7, 30), datetime.time(7, 45), datetime.time(8, 0),
datetime.time(8, 15), datetime.time(8, 30), datetime.time(8, 45), datetime.time(9, 0), datetime.time(9, 15), datetime.time(9, 30), datetime.time(9, 45), datetime.time(10, 0), datetime.time(10, 15), datetime.time(10, 30), datetime.time(10, 45), datetime.time(11, 0), datetime.time(11, 15), datetime.time(11, 30),
datetime.time(11, 45), datetime.time(12, 0), datetime.time(12, 15), datetime.time(12, 30), datetime.time(12, 45), datetime.time(13, 0), datetime.time(13, 15), datetime.time(13, 30), datetime.time(13, 45), datetime.time(14, 0), datetime.time(14, 15), datetime.time(14, 30), datetime.time(14, 45), datetime.time(15, 0),
datetime.time(15, 15), datetime.time(15, 30), datetime.time(15, 45), datetime.time(16, 0), datetime.time(16, 15), datetime.time(16, 30), datetime.time(16, 45), datetime.time(17, 0), datetime.time(17, 15), datetime.time(17, 30), datetime.time(17, 45), datetime.time(18, 0), datetime.time(18, 15), datetime.time(18, 30),
datetime.time(18, 45), datetime.time(19, 0), datetime.time(19, 15), datetime.time(19, 30), datetime.time(19, 45), datetime.time(20, 0), datetime.time(20, 15), datetime.time(20, 30), datetime.time(20, 45), datetime.time(21, 0), datetime.time(21, 15), datetime.time(21, 30), datetime.time(21, 45), datetime.time(22, 0),
datetime.time(22, 15), datetime.time(22, 30), datetime.time(22, 45), datetime.time(23, 0), datetime.time(23, 15), datetime.time(23, 30)], dtype=object)
y2 = numpy.array([1., 16., 15., 7., 27., 27., 86., 39., 92., 157., 257., 278., 253., 273., 217., 200., 94., 86., 54., 42., 24., 37., 34., 38., 34., 35., 26., 51., 29., 35., 30., 62., 27., 30., 19., 19., 24., 23., 19., 16., 13., 33., 36., 20.,
35., 127., 50., 22., 28., 103., 84., 43., 41., 100., 33.,33., 21., 20., 19., 20., 13., 8., 12., 8., 11., 2., 14., 3., 4., 4., 7., 1., 3., 1., 2., 1.])
df1 = pd.DataFrame(dict(x1=x1, y1=y1))
df2 = pd.DataFrame(dict(x2=x2, y2=y2))
# organize the data in a unique data frame
# with a common time axis
df3 = pd.DataFrame()
df3['range_data'] = pd.date_range('00:00', '23:45', freq='15min').time
df3 = pd.merge(left=df3, right=df1, left_on='range_data', right_on='x1', how='left')
df3 = pd.merge(left=df3, right=df2, left_on='range_data', right_on='x2', how='left')
df3 = df3[['range_data', 'y1', 'y2']]
# plot the data
data = [go.Scatter(x=df3['range_data'], y=df3['y1'], name='y1', mode='lines+markers', connectgaps=True),
go.Scatter(x=df3['range_data'], y=df3['y2'], name='y2', mode='lines+markers', connectgaps=True)]
layout = dict(xaxis=dict(range=[df3['range_data'].min(), df3['range_data'].max()]))
fig = go.Figure(data=data, layout=layout)
fig.show()
我不确定发生这种情况的确切原因,但我可以提供一个解决方案。
原因之一似乎是您的 x 变量长度不同。如果您仔细查看您的绘图,您会发现当您通过单击图例中的名称 select 其中一条时,两条线都显示得很好。 可能有一个或多个错误的时间戳,但我没能找到。那么,解决方案是什么?在 pandas 数据框中组织数据,执行 outer
连接并使用 'ffill'
和
剧情:
代码:
import plotly.graph_objects as go
import datetime
import numpy
import pandas as pd
x1= numpy.array([datetime.time(4, 45), datetime.time(5, 15), datetime.time(5, 30),datetime.time(5, 45), datetime.time(6, 0), datetime.time(6, 15),
datetime.time(6, 30), datetime.time(6, 45), datetime.time(7, 0),datetime.time(7, 15), datetime.time(7, 30), datetime.time(7, 45),
datetime.time(8, 0), datetime.time(8, 15), datetime.time(8, 30),datetime.time(8, 45), datetime.time(9, 0), datetime.time(9, 15),
datetime.time(9, 30), datetime.time(9, 45), datetime.time(10, 0),datetime.time(10, 15), datetime.time(10, 30),datetime.time(10, 45), datetime.time(11, 0), datetime.time(11, 15),datetime.time(11, 30), datetime.time(11, 45), datetime.time(12, 0),
datetime.time(12, 15), datetime.time(12, 30),datetime.time(12, 45), datetime.time(13, 0), datetime.time(13, 15),datetime.time(13, 30), datetime.time(13, 45), datetime.time(14, 0),datetime.time(14, 15), datetime.time(14, 30),datetime.time(14, 45), datetime.time(15, 0), datetime.time(15, 15),datetime.time(15, 30), datetime.time(15, 45), datetime.time(16, 0),datetime.time(16, 15), datetime.time(16, 30),
datetime.time(16, 45), datetime.time(17, 0), datetime.time(17, 15),datetime.time(17, 30), datetime.time(17, 45), datetime.time(18, 0),
datetime.time(18, 15), datetime.time(18, 30),datetime.time(18, 45), datetime.time(19, 0), datetime.time(19, 15),datetime.time(19, 30), datetime.time(19, 45), datetime.time(20, 0),datetime.time(20, 15), datetime.time(20, 30),
datetime.time(20, 45), datetime.time(21, 0), datetime.time(21, 15),datetime.time(22, 15), datetime.time(22, 45),datetime.time(23, 15), datetime.time(23, 30),datetime.time(23, 45)], dtype=object)
y1= numpy.array([ 13., 16., 8., 28., 45., 61., 74., 133., 191., 177., 249.,219., 249., 197., 89., 94., 38., 16., 4., 4., 12., 24.,
15., 34., 19., 23., 93., 30., 40., 30., 54., 41., 27.,23., 13., 31., 15., 19., 18., 14., 32., 19., 14., 23.,102., 30., 17., 18., 105., 67., 55., 56., 106., 50., 25.,17., 12., 8., 17., 12., 15., 8., 7., 6., 1., 10.,9., 5., 1., 1., 1.])
x2 = numpy.array(
[datetime.time(0, 15), datetime.time(4, 45), datetime.time(5, 15),datetime.time(5, 30), datetime.time(5, 45), datetime.time(6, 0),datetime.time(6, 15), datetime.time(6, 30), datetime.time(6, 45),datetime.time(7, 0), datetime.time(7, 15), datetime.time(7, 30),
datetime.time(7, 45), datetime.time(8, 0), datetime.time(8, 15),datetime.time(8, 30), datetime.time(8, 45), datetime.time(9, 0),datetime.time(9, 15), datetime.time(9, 30), datetime.time(9, 45),datetime.time(10, 0), datetime.time(10, 15), datetime.time(10, 30),datetime.time(10, 45), datetime.time(11, 0), datetime.time(11, 15),
datetime.time(11, 30), datetime.time(11, 45), datetime.time(12, 0),datetime.time(12, 15), datetime.time(12, 30),datetime.time(12, 45), datetime.time(13, 0), datetime.time(13, 15),datetime.time(13, 30), datetime.time(13, 45), datetime.time(14, 0),datetime.time(14, 15), datetime.time(14, 30),
datetime.time(14, 45), datetime.time(15, 0), datetime.time(15, 15),datetime.time(15, 30), datetime.time(15, 45), datetime.time(16, 0),datetime.time(16, 15), datetime.time(16, 30),datetime.time(16, 45), datetime.time(17, 0), datetime.time(17, 15),
datetime.time(17, 30), datetime.time(17, 45), datetime.time(18, 0),datetime.time(18, 15), datetime.time(18, 30),datetime.time(18, 45), datetime.time(19, 0), datetime.time(19, 15),datetime.time(19, 30), datetime.time(19, 45), datetime.time(20, 0),datetime.time(20, 15), datetime.time(20, 30),datetime.time(20, 45), datetime.time(21, 0), datetime.time(21, 15),
datetime.time(21, 30), datetime.time(21, 45), datetime.time(22, 0),datetime.time(22, 15), datetime.time(22, 30),datetime.time(22, 45), datetime.time(23, 0), datetime.time(23, 15),datetime.time(23, 30)], dtype=object)
y2 = numpy.array([ 1., 16., 15., 7., 27., 27., 86., 39., 92., 157., 257.,
278., 253., 273., 217., 200., 94., 86., 54., 42., 24., 37.,34., 38., 34., 35., 26., 51., 29., 35., 30., 62., 27.,30., 19., 19., 24., 23., 19., 16., 13., 33., 36., 20.,
35., 127., 50., 22., 28., 103., 84., 43., 41., 100., 33.,33., 21., 20., 19., 20., 13., 8., 12., 8., 11., 2.,
14., 3., 4., 4., 7., 1., 3., 1., 2., 1.])
# Organize data in pandas dataframes
df1 = pd.DataFrame({'x':x1, 'y1':y1})
df2 = pd.DataFrame({'x':x2, 'y2':y2})
# merge data
df = pd.merge(df2, df1, how='outer', on='x')
df = df.fillna(method='ffill')
df = df.fillna(method='bfill')
# set up plotly figure
fig = go.Figure()
# add trace for y1
fig.add_traces(go.Scatter(
x= df['x'],
y= df['y1'],
mode = 'lines'
))
# add trace for y2
fig.add_traces(go.Scatter(
x= df['x'],
y= df['y2'],
mode = 'lines'
))
fig.show()