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

Change default legend position to :outertopright #3399

Closed
niclasmattsson opened this issue Mar 29, 2021 · 14 comments
Closed

Change default legend position to :outertopright #3399

niclasmattsson opened this issue Mar 29, 2021 · 14 comments

Comments

@niclasmattsson
Copy link

This is halfway between a bug report and a feature request, so I'm not tagging it with either.

Please change the default legend position to :outertopright. Here's my one and only argument: a visualization package should not default to hiding part of the data you're trying to visualize.

@isentropic
Copy link
Member

isentropic commented Mar 29, 2021

a visualization package should not default to hiding part of the data you're trying to visualize.

well the arguably biggest plotting package matplotlib does

@niclasmattsson
Copy link
Author

Really? I haven't used Python or matplotlib in a long time, but I thought it used a heuristic to place the legend where it doesn't overlap the data. So if Plots.jl had a similar heuristic then an inside legend would be OK, but otherwise it really should place the legend outside.

@daschw
Copy link
Member

daschw commented Mar 29, 2021

I agree, that it would be nicer. Maybe we could change it with the next major release. For now, you can make it your default by adding const PLOTS_DEFAULTS = Dict{Symbol, Any}(:legend=>:outertopright) to your ~/.julia/config/startup.jl. You can add any plot attribute to this dict.

@isentropic
Copy link
Member

isentropic commented Mar 29, 2021

I still think that the current implementation is good already.

It's actually legend=:best
'Best' for pyplot is actually a smart legend placement so that it does not overlap with data.

Backends are supposed to find the best location that does not overlap with data but still manages to be within the plot. This is the best of both worlds, it is compact and does not intersect.

Most backends are unable to find the best location for the legend, hence they just fallback to topright

All in all, I think the current default is really good.

@isentropic
Copy link
Member

What could be changed is the inclusion of the legend by default for single series data. If only a single series is added perhaps the default y1 should not be placed at all

@lmiq
Copy link
Contributor

lmiq commented Jan 21, 2022

I have written a simple function set the legend position in a tentative minimal-overlapping position (see https://discourse.julialang.org/t/making-plots-place-its-legend-in-a-better-spot/74986/11?u=leandromartinez98)

What if Plots, with a function like that, defined the 'best' position and send it to the backend, instead of expecting the backend to support that feature? Apparently pyplot is the only one having that option.

@t-bltg
Copy link
Member

t-bltg commented Nov 20, 2022

I agree that having a best placement for GR would be an improvement, and it could be used by other backends as well.

@lmiq, can you create a PR ?

We'd need to @btime ... the placement algorithm obviously.

@lmiq
Copy link
Contributor

lmiq commented Nov 20, 2022

I can try, the function which is there is already fast (on the tenths of micro seconds), and can probably be optimized still.

What would be helpful for me is just a pointer on where such a function should be implemented, within GR.

@t-bltg
Copy link
Member

t-bltg commented Nov 20, 2022

on the tenths of micro seconds

👍

See the placeholder here:
https://github.com/JuliaPlots/Plots.jl/blob/master/src/backends/gr.jl#L1097-L1099

best should compute a new xpos, ypos.

@lmiq
Copy link
Contributor

lmiq commented Nov 20, 2022

That's perfect. As soon as possible I'll revise that function, possibly improve it l, and write a PR.

@t-bltg
Copy link
Member

t-bltg commented Nov 20, 2022

I think the function itself can reside in src/utils.jl and some tests can be added in test/test_utils.jl.

@lmiq
Copy link
Contributor

lmiq commented Nov 21, 2022

Done here: #4536

@lmiq
Copy link
Contributor

lmiq commented Nov 21, 2022

I think a nice way to test the placeholder is to plot repeatedly scatter(randn(100), randn(100)).

@t-bltg
Copy link
Member

t-bltg commented Nov 24, 2022

Fixed by #4536.

@t-bltg t-bltg closed this as completed Nov 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants