8000 Update watermark example by timhoffm · Pull Request #25689 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Update watermark example #25689

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

Merged
merged 1 commit into from
May 26, 2023
Merged

Conversation

timhoffm
Copy link
Member
@timhoffm timhoffm commented Apr 14, 2023

Note: this replaces mpl-data/sample_data/logo2.png with the current logo. I think that's ok and we do not guarantee content stability of that data (internally, it's only used for the watermark example). But we could also add it as logo3.png at the cost of having 22kB more data.

old: https://matplotlib.org/3.7.1/gallery/images_contours_and_fields/watermark_image.html#sphx-glr-gallery-images-contours-and-fields-watermark-image-py

new:

grafik

@timhoffm timhoffm added this to the v3.8.0 milestone Apr 14, 2023
@story645
Copy link
Member
story645 commented Apr 16, 2023

I'm all for updating the logo, but I think it's much clearer what figimage is doing on the old one because the position is hard coded. I don't know that enough folks want the watermark in the middle like that (vs the edge) that it pays to show the magic to always have it in the middle.

Alternatively, maybe add a comment on how these two lines always center the image regardless of figsize?

Comment on lines 27 to 30
image_size = (542, 130)
nx, ny = fig.get_size_inches() * fig.dpi

fig.figimage(im, (nx - image_size[0]) / 2, (ny - image_size[1]) / 2, zorder=3, alpha=.7)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think figimage is buggy, so probably shouldn't be used at all? #23399

On my hi-dpi screen this is half size:

Watermark

Suggested change
image_size = (542, 130)
nx, ny = fig.get_size_inches() * fig.dpi
fig.figimage(im, (nx - image_size[0]) / 2, (ny - image_size[1]) / 2, zorder=3, alpha=.7)
axin = fig.add_axes([0, 0, 1, 1])
axin.axis('off')
axin.imshow(im, alpha=0.7)

Copy link
Member Author
@timhoffm timhoffm Apr 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit torn here. Overlaying an Axes and plotting the image into that feels like a hacksish workaround workaround. OTOH figimage has currently it's deficits. (Not only the dpi issue, but also API wise: One would want a proper positioning logic (horizontal/vertical align, bbox_to_anchor, sizing).

What I additionally don't like about the Axes/imshow approach is that you get sampling artifacts unless you make sure the image is exactly the same size as the figure.

I will rethink how to approach this. (and also consider @story645's remarks)

Copy link
Member
@jklymak jklymak Apr 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I additionally don't like about the Axes/imshow approach is that you get sampling artifacts unless you make sure the image is exactly the same size as the figure.

I'm not sure I understand what you are referring to here.

I don't personally consider placing Artists in an Axes rather than on a Figure a "hack". Almost all our artists are created on Axes, and I don't think that there is much to be gained by making figure-equivalent versions of most of those methods.

I do consider axin.axis to be a bit clunky, but so far as I know that is the one-line way to make the axes disappear.

If we were to add an API, it might be fig.figure_axes which just does ax = fig.add_axes([0, 0, 1, 1]); ax.axis('off') to give a figure-wide canvas.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I additionally don't like about the Axes/imshow approach is that you get sampling artifacts unless you make sure the image is exactly the same size as the figure.

I'm not sure I understand what you are referring to here.

figimage only gets the anchor point an then draws the image 1:1 on the pixels -> every image pixel appears 1:1 in the figure. In constrast, for imshow the image is scaled to the size of the Axes. This scaling creates artifacts. While scaling can be a nice feature, there should be a way to have images rendered 1:1 on the pixels.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a watermark example - I would have thought we would want the watermark to rescale if the figure changes size?

Figimage indeed does what it says, which is "Add a non-resampled image to the figure.", but the example doesn't properly query the dpi or create a figure that is the right size for the logo png. As a general sniff test, if an example needs fig.dpi it's likely to be incorrect responding to figure size changes or screen resolutions.

@timhoffm timhoffm marked this pull request as draft April 16, 2023 17:57
Note: this replaces mpl-data/sample_data/logo2.png with the current
logo. I think that's ok and we do not guarantee content stability of
that data (internally, it's only used for the watermark example). But
we could also add it as logo3.png at the cost of having 22kB more
data.
@timhoffm timhoffm force-pushed the watermark-example branch from c08d4dd to b18beae Compare May 18, 2023 22:00
@timhoffm
Copy link
Member Author
timhoffm commented May 18, 2023

To avoid any discussion on positioning and scaling behavior, I've reverted to a fixed position.

This is now only a stylistic update that uses the current logo (and more realistic looking data - who would plot girant circle markers on a sine?).

image

@timhoffm timhoffm marked this pull request as ready for review May 18, 2023 22:04
Copy link
Member
@story645 story645 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably out of scope, but were we trying to get people to use PIL instead of imread?

@timhoffm
Copy link
Member Author

Probably out of scope, but were we trying to get people to use PIL instead of imread?

Yes. There are multiple uses of imread() in examples. It makes more sense to convert all of them in one go.

@story645
Copy link
Member

opened an imread issue #25914

@QuLogic
Copy link
Member
QuLogic commented May 25, 2023

Strange that this old logo doesn't match the one in https://matplotlib.org/stable/users/project/history; I wonder when it was in use? And should it be moved there?

@timhoffm
Copy link
Member Author

@timhoffm
Copy link
Member Author

I take the liberty to merge based on @story645's approval.

@timhoffm timhoffm merged commit d3f8864 into matplotlib:main May 26, 2023
@timhoffm timhoffm deleted the watermark-example branch May 26, 2023 08:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0