8000 Merge pull request #24279 from anntzer/bex · matplotlib/matplotlib@1b17c9e · GitHub
[go: up one dir, main page]

Skip to content

Commit 1b17c9e

Browse files
authored
Merge pull request #24279 from anntzer/bex
Cleanup BboxImage example.
2 parents 506b6e8 + 1bcf7b8 commit 1b17c9e

File tree

1 file changed

+9
-28
lines changed

1 file changed

+9
-28
lines changed

examples/images_contours_and_fields/demo_bboximage.py

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
a bounding box. This demo shows how to show an image inside a `.text.Text`'s
88
bounding box as well as how to manually create a bounding box for the image.
99
"""
10-
import matplotlib.pyplot as plt
10+
1111
import numpy as np
12+
13+
import matplotlib.pyplot as plt
1214
from matplotlib.image import BboxImage
1315
from matplotlib.transforms import Bbox, TransformedBbox
1416

@@ -19,24 +21,12 @@
1921
# Create a BboxImage with Text
2022
# ----------------------------
2123
txt = ax1.text(0.5, 0.5, "test", size=30, ha="center", color="w")
22-
kwargs = dict()
23-
24-
bbox_image = BboxImage(txt.get_window_extent,
25-
norm=None,
26-
origin=None,
27-
clip_on=False,
28-
**kwargs
29-
)
30-
a = np.arange(256).reshape(1, 256)/256.
31-
bbox_image.set_data(a)
32-
ax1.add_artist(bbox_image)
24+
ax1.add_artist(
25+
BboxImage(txt.get_window_extent, data=np.arange(256).reshape((1, -1))))
3326

3427
# ------------------------------------
3528
# Create a BboxImage for each colormap
3629
# ------------------------------------
37-
a = np.linspace(0, 1, 256).reshape(1, -1)
38-
a = np.vstack((a, a))
39-
4030
# List of all colormaps; skip reversed colormaps.
4131
cmap_names = sorted(m for m in plt.colormaps if not m.endswith("_r"))
4232

@@ -51,21 +41,12 @@
5141

5242
for i, cmap_name in enumerate(cmap_names):
5343
ix, iy = divmod(i, nrow)
54-
55-
bbox0 = Bbox.from_bounds(ix*dx*(1 + xpad_fraction),
56-
1. - iy*dy*(1 + ypad_fraction) - dy,
44+
bbox0 = Bbox.from_bounds(ix*dx*(1+xpad_fraction),
45+
1 - iy*dy*(1+ypad_fraction) - dy,
5746
dx, dy)
5847
bbox = TransformedBbox(bbox0, ax2.transAxes)
59-
60-
bbox_image = BboxImage(bbox,
61-
cmap=cmap_name,
62-
norm=None,
63-
origin=None,
64-
**kwargs
65-
)
66-
67-
bbox_image.set_data(a)
68-
ax2.add_artist(bbox_image)
48+
ax2.add_artist(
49+
BboxImage(bbox, cmap=cmap_name, data=np.arange(256).reshape((1, -1))))
6950

7051
plt.show()
7152

0 commit comments

Comments
 (0)
0