Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 6bb5a8f

Browse filesBrowse files
committed
Simplify ribbon_box example.
- Use AxesImage instead of the more obscure BboxImage. - Just hardcode the data instead of random-generating it and rounding it to hundreds when displaying. - Misc cleanups.
1 parent 983bbdd commit 6bb5a8f
Copy full SHA for 6bb5a8f

File tree

Expand file treeCollapse file tree

1 file changed

+28
-35
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+28
-35
lines changed

‎examples/misc/demo_ribbon_box.py

Copy file name to clipboardExpand all lines: examples/misc/demo_ribbon_box.py
+28-35Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
import numpy as np
99

1010
from matplotlib import cbook, colors as mcolors
11-
from matplotlib.image import BboxImage
11+
from matplotlib.image import AxesImage
1212
import matplotlib.pyplot as plt
13+
from matplotlib.transforms import Bbox, TransformedBbox, BboxTransformTo
1314

1415

1516
class RibbonBox:
@@ -38,16 +39,17 @@ def get_stretched_image(self, stretch_factor):
3839
self.im[self.cut_location:]])
3940

4041

41-
class RibbonBoxImage(BboxImage):
42+
class RibbonBoxImage(AxesImage):
4243
zorder = 1
4344

44-
def __init__(self, bbox, color, **kwargs):
45-
super().__init__(bbox, **kwargs)
45+
def __init__(self, ax, bbox, color, *, extent=(0, 1, 0, 1), **kwargs):
46+
super().__init__(ax, extent=extent, **kwargs)
47+
self._bbox = bbox
4648
self._ribbonbox = RibbonBox(color)
49+
self.set_transform(BboxTransformTo(bbox))
4750

4851
def draw(self, renderer, *args, **kwargs):
49-
bbox = self.get_window_extent(renderer)
50-
stretch_factor = bbox.height / bbox.width
52+
stretch_factor = self._bbox.height / self._bbox.width
5153

5254
ny = int(stretch_factor*self._ribbonbox.nx)
5355
if self.get_array() is None or self.get_array().shape[0] != ny:
@@ -57,45 +59,36 @@ def draw(self, renderer, *args, **kwargs):
5759
super().draw(renderer, *args, **kwargs)
5860

5961

60-
if True:
61-
from matplotlib.transforms import Bbox, TransformedBbox
62-
from matplotlib.ticker import ScalarFormatter
63-
64-
# Fixing random state for reproducibility
65-
np.random.seed(19680801)
66-
62+
def main():
6763
fig, ax = plt.subplots()
6864

6965
years = np.arange(2004, 2009)
70-
box_colors = [(0.8, 0.2, 0.2),
71-
(0.2, 0.8, 0.2),
72-
(0.2, 0.2, 0.8),
73-
(0.7, 0.5, 0.8),
74-
(0.3, 0.8, 0.7),
75-
]
76-
heights = np.random.random(years.shape) * 7000 + 3000
77-
78-
fmt = ScalarFormatter(useOffset=False)
79-
ax.xaxis.set_major_formatter(fmt)
66+
heights = [7900, 8100, 7900, 6900, 9800]
67+
box_colors = [
68+
(0.8, 0.2, 0.2),
69+
(0.2, 0.8, 0.2),
70+
(0.2, 0.2, 0.8),
71+
(0.7, 0.5, 0.8),
72+
(0.3, 0.8, 0.7),
73+
]
8074

8175
for year, h, bc in zip(years, heights, box_colors):
8276
bbox0 = Bbox.from_extents(year - 0.4, 0., year + 0.4, h)
8377
bbox = TransformedBbox(bbox0, ax.transData)
84-
rb_patch = RibbonBoxImage(bbox, bc, interpolation="bicubic")
78+
ax.add_artist(RibbonBoxImage(ax, bbox, bc, interpolation="bicubic"))
79+
ax.annotate(str(h), (year, h), va="bottom", ha="center")
8580

86-
ax.add_artist(rb_patch)
87-
88-
ax.annotate(r"%d" % (int(h/100.)*100),
89-
(year, h), va="bottom", ha="center")
81+
ax.set_xlim(years[0] - 0.5, years[-1] + 0.5)
82+
ax.set_ylim(0, 10000)
9083

91-
patch_gradient = BboxImage(ax.bbox, interpolation="bicubic", zorder=0.1)
9284
gradient = np.zeros((2, 2, 4))
93-
gradient[:, :, :3] = [1, 1, 0.]
85+
gradient[:, :, :3] = [1, 1, 0]
9486
gradient[:, :, 3] = [[0.1, 0.3], [0.3, 0.5]] # alpha channel
95-
patch_gradient.set_array(gradient)
96-
ax.add_artist(patch_gradient)
97-
98-
ax.set_xlim(years[0] - 0.5, years[-1] + 0.5)
99-
ax.set_ylim(0, 10000)
87+
ax.imshow(gradient, interpolation="bicubic", zorder=0.1,
88+
extent=(0, 1, 0, 1), transform=ax.transAxes)
89+
ax.set(aspect="auto")
10090

10191
plt.show()
92+
93+
94+
main()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.