Example #1
0
    heights = np.random.random(years.shape) * 7000 + 3000

    fmt = ScalarFormatter(useOffset=False)
    ax.xaxis.set_major_formatter(fmt)

    for year, h, bc in zip(years, heights, box_colors):
        bbox0 = Bbox.from_extents(year-0.4, 0., year+0.4, h)
        bbox = TransformedBbox(bbox0, ax.transData)
        rb_patch = RibbonBoxImage(bbox, bc, interpolation="bicubic")

        ax.add_artist(rb_patch)

        ax.annotate(r"%d" % (int(h/100.)*100),
                    (year, h), va="bottom", ha="center")

    patch_gradient = BboxImage(ax.bbox,
                               interpolation="bicubic",
                               zorder=0.1,
                               )
    gradient = np.zeros((2, 2, 4), dtype=np.float)
    gradient[:, :, :3] = [1, 1, 0.]
    gradient[:, :, 3] = [[0.1, 0.3], [0.3, 0.5]]  # alpha channel
    patch_gradient.set_array(gradient)
    ax.add_artist(patch_gradient)

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

    fig.savefig('ribbon_box.png')
    plt.show()
    fmt = ScalarFormatter(useOffset=False)
    ax.xaxis.set_major_formatter(fmt)

    for year, h, bc in zip(years, heights, box_colors):
        bbox0 = Bbox.from_extents(year - 0.4, 0., year + 0.4, h)
        bbox = TransformedBbox(bbox0, ax.transData)
        rb_patch = RibbonBoxImage(bbox, bc, interpolation="bicubic")

        ax.add_artist(rb_patch)

        ax.annotate(r"%d" % (int(h / 100.) * 100), (year, h),
                    va="bottom",
                    ha="center")

    patch_gradient = BboxImage(
        ax.bbox,
        interpolation="bicubic",
        zorder=0.1,
    )
    gradient = np.zeros((2, 2, 4), dtype=float)
    gradient[:, :, :3] = [1, 1, 0.]
    gradient[:, :, 3] = [[0.1, 0.3], [0.3, 0.5]]  # alpha channel
    patch_gradient.set_array(gradient)
    ax.add_artist(patch_gradient)

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

    fig.savefig('ribbon_box.png')
    plt.show()
def plotBar(ax=      None,
            xdata=   np.arange(4, 9),
            ydata=   np.random.random(5),
            labels=  None,
            colors=  'Random',
            figName= 'cilindro',
            path=    None):
    '''box_colors: 'random'|'blue'|'red'|'green'|'ligthgreen'|'darkblue'|'hsv'
    figure: redunca02|blue|aluminio|cilindro| 
    ''' 
    try:
        IMAGESPATH= os.path.join(wx.GetApp().installDir, 'nicePlot','images')
    except:
        path1= sys.argv[0]
        if os.path.isfile(path1):
            path1= os.path.split(path1)[0]
        path1= path1.decode( sys.getfilesystemencoding())
        if os.path.split( path1)[-1] == 'nicePlot':
            IMAGESPATH= os.path.join( path1, 'images')
        else:
            IMAGESPATH= os.path.join( path1, 'nicePlot', 'images')
        
    if len(xdata) != len(ydata):
        raise StandardError('xdata and ydata must have the same len()')
    
    if isinstance(figName,(str,unicode)):
        figName = [figName.lower() for i in range(len(xdata))]
    else:
        figName = [fig.lower() for fig in figName]
    ##xdata,ydata = orderData(xdata,ydata)
    # se generan los colores en forma aleatoria
    box_colors = _generatecolors(colors,len(xdata))           
    fig = plt.gcf()
    if ax == None:
        ax = plt.gca()
    fmt = ScalarFormatter(useOffset= True) #False
    ax.xaxis.set_major_formatter(fmt)
    if labels== None:
        labels = [None for i in ydata]
        
    if path == None:
        path= os.path.relpath(os.path.join(IMAGESPATH,'barplot'))

    for year, h, bc,label,figi in zip(xdata, ydata, box_colors,labels,figName):
        if h < 0: continue
        bbox0 = Bbox.from_extents(year-0.5, 0., year+0.5, h) # year-0.4, 0., year+0.4,
        bbox = TransformedBbox(bbox0, ax.transData)
        rb_patch = _RibbonBoxImage(bbox, bc, figi, path, interpolation='bicubic') #bicubic
        ax.add_artist(rb_patch)
        if isinstance(label,(str,unicode)):
            ax.annotate(label, (year, h), va="bottom", ha="center")
            if type(labels) == type(1):
                ax.annotate(r"%d" % labels,
                        (year, labels), va="bottom", ha="center")
            elif type(labels) == type(1.1):
                ax.annotate(r"%f" % labels,
                        (year, labels), va="bottom", ha="center")
            elif str(type(labels)) == "<type 'numpy.int32'>":
                ax.annotate(r"%d" % labels,
                        (year, labels), va="bottom", ha="center")
                
    patch_gradient = BboxImage(ax.bbox,
                               interpolation= 'bicubic', # "bicubic"
                               zorder=0.1,
                               )
    gradient = np.zeros((2, 2, 4), dtype=np.float)
    gradient[:,:,:3] = [1, 1, 1]
    #gradient[:,:,:3] = [0.5, 0.5, 0.5]
    gradient[:,:,3] = [[0.2, 0.3],[0.2, 0.5]] # alpha channel
    patch_gradient.set_array(gradient)
    ax.add_artist(patch_gradient)
    ax.set_xlim( min(xdata)-0.5, max(xdata)+0.5)
    # se determinan los limites para el eje Y
    if max(ydata) > 0.0:
        maxYlimit = max(ydata)*1.05
    elif max(ydata) == 0.0:
        maxYlimit = 1.0
    else:
        maxYlimit = max(ydata)*(1-0.05)
    ax.set_ylim(min(ydata)*(-0.05), maxYlimit)
    return (fig, plt)
