Skip to content

Annotate recipe #4891

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

Draft
wants to merge 23 commits into
base: master
Choose a base branch
from
Draft

Annotate recipe #4891

wants to merge 23 commits into from

Conversation

jkrumbiegel
Copy link
Member

@jkrumbiegel jkrumbiegel commented Mar 25, 2025

This PR adds an annotate recipe which is a sort of hybrid between ggrepel, which annotates a bunch of points with labels that should avoid each other, and matplotlib-style annotations where arrows can be drawn from text labels to targets with lots of style options.

If an array of labels is given with no explicit text positions, labels are iteratively moved in order to overlap less with each other and the data points:

using Random

Random.seed!(123)
n = 30
points = randn(Point2f, n)
f, ax, sc = scatter(points)

ann = annotate!(ax, points;
    text = string.(rand(names(Base), n)),
    style = Ann.Styles.Line(),
)

f
image

Text positions can be given in data space or in offset space (default) so that it's easy to move the text just some visual distance away from the annotated thing.

f, ax, _ = lines(0..4, x -> cos(6x) * exp(x))
annotate!(ax, 2.1, 8, 1, 20, text = "local maximum", path = Ann.Paths.Arc(0.3), style = Ann.Styles.LineArrow(),
    labelspace = :data)
annotate!(ax, 2.65, -15, -50, -50,
    text = "local minimum", path = Ann.Paths.Arc(-0.3), style = Ann.Styles.LineArrow(
        head = Ann.Arrows.Head(),
    )
)
ax.xgridvisible = false
ax.ygridvisible = false
f
image
f, ax, _ = lines(0..10, sin)

annotate!(ax, pi/2, 1.0, 0, -100, text = "Peak", style = Ann.Styles.LineArrow())
annotate!(ax, 3pi/2, -1.0, 0, 100, text = "Trough", style = Ann.Styles.LineArrow())
annotate!(ax, 5pi/2, 1.0, -100, 0, text = "Second Peak", style = Ann.Styles.LineArrow(),
    path = Ann.Paths.Arc(-0.7),
    )

f
image

@ffreyer Ideally we could combine this with the textlabel recipe from #4879 somehow so that each label could have a background shape as well?

@github-project-automation github-project-automation bot moved this to Work in progress in PR review Mar 25, 2025
@jkrumbiegel jkrumbiegel mentioned this pull request Mar 25, 2025
18 tasks
@MakieBot
Copy link
Collaborator

MakieBot commented Mar 25, 2025

Benchmark Results

SHA: ef03e1f9b26c857f9f894fb2112375e162d2c07f

Warning

These results are subject to substantial noise because GitHub's CI runs on shared machines that are not ideally suited for benchmarking.

GLMakie
CairoMakie
WGLMakie

@briochemc
Copy link
Contributor

I'd love this! Any reason preventing merging this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Work in progress
Development

Successfully merging this pull request may close these issues.

3 participants