4
4
===============
5
5
6
6
"""
7
- import matplotlib . pyplot as plt
7
+
8
8
import numpy as np
9
- from matplotlib .image import BboxImage
10
9
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
14
13
15
14
16
- class RibbonBox ( object ) :
15
+ class RibbonBox :
17
16
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" ))
20
19
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 ]
24
23
nx = original_image .shape [1 ]
25
24
26
25
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 ])
37
29
38
30
def get_stretched_image (self , stretch_factor ):
39
31
stretch_factor = max (stretch_factor , 1 )
40
32
ny , nx , nch = self .im .shape
41
33
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 :]])
54
39
55
40
56
41
class RibbonBoxImage (BboxImage ):
57
42
zorder = 1
58
43
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 )
81
46
self ._ribbonbox = RibbonBox (color )
82
- self ._cached_ny = None
83
47
84
48
def draw (self , renderer , * args , ** kwargs ):
85
-
86
49
bbox = self .get_window_extent (renderer )
87
50
stretch_factor = bbox .height / bbox .width
88
51
89
52
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 :
91
54
arr = self ._ribbonbox .get_stretched_image (stretch_factor )
92
55
self .set_array (arr )
93
- self ._cached_ny = ny
94
56
95
- BboxImage .draw (self , renderer , * args , ** kwargs )
57
+ super () .draw (renderer , * args , ** kwargs )
96
58
97
59
98
- if 1 :
60
+ if True :
99
61
from matplotlib .transforms import Bbox , TransformedBbox
100
62
from matplotlib .ticker import ScalarFormatter
101
63
@@ -126,11 +88,8 @@ def draw(self, renderer, *args, **kwargs):
126
88
ax .annotate (r"%d" % (int (h / 100. )* 100 ),
127
89
(year , h ), va = "bottom" , ha = "center" )
128
90
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 ))
134
93
gradient [:, :, :3 ] = [1 , 1 , 0. ]
135
94
gradient [:, :, 3 ] = [[0.1 , 0.3 ], [0.3 , 0.5 ]] # alpha channel
136
95
patch_gradient .set_array (gradient )
@@ -139,5 +98,4 @@ def draw(self, renderer, *args, **kwargs):
139
98
ax .set_xlim (years [0 ] - 0.5 , years [- 1 ] + 0.5 )
140
99
ax .set_ylim (0 , 10000 )
141
100
142
- fig .savefig ('ribbon_box.png' )
143
101
plt .show ()
0 commit comments