What I am trying to do
Hello, I’m trying to make something that looks similar to this excel spreadsheet. It has a Gantt chart on the right side and a table that it uses as its “y axis” on the left side
What I already have
I’ve only achieved the Gantt chart part, that is currently looking like this:
Code
import plotly.figure_factory as ff
from datetime import datetime
df = [
dict(Task="Job A", Start=datetime(2009, 1, 1), Finish=datetime(2009, 2, 28), Resource="Banana"),
dict(Task="Job B", Start=datetime(2009, 3, 5), Finish=datetime(2009, 4, 15), Resource="Apple"),
dict(Task="Job C", Start=datetime(2009, 2, 20), Finish=datetime(2009, 5, 30), Resource="Grape"),
]
fig = ff.create_gantt(df, colors="#4FADEA", title='')
annotations = []
for i, x in enumerate(df):
diff = abs(x['Finish'] - x['Start']) / 2
middle = x['Start'] + diff
title = dict(
x=middle,
y=i + 0.3,
text=x['Resource'],
showarrow=False,
font=dict(color="black"),
)
start_date = title.copy()
start_date.update(x=x['Start'], text=x['Start'].strftime('%d.%m'))
end_date = title.copy()
end_date.update(x=x['Finish'], text=x['Finish'].strftime('%d.%m'))
annotations += [title, start_date, end_date]
fig["layout"]["annotations"] = annotations
fig['layout']['xaxis']['rangeselector']['visible'] = False
fig.update_xaxes(tickformat='%b-%y\nКвартал %q')
fig.update_yaxes(tickmode='array', tickvals=[])
fig.update_layout(margin = {'l':0,'r':0,'t':0,'b':0},)
# fig.write_image("fig1.png")
fig.show(config=dict(displayModeBar = False))
And I have also managed to create a table
Code
import random
import plotly.graph_objects as go
fig = go.Figure(
data=[
go.Table(
header=dict(values=["ID", "Name", 'Property 1', 'Property 2', 'Property 4', 'Property 5']),
cells=dict(values=[
[1, 2, 3, 4, 5, 6],
['JOB A', 'JOB B', 'JOB C', 'JOB D', 'JOB E', 'JOB F'],
['m3', 'm3', 'm3', 'm3', 'm3', 'm3'],
[random.randint(100, 1000) for i in range(6)],
['0 days', '10 days', '20 days', '30 days', '40 days', '50 days'],
['43242/32423', '43242/32423', '43242/32423', '43242/32423', '43242/32423', '43242/32423', ],
]
),
)
]
)
fig.show()
What have I tried
I’ve tried making subplots with from plotly.subplots import make_subplots
like this:
Code
from plotly.subplots import make_subplots
import plotly.graph_objects as go
fig = make_subplots(rows=1, cols=2)
fig.add_trace(
go.Table(
header=dict(values=["A Scores", "B Scores"]),
cells=dict(values=[[100, 90, 80, 90], [95, 85, 75, 95]]),
),
row=1, col=1
)
fig.add_trace(
go.Table(
header=dict(values=["ID", "Name", 'Property 1', 'Property 2', 'Property 3', 'Property 4', 'Property 5']),
cells=dict(values=[
[100, 90, 80, 90],
[95, 85, 75, 95],
]),
),
row=1, col=2
)
fig.update_layout(height=600, width=800, title_text="Side By Side Subplots")
fig.show()
Error
ValueError: Trace type 'table' is not compatible with subplot type 'xy'
And I also tried making my Gantt chart into a subplot
Code
import plotly.figure_factory as ff
from datetime import datetime
from plotly.subplots import make_subplots
df = [
dict(
Task="Job A",
Start=datetime(2009, 1, 1),
Finish=datetime(2009, 2, 28),
Resource="Banana",
),
dict(
Task="Job B",
Start=datetime(2009, 3, 5),
Finish=datetime(2009, 4, 15),
Resource="Apple",
),
dict(
Task="Job C",
Start=datetime(2009, 2, 20),
Finish=datetime(2009, 5, 30),
Resource="Grape",
),
]
fig = ff.create_gantt(df, colors="#4FADEA", title="")
annotations = []
for i, x in enumerate(df):
diff = abs(x["Finish"] - x["Start"]) / 2
middle = x["Start"] + diff
title = dict(
x=middle,
y=i + 0.3,
text=x["Resource"],
showarrow=False,
font=dict(color="black"),
)
start_date = title.copy()
start_date.update(x=x["Start"], text=x["Start"].strftime("%d.%m"))
end_date = title.copy()
end_date.update(x=x["Finish"], text=x["Finish"].strftime("%d.%m"))
annotations += [title, start_date, end_date]
fig["layout"]["annotations"] = annotations
fig["layout"]["xaxis"]["rangeselector"]["visible"] = False
fig.update_xaxes(tickformat="%b-%y\nКвартал %q")
fig.update_yaxes(tickmode="array", tickvals=[])
fig.update_layout(
margin={"l": 0, "r": 0, "t": 0, "b": 0},
)
# fig.write_image("fig1.png")
# fig.show(config=dict(displayModeBar=False))
import plotly.graph_objects as go
layout = go.Layout(
xaxis=dict(
domain=[0, 0.45]
),
yaxis=dict(
domain=[0, 0.45]
),
xaxis2=dict(
domain=[0.55, 1]
),
xaxis4=dict(
domain=[0.55, 1],
anchor="y4"
),
yaxis3=dict(
domain=[0.55, 1]
),
yaxis4=dict(
domain=[0.55, 1],
anchor="x4"
)
)
plot = make_subplots(rows=1, cols=1)
plot.add_trace(
go.Figure(fig, layout=layout),
row=1, col=1
)
plot.show()
Error
raise ValueError(
ValueError:
Invalid element(s) received for the 'data' property of
Invalid elements include: [Figure({
'data': [{'fill': 'toself',
...
Question
Is there any chance someone will help me to combine a table and a Gantt chart into one figure please?