def plothist(ax=      None,
             xdata= np.arange(4, 9),
             ydata= np.random.random(5),
             labels= [],
             colors = 'random',
             figName='redunca03'):
    if len(xdata) != len(ydata):
        raise StandardError('xdata and ydata must have the same len()')
    
    try:
        IMAGESPATH= os.path.join(wx.GetApp().installDir, 'nicePlot','images')
    except:
        path1= sys.argv[0]
        if os.path.isfile(path1):
            path1= os.path.split(path1)[0]
        path1= path1.decode( sys.getfilesystemencoding())
        if os.path.split( path1)[-1] == 'nicePlot':
            IMAGESPATH= os.path.join( path1, 'images')
        else:
            IMAGESPATH= os.path.join( path1, 'nicePlot', 'images')
           
    # se generan los colores en forma aleatoria
    box_colors = _generatecolors( colors,len( xdata))
    # se genera la figura
    fig = plt.gcf()
    if ax == None:
        ax = plt.gca()
    fmt = ScalarFormatter( useOffset=False)
    ax.xaxis.set_major_formatter( fmt)
    for year, h, bc in zip( xdata, ydata, box_colors):
        bbox0 = Bbox.from_extents(year-0.5, 0.0, year+0.5, h) # year-0.48, 0., year+0.48, year-1 year+1
        bbox = TransformedBbox(bbox0, ax.transData)
        rb_patch = _RibbonBoxImage(bbox, bc, figName, 
                                   path=  os.path.join(IMAGESPATH, 'histplot'),
                                   interpolation="bicubic")
        ax.add_artist(rb_patch)
        if 0:
            if type(h) == type(1):
                ax.annotate(r"%d" % h,
                        (year, h), va="bottom", ha="center")
            elif type(h) == type(1.1):
                ax.annotate(r"%f" % h,
                        (year, h), va="bottom", ha="center")
            elif str(type(h)) == "<type 'numpy.int32'>":
                ax.annotate(r"%d" % h,
                        (year, h), va="bottom", ha="center")
    patch_gradient = BboxImage( ax.bbox,
                               interpolation="bicubic",
                               zorder=0.1,
                               )
    gradient = np.zeros( (2, 2, 4), dtype=np.float)
    gradient[:,:,:3] = [1, 1, 1]
    gradient[:,:,3] = [[0.2, 0.3],[0.2, 0.5]] # alpha channel
    patch_gradient.set_array( gradient)
    ax.add_artist( patch_gradient)
    ax.set_xlim( xdata[0]-1.0, xdata[-1]+1.0)
    # se determinan los limites para el eje Y
    try:
        ydatamax = ydata.max() # en el caso de informacion proveniente de numpy
    except AttributeError:
        ydatamax = max(ydata)
    if ydatamax > 0.0:
        maxYlimit = ydatamax*1.05
    elif ydatamax == 0.0:
        maxYlimit = 1.0
    else:
        maxYlimit = ydatamax*(1.0-0.05)
    ax.set_ylim( 0, maxYlimit)
    return ( fig,plt)
