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

add support for path_effects to overlay_ax #1

Merged
merged 1 commit into from
Aug 16, 2024
Merged

Conversation

Migelo
Copy link
Contributor

@Migelo Migelo commented Aug 7, 2024

When embedding the 2D colormap into the plot itself in full or just a part of it, it can happen that the labels and or ticks of the colormap are not visible due to being very similar in color to the picture itself. I fix this by allowing the user to pass path_effects to add an outline to the labels and ticks.

Here is an example of a problematic plot with $\phi$ not visible on the top and the fixed version below.
output

You can reproduce the figure above with my fork of the repo with this code snippet.

Happy to modify the functionality in any way :)
Looking forward to hearing your opinion @trygvrad !

import matplotlib.pyplot as plt
import colorstamps
import matplotlib.patheffects as PathEffects

img = colorstamps.helpers.get_random_data()
rgb, stamp = colorstamps.apply_stamp(
    img[:, :, 0],
    img[:, :, 1],
    "funnel",
    vmin_0=-1.2,
    vmax_0=1.2,
    vmin_1=-1,
    vmax_1=1,
)


fig, axes = plt.subplots(2)

# plot the original image
ax = axes[0]
ax.imshow(rgb)

# show colormap as overlay
overlaid_ax = stamp.overlay_ax(
    ax, lower_left_corner=[0.7, 0.85], width=0.2, path_effects=None
)
overlaid_ax.set_ylabel(r"$\phi$")
overlaid_ax.set_xlabel(r"$\omega$")


# plot the fix
ax = axes[1]
ax.imshow(rgb)

# add path effects to make text more readable
path_effects = [PathEffects.withStroke(linewidth=3, foreground="w")]

# show colormap as overlay
overlaid_ax = stamp.overlay_ax(
    ax, lower_left_corner=[0.7, 0.85], width=0.2, path_effects=path_effects
)
overlaid_ax.set_ylabel(r"$\phi$")
overlaid_ax.set_xlabel(r"$\omega$")

@trygvrad
Copy link
Owner

trygvrad commented Aug 9, 2024

@Migelo Thank you for the interest in this, I'll take a look and approve it soon.

In other news, I am trying to bring this functionality into matplotlib proper (see issue 14168, and PRs 28454, 28658, [28428]).
In this regard, it would be useful if you could make a comment on the issue (14168) describing your use case (astronomy/microscopy/etc.) and which colormaps from colorstamps you use (and why). That way we can make sure your use case is supported.

@Migelo
Copy link
Contributor Author

Migelo commented Aug 9, 2024

Great!

I am in astro and we sometimes use 2D colormaps to, in a single plot, show two properties of gas, e.g. density and temperature. I used the cut colormap, but I am not particularly attached to it and would be happy to use any better ones that you recommend.

@trygvrad trygvrad merged commit e29b381 into trygvrad:main Aug 16, 2024
@trygvrad
Copy link
Owner

I merged this and put a new wheel at pypi (0.1.3)
We should update the docs with a new example (based on the code you provided above).
You can make a new PR if you want, otherwise I will do it when I can find the time :)

@Migelo
Copy link
Contributor Author

Migelo commented Oct 1, 2024

Thanks!
Here is the pull request #3.

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.

2 participants