Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[ENH]: Add public method to update Legend object's loc property . #25014

Closed
@cocolato

Description

@cocolato

Problem

from matplotlib import pyplot as plt
from matplotlib.legend import Legend

def plot(loc: str):
    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)

    x = [-10.0, -9.657349547286204, -9.318462608835684, -9.031177432527166, -8.691618609025815, -8.407140700722843, -8.152708015644635, -7.839130676473357, -7.499034134688037, -7.172556788526309, -6.847257574849716, -6.552316320455642, -6.230727469453974, -5.914856113060868]
    y = [4.5397868702434395e-05, 6.394971420131934e-05, 8.974373333525978e-05, 0.00011960725629360318, 0.00016795968412322188, 0.000223217496066253, 0.00028787162356623547, 0.00039385623135828983, 0.0005533125089980317, 0.0007667698609716984, 0.0010612377365216156, 0.0014247739486663552, 0.001964154207369101, 0.002691782877150404]
    ax.plot(x, y, label="f(x)")
    if ax.legend():
        ax.legend_.set_visible(True)
        _loc_code = Legend.codes.get(loc, 'best')  # user choose the location
        ax.legend_._set_loc(_loc_code)  # Using a private function, which can be very fragile.
    plt.show()

plot("center")
  • Desired implementation
from matplotlib import pyplot as plt
from matplotlib.legend import Legend

def plot(loc: str):
    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)

    x = [-10.0, -9.657349547286204, -9.318462608835684, -9.031177432527166, -8.691618609025815, -8.407140700722843, -8.152708015644635, -7.839130676473357, -7.499034134688037, -7.172556788526309, -6.847257574849716, -6.552316320455642, -6.230727469453974, -5.914856113060868]
    y = [4.5397868702434395e-05, 6.394971420131934e-05, 8.974373333525978e-05, 0.00011960725629360318, 0.00016795968412322188, 0.000223217496066253, 0.00028787162356623547, 0.00039385623135828983, 0.0005533125089980317, 0.0007667698609716984, 0.0010612377365216156, 0.0014247739486663552, 0.001964154207369101, 0.002691782877150404]
    ax.plot(x, y, label="f(x)")
    if ax.legend():
        ax.legend_.set_visible(True)
        ax.legend_.set_loc(loc)  # A public method to change the legend location is better.
    plt.show()

plot("center")

Proposed solution

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions