I am trying to have both subplots share the same color pattern for easy visualization. Both figures use the same variables for displaying content. The current legend is displayed as part of the pie chart.
I have been diligently looking around for the answer but can't seem to figure it out.
import plotly.graph_objects as go
from plotly.subplots import make_subplots
subfig = make_subplots(
rows=1, cols=2,
column_widths=[10,10],
row_heights=[10],
subplot_titles=("Enrollment by Percentage", "Enrollment by Status"),
specs=[[{"type": "pie"}, {"type": "bar"}]])
cols = ['Baseline Failure', 'Exited Study', 'Screen Failure', 'Enrolled', 'Completed Study']
count = [146, 33, 218, 555, 2]
subfig.add_trace(go.Pie(labels=cols, values=count, showlegend=True), 1,1)
subfig.add_trace(go.Bar(x=cols, y=count, showlegend=False),1,2)
subfig.update_layout(
template="plotly_dark", title='Patient Enrollment'
)
subfig.show()
Thank you for your help.
go.Pie takes marker=dict(colors="...")
and go.Bar takes marker_color="..."
Since you have 5 different values we can create color list
colors=['red','green','blue','yellow','purple']
import plotly.graph_objects as go
from plotly.subplots import make_subplots
subfig = make_subplots(
rows=1, cols=2,
column_widths=[10,10],
row_heights=[10],
subplot_titles=("Enrollment by Percentage", "Enrollment by Status"),
specs=[[{"type": "pie"}, {"type": "bar"}]])
colors = ['red','green','blue','yellow','purple'] # bar and pie colors
cols = ['Baseline Failure', 'Exited Study', 'Screen Failure', 'Enrolled',
'Completed Study']
count = [146, 33, 218, 555, 2]
subfig.add_trace(go.Pie(labels=cols, values=count, marker=dict(colors=colors),
showlegend=True), 1,1)
subfig.add_trace(go.Bar(x=cols, y=count,marker_color=colors,
showlegend=False),1,2)
subfig.update_layout(template="plotly_dark",
title='Patient Enrollment')
subfig.show()