Exemplo n.º 1
0
def heatmap(d, scale, vmin=None, vmax=None, cmap=None, ax=None,
            scientific=False, style='triangular', colorbar=True):
    """
    Plots heatmap of given color values.

    Parameters
    ----------
    d: dictionary
        A dictionary mapping the i, j polygon to the heatmap color, where
        i + j + k = scale.
    scale: Integer
        The scale used to partition the simplex.
    vmin: float, None
        The minimum color value, used to normalize colors. Computed if absent.
    vmax: float, None
        The maximum color value, used to normalize colors. Computed if absent.
    cmap: String or matplotlib.colors.Colormap, None
        The name of the Matplotlib colormap to use.
    ax: Matplotlib AxesSubplot, None
        The subplot to draw on.
    scientific: Bool, False
        Whether to use scientific notation for colorbar numbers.
    style: String, "triangular"
        The style of the heatmap, "triangular" or "hexagonal".
    colorbar: bool, True
        Show colorbar.

    Returns
    -------
    ax: The matplotlib axis
    """
    
    if not ax:
        fig, ax = pyplot.subplots()
    cmap = get_cmap(cmap)
    if not vmin:
        vmin = min(d.values())
    if not vmax:
        vmax = max(d.values())
    style = style.lower()[0]
    if style not in ["t", "h"]:
        raise ValueError("Heatmap style must be 'triangular' or 'hexagonal'")
    if style == "h":
        mapping_functions = [(hexagon_coordinates, d.items())]
    else:
        mapping_functions = [(triangle_coordinates, d.items()), (alt_triangle_coordinates, alt_value_iterator(d))]

    # Color data triangles or hexagons
    for vertex_function, iterator in mapping_functions:
        for key, value in iterator:
            if value is not None:
                i, j = key
                k = scale - i - j
                vertices = vertex_function(i, j, k)
                color = colormapper(value, vmin, vmax, cmap=cmap)
                # Matplotlib wants a list of xs and a list of ys
                xs, ys = unzip(vertices)
                ax.fill(xs, ys, facecolor=color, edgecolor=color)

    if colorbar:
        colorbar_hack(ax, vmin, vmax, cmap, scientific=scientific)
    return ax
Exemplo n.º 2
0
def svg_heatmap(data, scale, filename, vmax=None, vmin=None, style='h',
                permutation=None, cmap=None):
    """
    Create a heatmap in SVG format. Intended for use with very large datasets,
    which would require large amounts of RAM using matplotlib. You can convert
    the image to another format with e.g. ImageMagick:

    convert -density 1200 -resize -rotate 180 1000x1000 your.svg your.png

    Parameters
    ----------

    data: dictionary or k, v generator
        A dictionary mapping the i, j polygon to the heatmap color, where
        i + j + k = scale. If using a generator, style must be 'h'.
    scale: Integer
        The scale used to partition the simplex.
    filename: string
        The filename to write the SVG data to.
    vmin: float
        The minimum color value, used to normalize colors.
    vmax: float
        The maximum color value, used to normalize colors.
    cmap: String or matplotlib.colors.Colormap, None
        The name of the Matplotlib colormap to use.
    style: String, "h"
        The style of the heatmap, "triangular", "dual-triangular" or "hexagonal"
    permutation: string, None
        A permutation of the coordinates
    """

    style = style.lower()[0]
    if style not in ["t", "h", 'd']:
        raise ValueError("Heatmap style must be 'triangular', 'dual-triangular', or 'hexagonal'")

    if not isinstance(data, dict):
        if not style == 'h':
            raise ValueError, "Data can only be given as a generator for hexagonal style heatmaps because of blending for adjacent polygons."
        elif vmax is None or vmin is None:
            raise ValueError, "vmax and vmin must be supplied for data given as a generator."

    cmap = get_cmap(cmap)

    if not vmin:
        vmin = min(data.values())
    if not vmax:
        vmax = max(data.values())

    height = scale * numpy.sqrt(3) / 2 + 2

    output_file = open(filename, 'w')
    output_file.write('<svg height="%s" width="%s">\n' % (height, scale))


    vertices_values = polygon_generator(data, scale, style,
                                       permutation=permutation)

    # Draw the polygons and color them
    for vertices, value in vertices_values:
        color = colormapper(value, vmin, vmax, cmap=cmap)
        output_file.write(svg_polygon(vertices, color))

    output_file.write('</svg>\n')
Exemplo n.º 3
0
def svg_heatmap(data,
                scale,
                filename,
                vmax=None,
                vmin=None,
                style='h',
                permutation=None,
                cmap=None):
    """
    Create a heatmap in SVG format. Intended for use with very large datasets,
    which would require large amounts of RAM using matplotlib. You can convert
    the image to another format with e.g. ImageMagick:

    convert -density 1200 -resize -rotate 180 1000x1000 your.svg your.png

    Parameters
    ----------

    data: dictionary or k, v generator
        A dictionary mapping the i, j polygon to the heatmap color, where
        i + j + k = scale. If using a generator, style must be 'h'.
    scale: Integer
        The scale used to partition the simplex.
    filename: string
        The filename to write the SVG data to.
    vmin: float
        The minimum color value, used to normalize colors.
    vmax: float
        The maximum color value, used to normalize colors.
    cmap: String or matplotlib.colors.Colormap, None
        The name of the Matplotlib colormap to use.
    style: String, "h"
        The style of the heatmap, "triangular", "dual-triangular" or "hexagonal"
    permutation: string, None
        A permutation of the coordinates
    """

    style = style.lower()[0]
    if style not in ["t", "h", 'd']:
        raise ValueError(
            "Heatmap style must be 'triangular', 'dual-triangular', or 'hexagonal'"
        )

    if not isinstance(data, dict):
        if not style == 'h':
            raise ValueError, "Data can only be given as a generator for hexagonal style heatmaps because of blending for adjacent polygons."
        elif vmax is None or vmin is None:
            raise ValueError, "vmax and vmin must be supplied for data given as a generator."

    cmap = get_cmap(cmap)

    if not vmin:
        vmin = min(data.values())
    if not vmax:
        vmax = max(data.values())

    height = scale * numpy.sqrt(3) / 2 + 2

    output_file = open(filename, 'w')
    output_file.write('<svg height="%s" width="%s">\n' % (height, scale))

    vertices_values = polygon_generator(data,
                                        scale,
                                        style,
                                        permutation=permutation)

    # Draw the polygons and color them
    for vertices, value in vertices_values:
        color = colormapper(value, vmin, vmax, cmap=cmap)
        output_file.write(svg_polygon(vertices, color))

    output_file.write('</svg>\n')
