Beispiel #1
0
def polar_map(hist: Histogram2D,
              ax: Axes,
              *,
              show_zero: bool = True,
              show_colorbar: bool = True,
              **kwargs):
    """Polar map of polar histograms.

    Similar to map, but supports less parameters."""
    data = get_data(hist,
                    cumulative=False,
                    flatten=True,
                    density=kwargs.pop("density", False))

    cmap = _get_cmap(kwargs)
    norm, cmap_data = _get_cmap_data(data, kwargs)
    colors = cmap(cmap_data)

    rpos, phipos = (arr.flatten() for arr in hist.get_bin_left_edges())
    dr, dphi = (arr.flatten() for arr in hist.get_bin_widths())
    rmax, _ = (arr.flatten() for arr in hist.get_bin_right_edges())

    bar_args = {}
    if "zorder" in kwargs:
        bar_args["zorder"] = kwargs.pop("zorder")

    alphas = _get_alpha_data(cmap_data, kwargs)
    if np.isscalar(alphas):
        alphas = np.ones_like(data) * alphas

    for i in range(len(rpos)):
        if data[i] > 0 or show_zero:
            bin_color = colors[i]
            # TODO: align = "edge"
            bars = ax.bar(phipos[i],
                          dr[i],
                          width=dphi[i],
                          bottom=rpos[i],
                          align='edge',
                          color=bin_color,
                          edgecolor=kwargs.get("grid_color", cmap(0.5)),
                          lw=kwargs.get("lw", 0.5),
                          alpha=alphas[i],
                          **bar_args)

    ax.set_rmax(rmax.max())
    if show_colorbar:
        _add_colorbar(ax, cmap, cmap_data, norm)
Beispiel #2
0
def bar3d(h2: Histogram2D, ax: Axes3D, **kwargs):
    """Plot of 2D histograms as 3D boxes."""
    density = kwargs.pop("density", False)
    data = get_data(h2, cumulative=False, flatten=True, density=density)

    if "cmap" in kwargs:
        cmap = _get_cmap(kwargs)
        _, cmap_data = _get_cmap_data(data, kwargs)
        colors = cmap(cmap_data)
    else:
        colors = kwargs.pop("color", kwargs.pop("c", "blue"))

    xpos, ypos = (arr.flatten() for arr in h2.get_bin_centers())
    zpos = np.zeros_like(ypos)
    dx, dy = (arr.flatten() for arr in h2.get_bin_widths())

    _add_labels(ax, h2, kwargs)
    ax.bar3d(xpos, ypos, zpos, dx, dy, data, color=colors, **kwargs)
    ax.set_zlabel("density" if density else "frequency")
Beispiel #3
0
def map(h2: Histogram2D,
        ax: Axes,
        *,
        show_zero: bool = True,
        show_values: bool = False,
        show_colorbar: bool = True,
        x=None,
        y=None,
        **kwargs):
    """Coloured-rectangle plot of 2D histogram.

    Parameters
    ----------
    show_zero : Whether to show coloured box for bins with 0 frequency (otherwise background).
    show_values : Whether to show labels with frequencies/densities in the middle of the bin


    text_color : Optional
        Colour of text descriptions
    text_alpha : Optional[float]
        Alpha for the text labels only
    x : Optional[Callable]
        Transformation of x bin coordinates
    y : Optional[Callable]
        Transformation of y bin coordinates
    zorder : float
        z-order in the axis (higher number above lower)

    See Also
    --------
    image, polar_map, surface_map

    Notes
    -----
    If you transform axes using x or y parameters, the deduction of axis limits
    does not work well automatically. Please, make sure to attend to it yourself.
    The densities in transformed maps are calculated from original bins.
    """
    # Detect transformation
    transformed = False
    if x is not None or y is not None:
        if not x:
            x = lambda x, y: x
        if not y:
            y = lambda x, y: y
        transformed = True

    value_format = kwargs.pop("value_format", lambda x: str(x))
    # TODO: Implement correctly the text_kwargs

    if isinstance(value_format, str):
        format_str = "{0:" + value_format + "}"
        value_format = lambda x: format_str.format(x)

    rect_args = {}
    if "zorder" in kwargs:
        rect_args["zorder"] = kwargs.pop("zorder")

    data = get_data(h2,
                    cumulative=False,
                    flatten=True,
                    density=kwargs.pop("density", False))

    cmap = _get_cmap(kwargs)
    norm, cmap_data = _get_cmap_data(data, kwargs)
    colors = cmap(cmap_data)

    xpos, ypos = (arr.flatten() for arr in h2.get_bin_left_edges())
    dx, dy = (arr.flatten() for arr in h2.get_bin_widths())
    text_x, text_y = (arr.flatten() for arr in h2.get_bin_centers())

    _apply_xy_lims(ax, h2, data=data, kwargs=kwargs)
    _add_labels(ax, h2, kwargs)

    ax.autoscale_view()

    alphas = _get_alpha_data(cmap_data, kwargs)
    if np.isscalar(alphas):
        alphas = np.ones_like(data) * alphas

    for i in range(len(xpos)):
        bin_color = colors[i]
        alpha = alphas[i]

        if data[i] != 0 or show_zero:
            if not transformed:
                rect = plt.Rectangle([xpos[i], ypos[i]],
                                     dx[i],
                                     dy[i],
                                     facecolor=bin_color,
                                     edgecolor=kwargs.get(
                                         "grid_color", cmap(0.5)),
                                     lw=kwargs.get("lw", 0.5),
                                     alpha=alpha,
                                     **rect_args)
                tx, ty = text_x[i], text_y[i]

            else:
                # See http://matplotlib.org/users/path_tutorial.html
                points = ((xpos[i], ypos[i]), (xpos[i] + dx[i], ypos[i]),
                          (xpos[i] + dx[i], ypos[i] + dy[i]),
                          (xpos[i], ypos[i] + dy[i]), (xpos[i], ypos[i]))

                verts = [(x(*p), y(*p)) for p in points]

                codes = [
                    path.Path.MOVETO,
                    path.Path.LINETO,
                    path.Path.LINETO,
                    path.Path.LINETO,
                    path.Path.CLOSEPOLY,
                ]

                rect_path = path.Path(verts, codes)
                rect = patches.PathPatch(rect_path,
                                         facecolor=bin_color,
                                         edgecolor=kwargs.get(
                                             "grid_color", cmap(0.5)),
                                         lw=kwargs.get("lw", 0.5),
                                         alpha=alpha,
                                         **rect_args)

                tx = x(text_x[i], text_y[i])
                ty = y(text_x[i], text_y[i])
            ax.add_patch(rect)

            if show_values:
                text = value_format(data[i])
                yiq_y = np.dot(bin_color[:3], [0.299, 0.587, 0.114])

                text_color = kwargs.get("text_color", None)
                if not text_color:
                    if yiq_y > 0.5:
                        text_color = (0.0, 0.0, 0.0,
                                      kwargs.get("text_alpha", alpha))
                    else:
                        text_color = (1.0, 1.0, 1.0,
                                      kwargs.get("text_alpha", alpha))
                ax.text(tx,
                        ty,
                        text,
                        horizontalalignment='center',
                        verticalalignment='center',
                        color=text_color,
                        clip_on=True,
                        **rect_args)

    if show_colorbar:
        _add_colorbar(ax, cmap, cmap_data, norm)