8
8
import numpy as np
9
9
10
10
from matplotlib import cbook , colors as mcolors
11
- from matplotlib .image import BboxImage
11
+ from matplotlib .image import AxesImage
12
12
import matplotlib .pyplot as plt
13
+ from matplotlib .transforms import Bbox , TransformedBbox , BboxTransformTo
13
14
14
15
15
16
class RibbonBox :
@@ -38,16 +39,17 @@ def get_stretched_image(self, stretch_factor):
38
39
self .im [self .cut_location :]])
39
40
40
41
41
- class RibbonBoxImage (BboxImage ):
42
+ class RibbonBoxImage (AxesImage ):
42
43
zorder = 1
43
44
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
46
48
self ._ribbonbox = RibbonBox (color )
49
+ self .set_transform (BboxTransformTo (bbox ))
47
50
48
51
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
51
53
52
54
ny = int (stretch_factor * self ._ribbonbox .nx )
53
55
if self .get_array () is None or self .get_array ().shape [0 ] != ny :
@@ -57,45 +59,35 @@ def draw(self, renderer, *args, **kwargs):
57
59
super ().draw (renderer , * args , ** kwargs )
58
60
59
61
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 ():
67
63
fig , ax = plt .subplots ()
68
64
69
65
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
+ ]
80
74
81
75
for year , h , bc in zip (years , heights , box_colors ):
82
76
bbox0 = Bbox .from_extents (year - 0.4 , 0. , year + 0.4 , h )
83
77
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" )
97
80
98
81
ax .set_xlim (years [0 ] - 0.5 , years [- 1 ] + 0.5 )
99
82
ax .set_ylim (0 , 10000 )
100
83
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
+
101
90
plt .show ()
91
+
92
+
93
+ main ()
0 commit comments