8000 Merge pull request #14117 from anntzer/ribbon_box · matplotlib/matplotlib@e64b966 · GitHub
[go: up one dir, main page]

Skip to content

Commit e64b966

Browse files
authored
Merge pull request #14117 from anntzer/ribbon_box
Simplify ribbon_box example.
2 parents 1f22f41 + cc42926 commit e64b966

File tree

1 file changed

+28
-36
lines changed

1 file changed

+28
-36
lines changed

examples/misc/demo_ribbon_box.py

Lines changed: 28 additions & 36 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,35 @@ 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, 2800]
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")
85-
86-
ax.add_artist(rb_patch)
87-
88-
ax.annotate(r"%d" % (int(h/100.)*100),
89-
(year, h), va="bottom", ha="center")
90-
91-
patch_gradient = BboxImage(ax.bbox, interpolation="bicubic", zorder=0.1)
92-
gradient = np.zeros((2, 2, 4))
93-
gradient[:, :, :3] = [1, 1, 0.]
94-
gradient[:, :, 3] = [[0.1, 0.3], [0.3, 0.5]] # alpha channel
95-
patch_gradient.set_array(gradient)
96-
ax.add_artist(patch_gradient)
78+
ax.add_artist(RibbonBoxImage(ax, bbox, bc, interpolation="bicubic"))
79+
ax.annotate(str(h), (year, h), va="bottom", ha="center")
9780

9881
ax.set_xlim(years[0] - 0.5, years[-1] + 0.5)
9982
ax.set_ylim(0, 10000)
10083

84+
background_gradient = np.zeros((2, 2, 4))
85+
background_gradient[:, :, :3] = [1, 1, 0]
86+
background_gradient[:, :, 3] = [[0.1, 0.3], [0.3, 0.5]] # alpha channel
87+
ax.imshow(background_gradient, interpolation="bicubic", zorder=0.1,
88+
extent=(0, 1, 0, 1), transform=ax.transAxes, aspect="auto")
89+
10190
plt.show()
91+
92+
93+
main()

0 commit comments

Comments
 (0)
0