Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Propagate loc="best" down to AnchoredOffsetbox? #17946

Open
jklymak opened this issue Jul 16, 2020 · 4 comments
Open

Propagate loc="best" down to AnchoredOffsetbox? #17946

jklymak opened this issue Jul 16, 2020 · 4 comments
Labels
Difficulty: Medium https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues

Comments

@jklymak
Copy link
Member

jklymak commented Jul 16, 2020

Folks often want a way to automatically place text, but not use a legend (#4489, #1313), but attempts to implement this have not passed muster (#12841)

As pointed out by @ImportanceofBeeingEarnest one way to make this more constrained is to use AnchoredOffsetbox which legend already uses, and just propagate the legend logic to the parent class. The folks could put their text or other annotations in this box.

@andrzejnovak I think what you're asking for is rather a loc="best" option for AnchoredOffsetbox, such that one could write AnchoredText("my text", loc="best"). This is very different from what this PR suggested, and most concerns raised here would not even apply.

Originally posted by @ImportanceOfBeingErnest in #12841 (comment)

@jklymak jklymak added the Difficulty: Medium https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues label Jul 16, 2020
@jklymak jklymak added this to the v3.4.0 milestone Jul 16, 2020
@QuLogic QuLogic modified the milestones: v3.4.0, v3.5.0 Jan 27, 2021
@QuLogic QuLogic modified the milestones: v3.5.0, v3.6.0 Sep 25, 2021
@QuLogic QuLogic modified the milestones: v3.6.0, v3.7.0 Aug 24, 2022
@QuLogic QuLogic modified the milestones: v3.7.0, future releases Jan 25, 2023
@codeananda
Copy link

codeananda commented May 30, 2023

Could something like this answer by Chipmuenk do the trick?

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as mpl_patches

x = np.linspace(-1,1)
fig, ax = plt.subplots()
ax.plot(x, x*x)

labels = [
    "pi = {0:.4g}".format(np.pi),
    "root(2) = {0:.4g}".format(np.sqrt(2))
]

# create a list with as many empty handles as labels
handles = [mpl_patches.Rectangle((0, 0), 1, 1, fc="white", ec="white", 
                                 lw=0, alpha=0)] * len(labels)

# create the legend, supressing the blank space of the empty line symbol and the
# padding between symbol and label by setting handlelenght and handletextpad
ax.legend(handles, labels, loc='best', fontsize='small', 
          fancybox=True, framealpha=0.7, 
          handlelength=0, handletextpad=0)
plt.show()
image

@timhoffm
Copy link
Member

timhoffm commented Jun 3, 2023

Could something like this answer by Chipmuenk do the trick?

Abusing legend as a simple list of texts is a hacking workaround. It may solve the problem for a certain subset of cases if users really need this capability in current versions of matplotlib. However, it cannot be an officially recommended way of working.

@codeananda
Copy link

Yes it is a hack. But it's the only one that gives you the option to use loc='best', right?

@timhoffm
Copy link
Member

timhoffm commented Jun 5, 2023

But it's the only one that gives you the option to use loc='best', right?

Yes, as long as this issue is not implemented it's the only option. But it's not the general solution needed to implement this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Difficulty: Medium https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues
Projects
None yet
Development

No branches or pull requests

4 participants