1

I have a dataframe and I have plotted four different columns of the dataframe each with a 5th column and colour them based on a 6th categorical column as subplots in the same figure.

I want to have one legend for all subplots but with my code each category repeats 4 times. This is the code I tried:

num_subplots = len(column_y)
    rows = (num_subplots + 1) // 2  # Calculate rows based on number of subplots
    cols = 2 if num_subplots > 1 else 1  # Set cols based on number of subplots

    # Create an empty dictionary to store legend labels
    legend_labels = {}

    # Loop through unique categories in the color column
    for cat in df[color_column].unique().tolist():
        # Extract the label from the category
        legend_labels[cat] = str(cat).split(": ", 1)[0]
    
    fig = make_subplots(rows=rows, cols=cols, subplot_titles=column_y, shared_xaxes=True, shared_yaxes=True, vertical_spacing=0.1, horizontal_spacing=0.1)
    
    # Scatterplot for colored categories
    for i, col_y in enumerate(column_y, start=1):
        scatter = px.scatter(df, x=column_x, y=col_y, color=color_column, color_discrete_sequence=custom_palette, opacity=0.8)
        scatter.update_traces(marker=dict(size=5))
        scatter.for_each_trace(lambda trace: trace.update(name=legend_labels.get(trace.name, trace.name)))

        row_num = (i + 1) // 2 if num_subplots > 1 else 1
        col_num = (i % 2) + 1 if num_subplots > 1 else 1
        
        for trace in scatter.data:
            fig.add_trace(trace, row=row_num, col=col_num)
1
  • You can set showlegend=False where necessary, as shown in this post. Commented Dec 20, 2023 at 15:42

0