Пример #1
0
#-----global.flag
web_get001txtFg = False  # @zt_web.web_get001txtFg

#-----bs4.findall
bs_get_ktag_kstr = ''

#--colors
#10,prism,brg,dark2,hsv,jet
#10,,hot,Vega10,Vega20
cors_brg = cm.brg(np.linspace(0, 1, 10))
cors_hot = cm.hot(np.linspace(0, 1, 10))
cors_hsv = cm.hsv(np.linspace(0, 1, 10))
cors_jet = cm.jet(np.linspace(0, 1, 10))
cors_prism = cm.prism(np.linspace(0, 1, 10))
cors_Dark2 = cm.Dark2(np.linspace(0, 1, 10))
cors_Vega10 = cm.Vega10(np.linspace(0, 1, 10))
cors_Vega20 = cm.Vega20(np.linspace(0, 1, 10))

#------str.xxx
sgnSP4 = '    '
sgnSP8 = sgnSP4 + sgnSP4

#-----FN.xxx
logFN = ''

#--------------dir
#
rdat0 = 'zDat/'
rdatCN = rdat0 + "cn/day/"
rdatCNX = rdat0 + "cn/xday/"
rdatInx = rdat0 + "inx/"
Пример #2
0
def stacked_bar_plot(data, labels, legend=None, table_header=None, title=None,
                     ax_names=None, normalize=False, normalize_factor=None):
    """
    Creates a tight stacked bar plot
    :param data: list, data for 2 groups should be like [[1,2], [2,3]]
    :param labels: list, should match the number of items in data.
    """

    plt.rcParams["figure.figsize"] = (8, 6)

    if normalize:

        data_original = np.copy(data)
        data = np.array([[y / normalize_factor for y in x] for x in data])

    if len(labels) > 10:
        plt.rcParams["figure.figsize"] = (len(labels) / 3, 6)
    else:
        plt.rcParams["figure.figsize"] = (8, 6)

    # Use ggpot style
    plt.style.use("ggplot")

    fig, ax = plt.subplots()

    # Get bottom positions for stacked bar
    bottoms = np.cumsum(np.vstack((np.zeros(data.shape[1]), data)),
                        axis=0)[:-1]

    w = .8

    # Get x positions
    xpos = [x + (w / 2) for x in range(len(labels))]

    for c, d in enumerate(data):

        if len(data) <= 10:
            c1 = c if c < 9 else c - 10
            clr = cm.Vega10(c1, 1)
        else:
            c1 = c if c < 19 else c - 20
            clr = cm.Vega20c(c1, 1)

        if c == 0:
            bplot = ax.bar(xpos, d, w, color=clr, label=legend[c], alpha=.9)
        else:
            bplot = ax.bar(xpos, d, w, color=clr, label=legend[c], alpha=.9,
                           bottom=bottoms[c])

    # Set x labels
    plt.xticks([x + (w / 2) for x in xpos], labels, ha="right", rotation=45)

    # Set legend
    if legend:
        if len(legend) <= 4:
            cols = 1
        else:
            cols = len(legend) if len(legend) < 3 else 3
        borderpad = cols * -6
        lgd = plt.legend(loc=7, fancybox=True,
                         shadow=True, framealpha=.8, ncol=cols,
                         borderaxespad=borderpad)

    if ax_names:
        _add_labels(ax_names)

    # Generate table structure
    if table_header:
        table = [table_header]
    else:
        table = []
    if normalize:
        for i, lbl in enumerate(labels):
            table.append([lbl] + list(chain.from_iterable((int(x[i]), y[i])
                                      for x, y in zip(*[data_original, data]))))
    else:
        for i, lbl in enumerate(labels):
            table.append([lbl] + [int(x[i]) for x in data])

    return fig, lgd, table
Пример #3
0
def stacked_bar_plot(data,
                     labels,
                     legend=None,
                     table_header=None,
                     title=None,
                     ax_names=None,
                     normalize=False,
                     normalize_factor=None,
                     width=0.8):
    """Creates a stacked bar plot.

    Parameters
    ----------
    data : numpy.array
        Multi-dimensional array.
    labels : list
        List of xtick labels. Should have the same length as `data`.
    title : str
        Title of the plot.
    table_header : list
        List with the header of the table object. Each element represents
        a column.
    title : str
        Title of the plot.
    ax_names : list
        List with the labels for the x-axis (first element) and y-axis
        (second element).
    normalize : bool
        If True, values of the `data` array will be normalized by the
        `normalize_factor`
    normalize_factor : int or float
        Number used to normalize values of the `data` array.

    Returns
    -------
    fig : matplotlib.Figure
        Figure object of the plot.
    lgd : matplotlib.Legend
        Legend object of the plot.
    table : list
        Table data in list format. Each item in the list corresponds to a
        table row.
    """

    plt.rcParams["figure.figsize"] = (8, 6)

    if normalize:

        data_original = np.copy(data)
        data = np.array([[y / normalize_factor for y in x] for x in data])

    if len(labels) > 10:
        plt.rcParams["figure.figsize"] = (len(labels) / 3, 6)
    else:
        plt.rcParams["figure.figsize"] = (8, 6)

    # Use ggpot style
    plt.style.use("ggplot")

    fig, ax = plt.subplots()

    # Get bottom positions for stacked bar
    bottoms = np.cumsum(np.vstack((np.zeros(data.shape[1]), data)),
                        axis=0)[:-1]

    # Get x positions
    xpos = [x + (width / 2) for x in range(len(labels))]

    for c, d in enumerate(data):

        if len(data) <= 10:
            c1 = c if c < 9 else c - 10
            clr = cm.Vega10(c1, 1)
        else:
            c1 = c if c < 19 else c - 20
            clr = cm.Vega20c(c1, 1)

        if legend:
            current_lgd = legend[c]
        else:
            current_lgd = None

        if c == 0:
            bplot = ax.bar(xpos,
                           d,
                           width,
                           color=clr,
                           label=current_lgd,
                           alpha=.9)
        else:
            bplot = ax.bar(xpos,
                           d,
                           width,
                           color=clr,
                           label=current_lgd,
                           alpha=.9,
                           bottom=bottoms[c])

    # Set x labels
    plt.xticks([x + (width / 2) for x in xpos],
               labels,
               ha="right",
               rotation=45)

    # Set legend
    if legend:
        if len(legend) <= 4:
            cols = 1
        else:
            cols = len(legend) if len(legend) < 3 else 3
        borderpad = cols * -6
        lgd = plt.legend(loc=7,
                         fancybox=True,
                         shadow=True,
                         framealpha=.8,
                         ncol=cols,
                         borderaxespad=borderpad)
    else:
        lgd = None

    # Generate table structure
    if table_header:
        table = [table_header]
    else:
        table = []
    if normalize:
        for i, lbl in enumerate(labels):
            table.append([lbl] + list(
                chain.from_iterable((int(x[i]), y[i])
                                    for x, y in zip(*[data_original, data]))))
    else:
        for i, lbl in enumerate(labels):
            table.append([lbl] + [x[i] for x in data])

    return fig, lgd, table