[Bug]: Legend overlaps shaded area in fill_between with legend location "best" · Issue #27414 · matplotlib/matplotlib · GitHub
Skip to content

[Bug]: Legend overlaps shaded area in fill_between with legend location "best" #27414

Description

@mauroo4

Bug summary

When using the fill_between function in matplotlib with a legend and specifying the legend location as "best", the legend is not taking into account the shaded area and may overlap with it.

Code for reproduction

import matplotlib.pyplot as plt
import numpy as np

# Example data
x = np.linspace(0, 5, 100)
y1 = 0.5 * np.exp(x) - 1
y2 = 2 * np.exp(x) + 1

# Plotting shaded area
plt.fill_between(x, y1, y2, color='gray', alpha=0.5, label='Shaded Area')

# Set y-axis limit
plt.ylim(-10, 100)

# Adding legend with location "best"
plt.legend(loc='best')

# Show the plot
plt.show()

Actual outcome

image

Expected outcome

image

Additional information

No response

Operating system

Windows 11

Matplotlib Version

3.8.0

Matplotlib Backend

module://backend_interagg

Python version

Python 3.11.5

Jupyter version

No response

Installation

conda

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions