44===============
55
66"""
7- import matplotlib . pyplot as plt
7+
88import numpy as np
9- from matplotlib .image import BboxImage
109
11- from matplotlib . _png import read_png
12- import matplotlib .colors
13- from matplotlib .cbook import get_sample_data
10+ from matplotlib import cbook , colors as mcolors
11+ from matplotlib .image import BboxImage
12+ import matplotlib .pyplot as plt
1413
1514
16- class RibbonBox ( object ) :
15+ class RibbonBox :
1716
18- original_image = read_png ( get_sample_data ( "Minduka_Present_Blue_Pack.png" ,
19- asfileobj = False ))
17+ original_image = plt . imread (
18+ cbook . get_sample_data ( "Minduka_Present_Blue_Pack.png" ))
2019 cut_location = 70
21- b_and_h = original_image [:, :, 2 ]
22- color = original_image [:, :, 2 ] - original_image [:, :, 0 ]
23- alpha = original_image [:, :, 3 ]
20+ b_and_h = original_image [:, :, 2 : 3 ]
21+ color = original_image [:, :, 2 : 3 ] - original_image [:, :, 0 : 1 ]
22+ alpha = original_image [:, :, 3 : 4 ]
2423 nx = original_image .shape [1 ]
2524
2625 def __init__ (self , color ):
27- rgb = matplotlib .colors .to_rgba (color )[:3 ]
28-
29- im = np .empty (self .original_image .shape ,
30- self .original_image .dtype )
31-
32- im [:, :, :3 ] = self .b_and_h [:, :, np .newaxis ]
33- im [:, :, :3 ] -= self .color [:, :, np .newaxis ] * (1 - np .array (rgb ))
34- im [:, :, 3 ] = self .alpha
35-
36- self .im = im
26+ rgb = mcolors .to_rgba (color )[:3 ]
27+ self .im = np .dstack (
28+ [self .b_and_h - self .color * (1 - np .array (rgb )), self .alpha ])
3729
3830 def get_stretched_image (self , stretch_factor ):
3931 stretch_factor = max (stretch_factor , 1 )
4032 ny , nx , nch = self .im .shape
4133 ny2 = int (ny * stretch_factor )
42-
43- stretched_image = np .empty ((ny2 , nx , nch ),
44- self .im .dtype )
45- cut = self .im [self .cut_location , :, :]
46- stretched_image [:, :, :] = cut
47- stretched_image [:self .cut_location , :, :] = \
48- self .im [:self .cut_location , :, :]
49- stretched_image [- (ny - self .cut_location ):, :, :] = \
50- self .im [- (ny - self .cut_location ):, :, :]
51-
52- self ._cached_im = stretched_image
53- return stretched_image
34+ return np .vstack (
35+ [self .im [:self .cut_location ],
36+ np .broadcast_to (
37+ self .im [self .cut_location ], (ny2 - ny , nx , nch )),
38+ self .im [self .cut_location :]])
5439
5540
5641class RibbonBoxImage (BboxImage ):
5742 zorder = 1
5843
59- def __init__ (self , bbox , color ,
60- cmap = None ,
61- norm = None ,
62- interpolation = None ,
63- origin = None ,
64- filternorm = 1 ,
65- filterrad = 4.0 ,
66- resample = False ,
67- ** kwargs
68- ):
69-
70- BboxImage .__init__ (self , bbox ,
71- cmap = cmap ,
72- norm = norm ,
73- interpolation = interpolation ,
74- origin = origin ,
75- filternorm = filternorm ,
76- filterrad = filterrad ,
77- resample = resample ,
78- ** kwargs
79- )
80-
44+ def __init__ (self , bbox , color , ** kwargs ):
45+ super ().__init__ (bbox , ** kwargs )
8146 self ._ribbonbox = RibbonBox (color )
82- self ._cached_ny = None
8347
8448 def draw (self , renderer , * args , ** kwargs ):
85-
8649 bbox = self .get_window_extent (renderer )
8750 stretch_factor = bbox .height / bbox .width
8851
8952 ny = int (stretch_factor * self ._ribbonbox .nx )
90- if self ._cached_ny != ny :
53+ if self .get_array () is None or self . get_array (). shape [ 0 ] != ny :
9154 arr = self ._ribbonbox .get_stretched_image (stretch_factor )
9255 self .set_array (arr )
93- self ._cached_ny = ny
9456
95- BboxImage .draw (self , renderer , * args , ** kwargs )
57+ super () .draw (renderer , * args , ** kwargs )
9658
9759
98- if 1 :
60+ if True :
9961 from matplotlib .transforms import Bbox , TransformedBbox
10062 from matplotlib .ticker import ScalarFormatter
10163
@@ -126,11 +88,8 @@ def draw(self, renderer, *args, **kwargs):
12688 ax .annotate (r"%d" % (int (h / 100. )* 100 ),
12789 (year , h ), va = "bottom" , ha = "center" )
12890
129- patch_gradient = BboxImage (ax .bbox ,
130- interpolation = "bicubic" ,
131- zorder = 0.1 ,
132- )
133- gradient = np .zeros ((2 , 2 , 4 ), dtype = float )
91+ patch_gradient = BboxImage (ax .bbox , interpolation = "bicubic" , zorder = 0.1 )
92+ gradient = np .zeros ((2 , 2 , 4 ))
13493 gradient [:, :, :3 ] = [1 , 1 , 0. ]
13594 gradient [:, :, 3 ] = [[0.1 , 0.3 ], [0.3 , 0.5 ]] # alpha channel
13695 patch_gradient .set_array (gradient )
@@ -139,5 +98,4 @@ def draw(self, renderer, *args, **kwargs):
13998 ax .set_xlim (years [0 ] - 0.5 , years [- 1 ] + 0.5 )
14099 ax .set_ylim (0 , 10000 )
141100
142- fig .savefig ('ribbon_box.png' )
143101 plt .show ()
0 commit comments