如何在绘制多条图的同时绘制python的x_Axis范围

问题描述 投票:1回答:2

我实际上是试图用一个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()

图:

enter image description here

您可以从图中看到,结果给出了一个奇怪的行为,而我的两条迹线都是从datetime.time(4,45)开始的,情况并非如此。注意我的x_Axis范围是变化的,我认为这是主要问题,实际上,我有多个示例数据,其中x_axis是datetime.time,其中每个示例数据(Xn,Yn)都有自己的间隔和值的长度,但是我知道我的x_axis值的间隔为以15分钟为步长,以datetime.time(0,0)作为最小值,以datetime.time(23,45)作为最大值。

因此,我的x1,x2…,xn中的每个都在此时间间隔中获取值,因此我想用此全局时间间隔(在下一个代码中称为range_data)修复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')

谢谢,

python plotly plotly-dash plotly-python
2个回答
0
投票
import plotly.graph_objects  as go
import numpy
import pandas as pd
import datetime

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))

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']]

data = []

data.append(go.Scatter(x=df3['range_data'], y=df3['y1'], name='y1', mode='lines+markers', connectgaps=True))
data.append(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()]), margin=dict(t=0, l=0, b=0, r=0, pad=0))

fig = go.Figure(data=data, layout=layout)

fig.show()

enter image description here


0
投票

我不确定为什么会发生这种情况,但我有一个解决方案。原因之一似乎是您的x变量的长度不同。而且,如果您仔细查看您的绘图,您会发现在通过单击图例中的名称选择其中的一条时,两条线都显示得很好。 可能有一个或多个错误的时间戳,但我一直找不到。那么,解决方案是什么?在pandas数据框中组织数据,执行outer连接,并使用'ffill'和[>]插入缺失值

图:

enter image description here

代码:

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()
© www.soinside.com 2019 - 2024. All rights reserved.