8000 Cleanup BboxImage example. by anntzer · Pull Request #24279 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Cleanup BboxImage example. #24279

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
Nov 2, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 9 additions & 28 deletions examples/images_contours_and_fields/demo_bboximage.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
a bounding box. This demo shows how to show an image inside a `.text.Text`'s
bounding box as well as how to manually create a bounding box for the image.
"""
import matplotlib.pyplot as plt

import numpy as np

import matplotlib.pyplot as plt
from matplotlib.image import BboxImage
from matplotlib.transforms import Bbox, TransformedBbox

Expand All @@ -19,24 +21,12 @@
# Create a BboxImage with Text
# ----------------------------
txt = ax1.text(0.5, 0.5, "test", size=30, ha="center", color="w")
kwargs = dict()

bbox_image = BboxImage(txt.get_window_extent,
norm=None,
origin=None,
clip_on=False,
**kwargs
)
a = np.arange(256).reshape(1, 256)/256.
bbox_image.set_data(a)
ax1.add_artist(bbox_image)
ax1.add_artist(
BboxImage(txt.get_window_extent, data=np.arange(256).reshape((1, -1))))

# ------------------------------------
# Create a BboxImage for each colormap
# ------------------------------------
a = np.linspace(0, 1, 256).reshape(1, -1)
a = np.vstack((a, a))

# List of all colormaps; skip reversed colormaps.
cmap_names = sorted(m for m in plt.colormaps if not m.endswith("_r"))

Expand All @@ -51,21 +41,12 @@

for i, cmap_name in enumerate(cmap_names):
ix, iy = divmod(i, nrow)

bbox0 = Bbox.from_bounds(ix*dx*(1 + xpad_fraction),
1. - iy*dy*(1 + ypad_fraction) - dy,
bbox0 = Bbox.from_bounds(ix*dx*(1+xpad_fraction),
1 - iy*dy*(1+ypad_fraction) - dy,
dx, dy)
bbox = TransformedBbox(bbox0, ax2.transAxes)

bbox_image = BboxImage(bbox,
cmap=cmap_name,
norm=None,
origin=None,
**kwargs
)

bbox_image.set_data(a)
ax2.add_artist(bbox_image)
ax2.add_artist(
BboxImage(bbox, cmap=cmap_name, data=np.arange(256).reshape((1, -1))))

plt.show()

Expand Down
0