简体   繁体   中英

In plotly line plot, is there a way to set plotly to not join the gaps between different labels?

I have attached a screenshot to my problem, but basically this is data of activity when the subject is awake and asleep, and the red is for when he is asleep. Is there a way to not have the blue lines connect to each other?


px.line(df["Axis1"], color = df["is_asleep"])

screenshot of the plot

The ability to exclude holidays and certain times of the day has been added as a range-breaking feature. However, this is only effective when there is missing in one time series data, and in the example of your data, the first time series, the next time series, and the last time series would be drawn. Since you did not present any data, I will create similar data to illustrate.

import pandas as pd
import numpy as np

# sample data
df1 = pd.DataFrame({'time':pd.date_range('2022-06-01 00:00:00', '2022-06-01 12:00:00', freq='10min'), 'name':['a']*73, 'value': np.random.randint(0,3000,73)})
df2 = pd.DataFrame({'time':pd.date_range('2022-06-01 12:00:00', '2022-06-02 00:00:00', freq='10min'), 'name':['br']*73, 'value': np.random.randint(0,1000,73)})
df3 = pd.DataFrame({'time':pd.date_range('2022-06-02 00:00:00', '2022-06-02 12:00:00', freq='10min'), 'name':['a']*73, 'value': np.random.randint(0,3000,73)})
df = pd.concat([df1,df2,df3], axis=0, ignore_index=True)

import plotly.express as px

px.line(df, x=df['time'], y=df['value'], color='name')

在此处输入图像描述

 # use rangebreaks

fig = px.line(df, x=df['time'], y=df['value'], color='name')

fig.update_xaxes(
    rangebreaks=[
        dict(bounds=[12,0], pattern='hour')
    ]
)
fig.show()

在此处输入图像描述

import plotly.graph_objects as go

fig = go.Figure()
fig.add_trace(go.Scatter(
    mode='lines',
    x=df1['time'],
    y=df1['value'],
    line=dict(
        color='blue',
        width=2,
        ),
    name='a'
    )
)

fig.add_trace(go.Scatter(
    mode='lines',
    x=df2['time'],
    y=df2['value'],
    line=dict(
        color='red',
        width=2,
        ),
    name='br'
    )
)

fig.add_trace(go.Scatter(
    mode='lines',
    x=df3['time'],
    y=df3['value'],
    line=dict(
        color='blue',
        width=2,
        ),
    name='a',
    showlegend=False,
    )
)

fig.show()

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM