Search code examples
pythonchartsplotlyvisualizationsubplot

Color in Multiple Subplots Plotly Chart


I would like to have a multiple subplots to compare before and after workout progress. As you can see in the attached image, they are all in different colors and I would like to have the same color of before and after workout in each plot. Anyone knows how to do that?

This is what I have done so far to generate the subplots:

fig = make_subplots(
    rows=3, cols=2,
    specs = [[{'colspan':2}, None],
    [{},{}],
    [{},{}]],
    subplot_titles=("Body Mass", "Active Energy Burned", "Basal Energy Burned", "Distance Walking-Running", "Step Count"))

#Body Mass Chart
fig.add_trace(go.Scatter(x=record_data_df_dict["BodyMass"]["Date"], y=record_data_df_dict["BodyMass"]["BodyMass"],
    name = "Before Workout Routine"), row=1, col=1)

fig.add_trace(go.Scatter(x=record_data_df_BodyMass_start_Sep22["Date"], y=record_data_df_BodyMass_start_Sep22["BodyMass"],
    name = "After Workout Routine"), row=1, col=1)

# Active Energy Burned Chart
fig.add_trace(go.Bar(x=record_data_df_ActiveEnergyBurned_before_workout["Date"], y=record_data_df_ActiveEnergyBurned_before_workout["ActiveEnergyBurned"],
    name = "Before Workout Routine"), row=2, col=1)

fig.add_trace(go.Bar(x=record_data_df_ActiveEnergyBurned_after_workout["Date"], y=record_data_df_ActiveEnergyBurned_after_workout["ActiveEnergyBurned"],
    name = "After Workout Routine"), row=2, col=1)

# Basal Energy Burned Chart
fig.add_trace(go.Bar(x=record_data_df_BasalEnergyBurned_before_workout["Date"], y=record_data_df_BasalEnergyBurned_before_workout["BasalEnergyBurned"],
    name = "Before Workout Routine"), row=2, col=2)

fig.add_trace(go.Bar(x=record_data_df_BasalEnergyBurned_after_workout["Date"], y=record_data_df_BasalEnergyBurned_after_workout["BasalEnergyBurned"],
    name = "After Workout Routine"), row=2, col=2)

# Distance Chart
fig.add_trace(go.Bar(x=record_data_df_Distance_before_workout["Date"], y=record_data_df_Distance_before_workout["DistanceWalkingRunning"],
    name = "Before Workout Routine"), row=3, col=1)

fig.add_trace(go.Bar(x=record_data_df_Distance_after_workout["Date"], y=record_data_df_Distance_after_workout["DistanceWalkingRunning"],
    name = "After Workout Routine"), row=3, col=1)

# Step Chart
fig.add_trace(go.Bar(x=record_data_df_StepCount_before_workout["Date"], y=record_data_df_StepCount_before_workout["StepCount"],
    name = "Before Workout Routine"), row=3, col=2)

fig.add_trace(go.Bar(x=record_data_df_StepCount_after_workout["Date"], y=record_data_df_StepCount_after_workout["StepCount"],
    name = "After Workout Routine"), row=3, col=2)

fig.update_layout(height=800, width=600, showlegend=False, title_text="Before vs After Workout")
fig.show()

This is the generated plots: enter image description here


Solution

  • You need to set the marker_color argument in your add_trace calls to colour the bars/markers/lines.

    Using a hex string for example:

    fig.add_trace(go.Bar(x=x, y=y, name='example', marker_color='#cd5c5c'), row=1, col=1)