mwaskom / seaborn

Statistical data visualization in Python
https://seaborn.pydata.org
BSD 3-Clause "New" or "Revised" License
12.59k stars 1.93k forks source link

How to hide legend while using seaborn.objects? #3748

Closed MarkChenXY163 closed 3 months ago

MarkChenXY163 commented 3 months ago

I really like the flexibility of seaborn.objects. I would like to ask how to hide its legend?

Next is my code:

import pandas as pd
import numpy as np

# Define parameters
num_cages = 20          # Number of cages
num_weeks = 10          # Number of weeks
samples_per_week = 5    # Number of samples per week

# Generate data
np.random.seed(42)  # For reproducibility

# Create Week variable (1-10)
weeks = np.tile(np.arange(1, num_weeks + 1), num_cages * samples_per_week)

# Create Cage variable (1-20)
cages = np.repeat(np.arange(1, num_cages + 1), num_weeks * samples_per_week)

# Create Weight variable (randomly generated, assumed mean=50, std=5)
weights = np.random.normal(loc=50, scale=5, size=len(weeks))

# Create DataFrame
total_data = pd.DataFrame({
    'Week': weeks,
    'Cage': cages,
    'Weight': weights
})

(
    so.Plot(total_data, x='Week', y="Weight", color='Cage')
    .facet('Cage', wrap=cols).label(col="Cage")
    .layout(extent=[0.,0.,3.,3.])
    .limit(y=(0, None))
    .scale(color="Paired")
    .add(so.Line(), so.Agg())
    .add(so.Dot(), so.Agg('mean'))
    .add(so.Band(), so.Est())
)

output

mwaskom commented 3 months ago

legend=False.