lets-plot icon indicating copy to clipboard operation
lets-plot copied to clipboard

geom_imshow and scale_y_reverse

Open poke1024 opened this issue 1 year ago • 3 comments

Combining geom_imshow with scale_y_reverse does not seem to work:

(
        ggplot()
        + scale_y_reverse()
        + geom_imshow(pixels, extent=[0, im.width, im.height, 0])
)

produces

Bildschirmfoto 2024-10-16 um 10 11 44

i.e. the image gets clipped at y = 0. Things work fine when removing scale_y_reverse, however without the reversed scale.

poke1024 avatar Oct 16 '24 08:10 poke1024

Unfortunately, geom_imshow() doesn't play well with positional scale/coord system transformations at the moment.

There is a workaround that you can try:

(
        ggplot()
        + scale_y_reverse()
        + geom_imshow(pixels, extent=[0, im.width, 0, -im.height])
        + ylim(0, im.height)
)

alshan avatar Oct 17 '24 15:10 alshan

@alshan Thank you for the workaround. This produces a full image with correct axes, but unfortunately the image is now flipped vertically, i.e. the mapping of y coordinate to pixel value is broken. Building on your suggestion, I was finally able to achieve what I wanted using:

ggplot() + geom_imshow(pixels,  extent=[0, im.width, -im.height, 0]) + ylim(0, -im.height)

That puts the pixel at pixels[0, 0] to the top left, and the y axis position there is 0.

poke1024 avatar Nov 07 '24 14:11 poke1024

Oh, great! I assumed your intention was to reverse the y-axis AND flip the image accordingly.

alshan avatar Nov 07 '24 19:11 alshan