def segmentation_figure(label, cat, segfile):
    """
    Make a figure showing a cutout of the segmentation file
    """
    import matplotlib.pyplot as plt
    import numpy as np
    import astropy.io.fits as pyfits
    import astropy.wcs as pywcs
    from grizli import utils
    plt.ioff()
    seg = pyfits.open(segfile)
    seg_data = seg[0].data
    seg_wcs = pywcs.WCS(seg[0].header)
    np.random.seed(hash(label.split('_')[0]) % 10 ** 8)
    rnd_ids = np.append([0], np.argsort(np.random.rand(len(cat))) + 1)
    th = pyfits.open('{0}.thumb.fits'.format(label), mode='update')
    th_wcs = pywcs.WCS(th[0].header)
    blot_seg = utils.blot_nearest_exact(seg_data, seg_wcs, th_wcs, stepsize
        =-1, scale_by_pixel_area=False)
    rnd_seg = rnd_ids[np.cast[int](blot_seg)] * 1.0
    th_ids = np.unique(blot_seg)
    sh = th[0].data.shape
    yp, xp = np.indices(sh)
    thumb_height = 2.0
    fig = plt.figure(figsize=[thumb_height * sh[1] / sh[0], thumb_height])
    ax = fig.add_subplot(111)
    rnd_seg[rnd_seg == 0] = np.nan
    ax.imshow(rnd_seg, aspect='equal', cmap='terrain_r', vmin=-0.05 * len(
        cat), vmax=1.05 * len(cat))
    ax.set_xticklabels([])
    ax.set_yticklabels([])
    ix = utils.column_values_in_list(cat['number'], th_ids)
    xc, yc = th_wcs.all_world2pix(cat['ra'][ix], cat['dec'][ix], 0)
    xc = np.clip(xc, 0.09 * sh[1], 0.91 * sh[1])
    yc = np.clip(yc, 0.08 * sh[0], 0.92 * sh[0])
    for th_id, x_i, y_i in zip(cat['number'][ix], xc, yc):
        if th_id == 0:
            continue
        ax.text(x_i, y_i, '{0:.0f}'.format(th_id), ha='center', va='center',
            fontsize=8, color='w')
        ax.text(x_i, y_i, '{0:.0f}'.format(th_id), ha='center', va='center',
            fontsize=8, color='k', alpha=0.95)
    ax.set_xlim(0, sh[1] - 1)
    ax.set_ylim(0, sh[0] - 1)
    ax.set_axis_off()
    fig.tight_layout(pad=0.01)
    fig.savefig('{0}.seg.png'.format(label))
    plt.close(fig)
    seg_hdu = pyfits.ImageHDU(data=np.cast[int](blot_seg), name='SEG')
    if 'SEG' in th:
        th.pop('SEG')
    th.append(seg_hdu)
    th.writeto('{0}.thumb.fits'.format(label), overwrite=True,
        output_verify='fix')
    th.close()
