[Python] Hide legend labels with underscore in matplotlib>3.10

Discussão em 'Python' iniciado por Stack, Agosto 25, 2025.

  1. Stack

    Stack Membro Participativo

    I create a plot with seaborn that has several lines and error bands. In the legend, I only want to show some of the labels and hide others.

    Previously, it was possible to call ax.legend(['one', '_', 'two']) to hide specific labels/artists from appearing in the legend. However, in the newest matplotlib version (>3.10) this behaviour has been removed. As I'm using seaborn, I don't have access to the ax.plot calls themself, in which I could set the labels manually.

    Is there any other way to selectively show only some legend labels and suppress others?

    import numpy as np
    import pandas as pd
    import seaborn as sns
    import matplotlib.pyplot as plt

    df = pd.DataFrame({'timepoint': np.random.randint(0, 10, 1000),
    'class': np.random.choice(['cat', 'dog', 'duck', 'hare'], 1000),
    'probability': np.random.rand(1000)
    })
    sns.lineplot(df, x='timepoint', y='probability', hue='class')

    # previously, providing underscores as names was hiding the label
    # this has unfortunately been deprecated
    plt.legend(['cat', *['_']*5, 'hare'])


    [​IMG]

    Continue reading...

Compartilhe esta Página