Example #5
0
                                                                                                                                                                                                                                                                    def draw(self, renderer, *args, **kwargs):

                                                                                                                                                                                                                                                                                bbox = self.get_window_extent(renderer)
                                                                                                                                                                                                                                                                                        stretch_factor = bbox.height / bbox.width

                                                                                                                                                                                                                                                                                                ny = int(stretch_factor*self._ribbonbox.nx)
                                                                                                                                                                                                                                                                                                        if self._cached_ny != ny:
                                                                                                                                                                                                                                                                                                                        arr = self._ribbonbox.get_stretched_image(stretch_factor)
                                                                                                                                                                                                                                                                                                                                    self.set_array(arr)
                                                                                                                                                                                                                                                                                                                                                self._cached_ny = ny

                                                                                                                                                                                                                                                                                                                                                        BboxImage.draw(self, renderer, *args, **kwargs)


                                                                                                                                                                                                                                                                                                                                                        if 1:
                                                                                                                                                                                                                                                                                                                                                                from matplotlib.transforms import Bbox, TransformedBbox
                                                                                                                                                                                                                                                                                                                                                                    from matplotlib.ticker import ScalarFormatter

                                                                                                                                                                                                                                                                                                                                                                        fig, ax = plt.subplots()

                                                                                                                                                                                                                                                                                                                                                                            years = np.arange(2004, 2009)
                                                                                                                                                                                                                                                                                                                                                                                box_colors = [(0.8, 0.2, 0.2),
                                                                                                                                                                                                                                                                                                                                                                                                          (0.2, 0.8, 0.2),
                                                                                                                                                                                                                                                                                                                                                                                                                            (0.2, 0.2, 0.8),
                                                                                                                                                                                                                                                                                                                                                                                                                                              (0.7, 0.5, 0.8),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                (0.3, 0.8, 0.7),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  ]
                                                                                                                                                                                                                                                                                                                                                                                    heights = np.random.random(years.shape) * 7000 + 3000

                                                                                                                                                                                                                                                                                                                                                                                        fmt = ScalarFormatter(useOffset=False)
                                                                                                                                                                                                                                                                                                                                                                                            ax.xaxis.set_major_formatter(fmt)

                                                                                                                                                                                                                                                                                                                                                                                                for year, h, bc in zip(years, heights, box_colors):
                                                                                                                                                                                                                                                                                                                                                                                                            bbox0 = Bbox.from_extents(year-0.4, 0., year+0.4, h)
                                                                                                                                                                                                                                                                                                                                                                                                                    bbox = TransformedBbox(bbox0, ax.transData)
                                                                                                                                                                                                                                                                                                                                                                                                                            rb_patch = RibbonBoxImage(bbox, bc, interpolation="bicubic")

                                                                                                                                                                                                                                                                                                                                                                                                                                    ax.add_artist(rb_patch)

                                                                                                                                                                                                                                                                                                                                                                                                                                            ax.annotate(r"%d" % (int(h/100.)*100),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                        (year, h), va="bottom", ha="center")

                                                                                                                                                                                                                                                                                                                                                                                                                                                patch_gradient = BboxImage(ax.bbox,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       interpolation="bicubic",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      zorder=0.1,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     )
                                                                                                                                                                                                                                                                                                                                                                                                                                                    gradient = np.zeros((2, 2, 4), dtype=np.float)
                                                                                                                                                                                                                                                                                                                                                                                                                                                        gradient[:,:,:3] = [1, 1, 0.]
                                                                                                                                                                                                                                                                                                                                                                                                                                                            gradient[:,:,3] = [[0.1, 0.3],[0.3, 0.5]] # alpha channel
                                                                                                                                                                                                                                                                                                                                                                                                                                                                patch_gradient.set_array(gradient)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                    ax.add_artist(patch_gradient)


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

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                fig.savefig('ribbon_box.png')
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    plt.show()