实际上,我正试图用一个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()
Plot:
如你所见,从图中可以看出,结果给出了一个奇怪的行为,我的两个轨迹都是从datetime.time(4, 45)开始的,而事实并非如此.注意到我的x_Axis范围是不同的,我认为这是主要的问题,事实上,我有多个样本数据,如x_axis是一个datetime. 时间,其中每个样本数据(Xn,Yn)都有自己的区间和自己的数值长度,但我知道我的x_axis的数值从最小值的datetime.time(0,0)区间到最大值的datetime.time(23,45)区间变化,步长为15min。
所以我的每一个 x1 , x2 ...,xn 都是在这个区间内取值的,所以我想用这个全局区间来固定 x_axis ( 我在接下来的代码中称为 range_data) ,然后在上面绘制我的任何一个样本数据。
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变量的长度不同。如果你仔细看一下你的图,你会发现,当你通过点击图例中的名称选择其中一条线时,两条线都显示得很好。在那里 可 是一个或多个错误的时间戳, 但我一直没能找到任何时间戳。那么,有什么办法呢?把你的数据组织在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()