Skip to content

Add I() for literal values and panel-relative positions - #1124

Merged
has2k1 merged 6 commits into
mainfrom
asis-mapping
Sep 11, 2026
Merged

has2k1 merged 6 commits into
mainfrom
asis-mapping

Conversation

@has2k1

@has2k1 has2k1 commented Sep 11, 2026

Copy link
Copy Markdown
Owner

This PR adds I(), which marks an aesthetic value as literal.

Until now every aesthetic value went through a scale, so there was no way to hand a layer values to be used exactly as written. To put a label at 90% of the y axis you had to partly build the plot to compute what the limits would be, and work out the data coordinate yourself. Even with a helper function, it was still clanky.

Before

from plotnine import *
from plotinine.data import mtcars
from plotnine.helpers import get_aesthetic_limits

p = (
    ggplot(mpg, aes("displ", "hwy"))
    + geom_point()
)
ymin, ymax = get_aesthetic_limits(p, "y")
p + annotate("text", x=3, y=ymin + 0.9 * (ymax - ymin), label="90%", color="red")

Now

(
    ggplot(mpg, aes("displ", "hwy"))
    + geom_point()
    + annotate("text", x=3, y=I(0.9), label="90%", color="red")
)

closes #1008
closes #939

Let I() mark an aesthetic value as literal. Literal values bypass

scale training, mapping, and legends while retaining their ordinary

numeric or discrete classification.

Store the tag in the pandas dtype so dataframe operations preserve it.

Treat bare I() calls as literal expressions, while derived expressions

remain ordinary mappings and retain their labels.

Skip tagged columns during scale and guide processing so layers can

combine literal and mapped values for the same aesthetic.
Allow annotate() positions wrapped in I() to bypass position scales

and resolve as panel fractions. Repeat a length-one position to match

the other position vectors, including under faceting.
Resolve positions wrapped in I() against each panel after scales and

coordinate parameters are ready. Convert the resulting fractions to data

coordinates, including transformed-space behaviour and coord_flip

handling.
Measure panel fractions in the drawn coordinate space, then invert the

result before writing it to the data column.

Literal positions now appear at the visual centre under transformed

coordinates.
Document I() in the API reference and describe literal values and

panel-relative annotation positions in the changelog.
Add baseline images for literal colours, panel-relative annotations,

and coord_flip rendering.
@has2k1
has2k1 merged commit 019a08b into main Sep 11, 2026
13 of 14 checks passed
@has2k1
has2k1 deleted the asis-mapping branch September 11, 2026 14:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support for AsIs variables Relative positioning for annotations

1 participant