|
| 1 | +import matplotlib.pyplot as plt |
| 2 | +import numpy as np |
| 3 | +from matplotlib.image import BboxImage |
| 4 | + |
| 5 | +from matplotlib._png import read_png |
| 6 | +import matplotlib.colors |
| 7 | +from matplotlib.cbook import get_sample_data |
| 8 | + |
| 9 | +class RibbonBox(object): |
| 10 | + |
| 11 | + original_image = read_png(get_sample_data("Minduka_Present_Blue_Pack.png", |
| 12 | + asfileobj=False)) |
| 13 | + cut_location = 70 |
| 14 | + b_and_h = original_image[:,:,2] |
| 15 | + color = original_image[:,:,2] - original_image[:,:,0] |
| 16 | + alpha = original_image[:,:,3] |
| 17 | + nx = original_image.shape[1] |
| 18 | + |
| 19 | + def __init__(self, color): |
| 20 | + rgb = matplotlib.colors.colorConverter.to_rgb(color) |
| 21 | + |
| 22 | + im = np.empty(self.original_image.shape, |
| 23 | + self.original_image.dtype) |
| 24 | + |
| 25 | + |
| 26 | + im[:,:,:3] = self.b_and_h[:,:,np.newaxis] |
| 27 | + im[:,:,:3] -= self.color[:,:,np.newaxis]*(1.-np.array(rgb)) |
| 28 | + im[:,:,3] = self.alpha |
| 29 | + |
| 30 | + self.im = im |
| 31 | + |
| 32 | + |
| 33 | + def get_stretched_image(self, stretch_factor): |
| 34 | + stretch_factor = max(stretch_factor, 1) |
| 35 | + ny, nx, nch = self.im.shape |
| 36 | + ny2 = int(ny*stretch_factor) |
| 37 | + |
| 38 | + stretched_image = np.empty((ny2, nx, nch), |
| 39 | + self.im.dtype) |
| 40 | + cut = self.im[self.cut_location,:,:] |
| 41 | + stretched_image[:,:,:] = cut |
| 42 | + stretched_image[:self.cut_location,:,:] = \ |
| 43 | + self.im[:self.cut_location,:,:] |
| 44 | + stretched_image[-(ny-self.cut_location):,:,:] = \ |
| 45 | + self.im[-(ny-self.cut_location):,:,:] |
| 46 | + |
| 47 | + self._cached_im = stretched_image |
| 48 | + return stretched_image |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | +class RibbonBoxImage(BboxImage): |
| 53 | + zorder = 1 |
| 54 | + |
| 55 | + def __init__(self, bbox, color, |
| 56 | + cmap = None, |
| 57 | + norm = None, |
| 58 | + interpolation=None, |
| 59 | + origin=None, |
| 60 | + filternorm=1, |
| 61 | + filterrad=4.0, |
| 62 | + resample = False, |
| 63 | + **kwargs |
| 64 | + ): |
| 65 | + |
| 66 | + BboxImage.__init__(self, bbox, |
| 67 | + cmap = None, |
| 68 | + norm = None, |
| 69 | + interpolation=None, |
| 70 | + origin=None, |
| 71 | + filternorm=1, |
| 72 | + filterrad=4.0, |
| 73 | + resample = False, |
| 74 | + **kwargs |
| 75 | + ) |
| 76 | + |
| 77 | + self._ribbonbox = RibbonBox(color) |
| 78 | + self._cached_ny = None |
| 79 | + |
| 80 | + |
| 81 | + def draw(self, renderer, *args, **kwargs): |
| 82 | + |
| 83 | + bbox = self.get_window_extent(renderer) |
| 84 | + stretch_factor = bbox.height / bbox.width |
| 85 | + |
| 86 | + ny = int(stretch_factor*self._ribbonbox.nx) |
| 87 | + if self._cached_ny != ny: |
| 88 | + arr = self._ribbonbox.get_stretched_image(stretch_factor) |
| 89 | + self.set_array(arr) |
| 90 | + self._cached_ny = ny |
| 91 | + |
| 92 | + BboxImage.draw(self, renderer, *args, **kwargs) |
| 93 | + |
| 94 | + |
| 95 | +if 1: |
| 96 | + from matplotlib.transforms import Bbox, TransformedBbox |
| 97 | + from matplotlib.ticker import ScalarFormatter |
| 98 | + |
| 99 | + fig = plt.gcf() |
| 100 | + fig.clf() |
| 101 | + ax = plt.subplot(111) |
| 102 | + |
| 103 | + years = np.arange(2004, 2009) |
| 104 | + box_colors = [(0.8, 0.2, 0.2), |
| 105 | + (0.2, 0.8, 0.2), |
| 106 | + (0.2, 0.2, 0.8), |
| 107 | + (0.7, 0.5, 0.8), |
| 108 | + (0.3, 0.8, 0.7), |
| 109 | + ] |
| 110 | + heights = np.random.random(years.shape) * 7000 + 3000 |
| 111 | + |
| 112 | + fmt = ScalarFormatter(useOffset=False) |
| 113 | + ax.xaxis.set_major_formatter(fmt) |
| 114 | + |
| 115 | + for year, h, bc in zip(years, heights, box_colors): |
| 116 | + bbox0 = Bbox.from_extents(year-0.4, 0., year+0.4, h) |
| 117 | + bbox = TransformedBbox(bbox0, ax.transData) |
| 118 | + rb_patch = RibbonBoxImage(bbox, bc) |
| 119 | + |
| 120 | + ax.add_artist(rb_patch) |
| 121 | + |
| 122 | + ax.annotate(r"%d" % (int(h/100.)*100), |
| 123 | + (year, h), va="bottom", ha="center") |
| 124 | + |
| 125 | + patch_gradient = BboxImage(ax.bbox, |
| 126 | + interpolation="bicubic", |
| 127 | + zorder=0.1, |
| 128 | + ) |
| 129 | + gradient = np.zeros((2, 2, 4), dtype=np.float) |
| 130 | + gradient[:,:,:3] = [1, 1, 0.] |
| 131 | + gradient[:,:,3] = [[0.1, 0.3],[0.3, 0.5]] # alpha channel |
| 132 | + patch_gradient.set_array(gradient) |
| 133 | + ax.add_artist(patch_gradient) |
| 134 | + |
| 135 | + |
| 136 | + ax.set_xlim(years[0]-0.5, years[-1]+0.5) |
| 137 | + ax.set_ylim(0, 10000) |
| 138 | + |
| 139 | + plt.show() |
| 140 | + |
0 commit comments