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

Legend overrides #4427

Merged
merged 13 commits into from
Oct 1, 2024
Merged

Legend overrides #4427

merged 13 commits into from
Oct 1, 2024

Conversation

jkrumbiegel
Copy link
Member

@jkrumbiegel jkrumbiegel commented Sep 30, 2024

Implements a mechanism to override legend element attributes that are normally set by copying them from the respective plot objects. Introduces the LegendOverride object, although I'm not 100% sure if we really need that as API, or should just use whatever key-value pairs we can get. Now uses any key-value object.

The overrides can either be paired to label keywords (as that's the legend interface for plotting functions), or to the plotting functions themselves in the Legend constructor (there I don't think it makes sense to pair to labels although it might be perceived as inconsistent?).

Examples

f, ax, sc = scatter(
    cos.(range(0, 7pi, 100)),
    color = :black,
    markersize = 8,
    label = "cos" => (; markersize = 15)
)
scatter!(
    sin.(range(0, 7pi, 100)),
    color = :black,
    marker = :utriangle,
    markersize = 8,
    label = "sin" => (; markersize = 15)
)
Legend(f[1, 2], ax)
f
grafik
f = Figure()
ax = Axis(f[1, 1])
li = lines!(ax, 1:5, linestyle = :dot)
sc = scatter!(ax, 1:5, markersize = 10)
Legend(
    f[1, 2],
    [
        sc => (; markersize = 20),
        li => (; linewidth = 3),
        [li, sc] => (; color = :red),
        [li => (; linewidth = 3), sc => (; markersize = 20)],
    ],
    ["Scatter", "Line", "Both", "Both 2"],
    patchsize = (40, 20),
)
f
grafik
f, ax, bp = barplot(
    1:5,
    [1, 3, 2, 5, 4],
    color = 1:5,
    colorrange = (1, 5),
    colormap = :Set1_5,
    label = [label => (; color = i)
        for (i, label) in enumerate(["red", "blue", "green", "purple", "orange"])]
)
Legend(f[1, 2], ax)
f
grafik

@MakieBot
Copy link
Collaborator

MakieBot commented Sep 30, 2024

Compile Times benchmark

Note, that these numbers may fluctuate on the CI servers, so take them with a grain of salt. All benchmark results are based on the mean time and negative percent mean faster than the base branch. Note, that GLMakie + WGLMakie run on an emulated GPU, so the runtime benchmark is much slower. Results are from running:

using_time = @ctime using Backend
# Compile time
create_time = @ctime fig = scatter(1:4; color=1:4, colormap=:turbo, markersize=20, visible=true)
display_time = @ctime Makie.colorbuffer(display(fig))
# Runtime
create_time = @benchmark fig = scatter(1:4; color=1:4, colormap=:turbo, markersize=20, visible=true)
display_time = @benchmark Makie.colorbuffer(fig)
using create display create display
GLMakie 4.40s (4.33, 4.46) 0.04+- 105.60ms (104.26, 107.93) 1.28+- 459.34ms (445.00, 472.42) 10.33+- 9.30ms (9.00, 9.56) 0.24+- 25.91ms (25.76, 26.13) 0.13+-
master 4.39s (4.33, 4.43) 0.04+- 106.63ms (103.82, 113.33) 3.13+- 471.96ms (465.91, 482.80) 5.80+- 9.29ms (9.00, 9.63) 0.23+- 26.08ms (25.84, 26.43) 0.22+-
evaluation 1.00x invariant, 0.01s (0.17d, 0.76p, 0.04std) 1.01x invariant, -1.04ms (-0.43d, 0.44p, 2.21std) 1.03x faster ✓, -12.62ms (-1.51d, 0.02p, 8.06std) 1.00x invariant, 0.01ms (0.06d, 0.92p, 0.23std) 1.01x invariant, -0.17ms (-0.95d, 0.11p, 0.17std)
CairoMakie 4.07s (4.04, 4.09) 0.02+- 104.36ms (103.35, 107.15) 1.31+- 163.73ms (163.01, 164.84) 0.61+- 9.53ms (9.44, 9.64) 0.08+- 1.14ms (1.13, 1.15) 0.01+-
master 4.07s (4.06, 4.08) 0.01+- 104.11ms (103.30, 104.97) 0.51+- 163.93ms (163.25, 165.15) 0.71+- 9.44ms (9.36, 9.53) 0.06+- 1.13ms (1.12, 1.14) 0.01+-
evaluation 1.00x invariant, -0.0s (-0.17d, 0.76p, 0.01std) 1.00x invariant, 0.25ms (0.25d, 0.65p, 0.91std) 1.00x invariant, -0.2ms (-0.31d, 0.57p, 0.66std) 0.99x slower X, 0.09ms (1.32d, 0.03p, 0.07std) 0.99x invariant, 0.01ms (1.03d, 0.08p, 0.01std)
WGLMakie 4.66s (4.63, 4.71) 0.03+- 109.53ms (105.17, 127.06) 7.82+- 9.27s (9.20, 9.33) 0.04+- 11.91ms (11.24, 15.09) 1.41+- 118.47ms (116.37, 122.28) 2.17+-
master 4.63s (4.58, 4.69) 0.04+- 106.96ms (105.56, 109.60) 1.40+- 9.25s (9.02, 9.43) 0.13+- 11.29ms (11.16, 11.61) 0.15+- 116.00ms (109.77, 118.85) 3.00+-
evaluation 0.99x invariant, 0.03s (0.79d, 0.17p, 0.04std) 0.98x invariant, 2.57ms (0.46d, 0.42p, 4.61std) 1.00x invariant, 0.03s (0.29d, 0.61p, 0.08std) 0.95x noisy🤷‍♀️, 0.62ms (0.62d, 0.29p, 0.78std) 0.98x invariant, 2.47ms (0.94d, 0.11p, 2.59std)

@asinghvi17
Copy link
Member

This looks really useful, will probably let me move from manually constructing legends to simply specifying them in this way as well!

I'd rather we support key-value pairs (maybe NamedTuples?) instead of an override object, just because it is a lot easier to learn and reason about than yet another custom object...

@jkrumbiegel
Copy link
Member Author

I'd rather we support key-value pairs (maybe NamedTuples?

Yeah I guess I'll just convert internally to Attributes, I don't think the custom type is really necessary as long as we don't expand the label => something interface further so that one has to differentiate.

@jkrumbiegel jkrumbiegel closed this Oct 1, 2024
@jkrumbiegel jkrumbiegel reopened this Oct 1, 2024
@jkrumbiegel jkrumbiegel merged commit ad75383 into master Oct 1, 2024
34 checks passed
@jkrumbiegel jkrumbiegel deleted the jk/legend-overrides branch October 1, 2024 11:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Merged
Development

Successfully merging this pull request may close these issues.

4 participants