I am currently working with Plotly, specifically using px.timeline to create horizontal timeline bars for different groups with different colors representing their status. The goal is to use make_subplots to arrange multiple figures vertically. A single bar should look like this:
import pandas as pd
import plotly.express as px
data = [
[True, '2016-11-28 00:06:00.000000', '2016-11-28 00:10:00.000000', 'Lamp'],
[False, '2016-11-28 00:10:00.000000', '2016-11-28 00:14:00.000000', 'Lamp'],
[True, '2016-11-28 00:14:00.000000', '2016-11-28 00:18:00.000000', 'Lamp']
]
columns = ['status', 'start', 'end', 'group']
df = pd.DataFrame(data, columns=columns)
df['start'] = pd.to_datetime(df['start'])
df['end'] = pd.to_datetime(df['end'])
fig = px.timeline(df, x_start='start', x_end='end', y="group", color="status")
fig.show()
My problem starts when I use px.timeline to create separate figures and then combine them using make_subplots, the bars within each subplot are split across multiple bars, rather than appearing as a single horizontal bar for each group. The Subfigure looks like this:
from plotly.subplots import make_subplots
subfig = make_subplots(rows=2, cols=1)
fig = px.timeline(df, x_start='start', x_end='end', y="group", color="status", template="plotly")
fig2 = px.timeline(df, x_start='start', x_end='end', y="group", color="status", template="plotly")
subfig.add_traces(fig.data, rows=1, cols=1)
subfig.add_traces(fig2.data, rows=2, cols=1)
subfig.update_xaxes(type='date')
subfig.show()
The expected output should have been like in the first image, only twice. I can't find a solution to why the subplots get split when using make_subplots.
So far I've tried to
I would be very thankful for solutions with plotly.express and make_subplots or alternatively recommendations for other libraries that can realize this form of plot.
EricLavault's comment resolved the issue:
So you don't need subplots since you already have
y="group"
, just add data for the other groups in your dataframe.
Simply removing them and working with y
did the trick!