Exemplo n.º 4
0
def heatmap(data, scale, vmin=None, vmax=None, cmap=None, ax=None,
            scientific=False, style='triangular', colorbar=True,
            permutation=None, colormap=True):
    """
    Plots heatmap of given color values.

    Parameters
    ----------
    data: dictionary
        A dictionary mapping the i, j polygon to the heatmap color, where
        i + j + k = scale.
    scale: Integer
        The scale used to partition the simplex.
    vmin: float, None
        The minimum color value, used to normalize colors. Computed if absent.
    vmax: float, None
        The maximum color value, used to normalize colors. Computed if absent.
    cmap: String or matplotlib.colors.Colormap, None
        The name of the Matplotlib colormap to use.
    ax: Matplotlib AxesSubplot, None
        The subplot to draw on.
    scientific: Bool, False
        Whether to use scientific notation for colorbar numbers.
    style: String, "triangular"
        The style of the heatmap, "triangular", "dual-triangular" or "hexagonal"
    colorbar: bool, True
        Show colorbar.
    permutation: string, None
        A permutation of the coordinates

    Returns
    -------
    ax: The matplotlib axis
    """

    if not ax:
        fig, ax = pyplot.subplots()
    # If not colormap, then make the RGBA values numpy arrays so that they can
    # be averaged.
    if not colormap:
        for k, v in data.items():
            data[k] = numpy.array(v)
    else:
        cmap = get_cmap(cmap)
        if not vmin:
            vmin = min(data.values())
        if not vmax:
            vmax = max(data.values())
    style = style.lower()[0]
    if style not in ["t", "h", 'd']:
        raise ValueError("Heatmap style must be 'triangular', 'dual-triangular', or 'hexagonal'")

    vertices_values = polygon_generator(data, scale, style,
                                       permutation=permutation)

    # Draw the polygons and color them
    for vertices, value in vertices_values:
        if value is None:
            continue
        if colormap:
            color = colormapper(value, vmin, vmax, cmap=cmap)
        else:
            color = value # rgba tuple (r,g,b,a) all in [0,1]
        # Matplotlib wants a list of xs and a list of ys
        xs, ys = unzip(vertices)
        ax.fill(xs, ys, facecolor=color, edgecolor=color)

    if colorbar and colormap:
        colorbar_hack(ax, vmin, vmax, cmap, scientific=scientific)
    return ax
Exemplo n.º 5
0
def heatmap(data,
            scale,
            vmin=None,
            vmax=None,
            cmap=None,
            ax=None,
            scientific=False,
            style='triangular',
            colorbar=True,
            permutation=None):
    """
    Plots heatmap of given color values.

    Parameters
    ----------
    data: dictionary
        A dictionary mapping the i, j polygon to the heatmap color, where
        i + j + k = scale.
    scale: Integer
        The scale used to partition the simplex.
    vmin: float, None
        The minimum color value, used to normalize colors. Computed if absent.
    vmax: float, None
        The maximum color value, used to normalize colors. Computed if absent.
    cmap: String or matplotlib.colors.Colormap, None
        The name of the Matplotlib colormap to use.
    ax: Matplotlib AxesSubplot, None
        The subplot to draw on.
    scientific: Bool, False
        Whether to use scientific notation for colorbar numbers.
    style: String, "triangular"
        The style of the heatmap, "triangular", "dual-triangular" or "hexagonal"
    colorbar: bool, True
        Show colorbar.
    permutation: string, None
        A permutation of the coordinates

    Returns
    -------
    ax: The matplotlib axis
    """

    if not ax:
        fig, ax = pyplot.subplots()
    cmap = get_cmap(cmap)
    if not vmin:
        vmin = min(data.values())
    if not vmax:
        vmax = max(data.values())
    style = style.lower()[0]
    if style not in ["t", "h", 'd']:
        raise ValueError(
            "Heatmap style must be 'triangular', 'dual-triangular', or 'hexagonal'"
        )

    vertices_values = polygon_generator(data,
                                        scale,
                                        style,
                                        permutation=permutation)

    # Draw the polygons and color them
    for vertices, value in vertices_values:
        if value is None:
            continue
        color = colormapper(value, vmin, vmax, cmap=cmap)
        # Matplotlib wants a list of xs and a list of ys
        xs, ys = unzip(vertices)
        ax.fill(xs, ys, facecolor=color, edgecolor=color)

    if colorbar:
        colorbar_hack(ax, vmin, vmax, cmap, scientific=scientific)
    return ax