|
7 | 7 | a bounding box. This demo shows how to show an image inside a `.text.Text`'s |
8 | 8 | bounding box as well as how to manually create a bounding box for the image. |
9 | 9 | """ |
10 | | -import matplotlib.pyplot as plt |
| 10 | + |
11 | 11 | import numpy as np |
| 12 | + |
| 13 | +import matplotlib.pyplot as plt |
12 | 14 | from matplotlib.image import BboxImage |
13 | 15 | from matplotlib.transforms import Bbox, TransformedBbox |
14 | 16 |
|
|
19 | 21 | # Create a BboxImage with Text |
20 | 22 | # ---------------------------- |
21 | 23 | 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)))) |
33 | 26 |
|
34 | 27 | # ------------------------------------ |
35 | 28 | # Create a BboxImage for each colormap |
36 | 29 | # ------------------------------------ |
37 | | -a = np.linspace(0, 1, 256).reshape(1, -1) |
38 | | -a = np.vstack((a, a)) |
39 | | - |
40 | 30 | # List of all colormaps; skip reversed colormaps. |
41 | 31 | cmap_names = sorted(m for m in plt.colormaps if not m.endswith("_r")) |
42 | 32 |
|
|
51 | 41 |
|
52 | 42 | for i, cmap_name in enumerate(cmap_names): |
53 | 43 | 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, |
57 | 46 | dx, dy) |
58 | 47 | 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)))) |
69 | 50 |
|
70 | 51 | plt.show() |
71 | 52 |
|
|
0 commit comments