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

Missing zorder? #22

Open
jankaWIS opened this issue Aug 25, 2024 · 4 comments
Open

Missing zorder? #22

jankaWIS opened this issue Aug 25, 2024 · 4 comments

Comments

@jankaWIS
Copy link

Hi @mwshinn ,

Maybe I have missed it, but I cannot find zorder. How can I specify the order of items plot on canvas? Let's take the following example. I want to load and add an image and on top of it place text. I can do it by creating a separate unitname and then add the text there (middle image). But what if I want to add the text relative to the canvas like in the example of the left image? I still want the text above (on top of) the image which I'd do with zorder. How can I do it here?

from urllib.request import urlretrieve
urlretrieve("https://raw.githubusercontent.com/mwshinn/CanD/master/cand-logo.png", "_logo.png")

from cand import Canvas, Point, Vector

c = Canvas(20, 8, "cm")
c.add_text("Left image", Point(0.1, .8), weight="bold", size=13)
c.add_image("_logo.png", Point(1, 3, "cm"), ha="left", va="bottom", width=Vector(4, 0, "cm"))
c.add_image("_logo.png", Point(9, 4, "cm"), ha="center", va="center", height=Vector(0, 6, "cm"), unitname="middleimg")
c.add_rect(Point(-.05, -.05, "middleimg"), Point(1.05, 1.10, "middleimg"), fill=None, linewidth=3)
c.add_text("Middle image", Point(.5, 1.05, "middleimg"), weight="bold", size=12)

c.show()

image

@mwshinn
Copy link
Owner

mwshinn commented Aug 25, 2024

Right now, images are handled differently than other objects on the Canvas. This is because matplotlib has some issues with aliasing when displaying some images, and because it cannot efficiently show images encoded as pdfs without a raster conversion. So, all objects on the Canvas use the standard matplotlib zorder except the images. Images always use a higher zorder than everything else. Relative to each other, images added first are underneath images added later. (So in your example, the smaller logo would be below the bigger logo.) But both are on top of other objects. Unfortunately, including images in the same z ordering as other objects would require some pretty substantial changes in matplotlib before it could be supported this in CanD.

I will keep this open in case changes in matplotlib allow a better solution in the future.

@jankaWIS
Copy link
Author

I see, thanks, that is very unfortunate. Is there any way around this? You mentioned pdf and that it wouldn't work well either (if I understood you correctly), what about svg or other formats?

@mwshinn
Copy link
Owner

mwshinn commented Aug 26, 2024

If the image you want to display is a raster (not a pdf) and you don't care about antialiasing, you can create an axis and then use ax.imshow. E.g., based on your example,

from urllib.request import urlretrieve
urlretrieve("https://raw.githubusercontent.com/mwshinn/CanD/master/cand-logo.png", "_logo.png")

from cand import Canvas, Point, Vector
import imageio

c = Canvas(10, 8, "cm")

im = imageio.imread("_logo.png")
ax = c.add_axis("logoax", Point(1, 3, "cm"), Point(5, 7, "cm"))
ax.imshow(im)
ax.axis("off")
c.add_text("In front", Point(0.3, .8), weight="bold", size=13, zorder=100)
c.add_text("Behind", Point(0.3, .5), weight="bold", size=13, zorder=-100)

c.show()

@jankaWIS
Copy link
Author

That works great, thanks!

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

No branches or pull requests

2 participants