Ejemplo n.º 1
0
def show_histogram(
    data, ax: plt.Axes, start: float, end: float, group_inds=None, nbins: int = 30
):
    if not len(data):
        return

    if group_inds is None:
        height, x = np.histogram(np.hstack(data), bins=nbins, range=(start, end))
        width = np.diff(x[:2])
        height = height / len(data) / width
        plt.bar(x[:-1], height, edgecolor=(0.3, 0.3, 0.3), width=width, align="edge")
    else:
        data = np.asarray(data, dtype="object")
        # group_inds = np.asarray(group_inds)
        for group in np.unique(group_inds):
            this_data = np.hstack(data[group_inds == group])
            height, x = np.histogram(this_data, bins=nbins, range=(start, end))
            width = np.diff(x[:2])
            height = height / np.sum(group_inds == group) / width
            ax.bar(
                x[:-1],
                height,
                color=color_wheel[group % len(color_wheel)],
                edgecolor=(0.3, 0.3, 0.3),
                width=width,
                align="edge",
                alpha=0.6,
            )
Ejemplo n.º 2
0
def generate_gsr_max_min_plot(axes: plt.Axes, stimulus_data: pd.DataFrame):
    """
        My plot
    """
    plt.sca(axes)

    windowed_data_max = stimulus_data.resample("2min").max()[:15]
    windowed_data_min = stimulus_data.resample("2min").min()[:15]
    time = convert_date_to_time(windowed_data_max.index)

    axes.set_title(
        "Range-Corrected GSR Maximums and Minimums Over Two-Minute Windows")
    axes.set_xlabel("Time (minutes)", fontsize="large")
    axes.set_ylabel("Range-Corrected GSR (dimensionless)")
    axes.set_ylim(0, 1)
    set_windowed_x_axis(axes)
    axes.bar(time,
             windowed_data_max[RANGE_CORRECT_EDA],
             width=2,
             align="edge",
             edgecolor="black")
    axes.bar(time,
             windowed_data_min[RANGE_CORRECT_EDA],
             width=2,
             align="edge",
             edgecolor="black")
Ejemplo n.º 3
0
def multiple_bar_chart(ax: plt.Axes,
                       xvalues: list,
                       yvalues: dict,
                       title: str,
                       xlabel: str,
                       ylabel: str,
                       percentage=False):
    ax.set_title(title)
    ax.set_xlabel(xlabel)
    ax.set_ylabel(ylabel)
    x = np.arange(len(xvalues))  # the label locations
    ax.set_xticks(x)
    ax.set_xticklabels(xvalues, fontsize='small')
    if percentage:
        ax.set_ylim(0.0, 1.0)
    width = 0.8  # the width of the bars
    step = width / len(yvalues)
    i = 0
    for name, y in yvalues.items():
        ax.bar(x + i * step, y, step, label=name)
        i += 1
    ax.legend(loc='lower center',
              ncol=len(yvalues),
              bbox_to_anchor=(0.5, -0.2),
              fancybox=True,
              shadow=True)
Ejemplo n.º 4
0
def generate_gsr_range_correct_means_plot_two_min(axes: plt.Axes,
                                                  stimulus_data: pd.DataFrame):
    """
    Plots range corrected means over two minute windows.

    :param axes: the axes to plot on
    :param stimulus_data: the raw stimulus data
    :return: None
    """
    plt.sca(axes)

    windowed_data = stimulus_data.resample("2min").mean()[:15]
    time = convert_date_to_time(windowed_data.index)

    axes.set_title(
        "Range-Corrected GSR Means Over Two-Minute Windows (Range Correct on Two-Min)"
    )
    axes.set_xlabel("Time (minutes)", fontsize="large")
    axes.set_ylabel("Range-Corrected GSR (dimensionless)")
    axes.set_ylim(0, 1)
    set_windowed_x_axis(axes)
    axes.bar(time,
             windowed_data["gsr_two_min"],
             width=2,
             align="edge",
             edgecolor="black",
             color="orange")
Ejemplo n.º 5
0
def multiple_bar_chart(xvalues: list,
                       yvalues: dict,
                       ax: plt.Axes = None,
                       title: str = '',
                       xlabel: str = '',
                       ylabel: str = '',
                       percentage=False):
    ax = set_axes(xvalues,
                  ax=ax,
                  title=title,
                  xlabel=xlabel,
                  ylabel=ylabel,
                  percentage=percentage)

    x = np.arange(len(xvalues))  # the label locations

    width = 0.8 / (len(xvalues) * len(yvalues))
    # the width of the bars
    step = width / len(xvalues)
    i: int = 0
    for metric in yvalues:
        ax.bar(x + i * width,
               yvalues[metric],
               width=width,
               align='center',
               label=metric)
        i += 1
    ax.set_xticks(x + width / len(xvalues) - step / 2)
    ax.legend(fontsize='x-small', title_fontsize='small')
Ejemplo n.º 6
0
def npsPlot(pkg: NotePkg, ax:plt.Axes = None, window=1000, stride=None, legend=True, tickStepSize=60000,
            barKwargs=None) -> plt.Axes:
    """ This creates an NPS Plot with the axes.

    :param pkg: Any Note Package
    :param ax: The axis to plot on, if None, we use gca()
    :param window: The window of the roll
    :param stride: The stride length of the roll
    :param legend: Whether to show the legend
    :param tickStepSize: How many milliseconds per tick
    :param barKwargs: The kwargs to pass into plot()
    """
    if ax is None: ax = plt.gca()
    if barKwargs is None: barKwargs = {}
    dns = pkg.rollingDensity(window=window, stride=stride)

    prevHeights = None
    for lisType, lis in dns.items():
        if all(v == 0 for v in lis.values()): continue
        currIndexes = list(lis.keys())
        currHeights = [RAConst.secToMSec(v / window) for v in list(lis.values())]
        ax.bar(currIndexes, currHeights,
               width=pkg.duration() / (len(lis.keys()) - 1),  # -1 to make sure there's no gaps
               bottom=prevHeights,  # Aligns next bar heights with previous
               label=lisType,
               **barKwargs)  # Aligns the bars next to each other
        prevHeights = currHeights
    if legend: ax.legend()
    ax.set_xlim(left=pkg.firstOffset(), right=pkg.lastOffset())
    ax = timedXAxis(ax=ax, stepSize=tickStepSize)
    return ax
Ejemplo n.º 7
0
def weight_per_neuron(ax: plt.Axes, w: np.ndarray, neurons: np.ndarray):

    width = 0.7
    num = w.shape[0]

    w_n, w_n_a, x_n_a = [], [], []

    x_n = np.arange(1, num + 1)

    for i in range(num):
        w_n.append(np.sum(w[i]))

        if neurons[i] == 1:
            sm = 0
            for j in range(num):
                sm += w[i][j] if neurons[j] == 1 else 0
            w_n_a.append(sm)
            x_n_a.append(x_n[i])

    w_max = np.max(w_n)

    # customize layout
    step = (num // 10)
    steps = x_n[0::max(1, step)]
    steps = np.array(steps) - 1
    steps[0] = 1
    if steps[-1] != x_n[-1]:
        steps = np.append(steps, x_n[-1])

    major_locator_n = tik.FixedLocator(steps)
    major_locator_n.view_limits(1, num)
    minor_locator_n = tik.MultipleLocator(1)
    ax.xaxis.set_major_locator(major_locator_n)
    ax.xaxis.set_minor_locator(minor_locator_n)

    ax.set_xlim(0, num + 1)
    ax.set_ylim(0, max(2, w_max))

    # colormap for active neurons:
    y = np.array(w_n_a) - 1
    sp = cm.get_cmap("spring").reversed()
    atu = cm.get_cmap("autumn").reversed()
    colors = [
        atu(abs(y_i) / 1) if y_i < 0 else sp(y_i / max(1, w_max - 1))
        for y_i in y
    ]

    # red dash line:
    ax.plot((0, num + 1), (1, 1), 'red', linestyle='--')

    # gray bars for inactive neurons
    ax.bar(x_n, w_n, width, color='gray')

    # colored active neurons
    ax.bar(x_n_a, w_n_a, width, color=colors)
def plot_4B(ax: plt.Axes):
    df = pd.DataFrame(L).transpose()
    res = GroupBMC(df.to_numpy()).get_result()
    ax.bar(np.arange(4),
           res.protected_exceedance_probability,
           color='white',
           edgecolor='k')
    ax.set_ylim(0, 1)
    ax.set_ylabel('PXP')
    ax.set_xticks(np.arange(4))
    ax.set_xticklabels([Model.name for Model in Models], rotation=22.5)
Ejemplo n.º 9
0
def bar_chart(ax: plt.Axes,
              xvalues: list,
              yvalues: list,
              title: str,
              xlabel: str,
              ylabel: str,
              percentage=False):
    ax.set_title(title)
    ax.set_xlabel(xlabel)
    ax.set_ylabel(ylabel)
    ax.set_xticklabels(xvalues, rotation=90, fontsize='small')
    if percentage:
        ax.set_ylim(0.0, 1.0)
    ax.bar(xvalues, yvalues, edgecolor='grey')
Ejemplo n.º 10
0
def bar_chart(xvalues: list,
              yvalues: list,
              ax: plt.Axes = None,
              title: str = '',
              xlabel: str = '',
              ylabel: str = '',
              percentage=False):
    ax = set_axes(xvalues,
                  ax=ax,
                  title=title,
                  xlabel=xlabel,
                  ylabel=ylabel,
                  percentage=percentage)
    ax.bar(xvalues, yvalues, edgecolor=cfg.LINE_COLOR, color=cfg.FILL_COLOR)
Ejemplo n.º 11
0
def bar_chart(ax: plt.Axes,
              xvalues: list,
              yvalues: list,
              title: str,
              xlabel: str,
              ylabel: str,
              percentage=False,
              logScale=False):
    ax.set_title(title)
    ax.set_xlabel(xlabel)
    ax.set_ylabel(ylabel)
    if logScale:
        ax.set_yscale('log')
        ax.set_xscale('log')
    if percentage:
        ax.set_ylim(0.0, 1.0)
    ax.bar(xvalues, yvalues, edgecolor='grey')
Ejemplo n.º 12
0
def plot_daily_hours_over_timespan(axes: plt.Axes,
                                   projects_dict: dict,
                                   start_datetime=datetime.datetime.now(),
                                   end_datetime=datetime.datetime.now(),
                                   ) -> plt.Axes:

    # ~ Populating the plot
    dts = [(start_datetime + datetime.timedelta(days=offset))
           for offset in range((end_datetime - start_datetime).days + 1)]

    for index, dt in enumerate(dts, start=1):

        date_string = dt.strftime(settings.HARVEST_DATETIME_FORMAT)
        previous_hours = 0
        for project_id, project_data in projects_dict.items():
            if date_string in project_data['daily_hours']:
                hours = project_data['daily_hours'][date_string]
                axes.bar(x=index,
                         bottom=previous_hours,
                         height=hours,
                         color=project_data['color'])

                previous_hours += hours

        axes.text(x=index,
                  y=previous_hours + 0.1,
                  s=f'{previous_hours:0.2f}',
                  fontsize='large',
                  ha='center')

    # ~ Additional plot info
    yticks = list(range(1, len(dts) + 1))
    axes.set_xlim(yticks[0] - 0.5, yticks[-1] + 0.5)
    axes.set_xticks(yticks)
    axes.set_xticklabels([dt.strftime('%d.%m.%Y\n(%A)') for dt in dts])
    axes.set_ylim(0, 12)
    axes.set_title('Daily hours')

    custom_lines = []
    custom_labels = []
    for project_id, project_data in projects_dict.items():
        custom_lines.append(lns.Line2D([0], [0], color=project_data['color'], lw=3))
        custom_labels.append(project_data['label'])
    axes.legend(custom_lines, custom_labels)

    return axes
Ejemplo n.º 13
0
 def __draw_track__(self, rhythm_track: Track, axes: plt.Axes, **kw):
     # compute inter onset intervals and draw bars
     ioi_vector = self.get_feature_extractor("ioi_vector").process(
         rhythm_track)
     return axes.bar(list(range(len(ioi_vector))),
                     ioi_vector,
                     width=0.95,
                     color=kw['color'])
Ejemplo n.º 14
0
def generate_correlation_plot(axes: plt.Axes, window_metrics: pd.DataFrame):
    """
    Creates plot that demonstrates correlation between fixation counts and average fixation duration.

    :param axes: the axes to plot on
    :param window_metrics: the pre-cleaned data
    :return: None
    """
    plt.sca(axes)

    min_fix_dur = window_metrics[AVERAGE_FIX_DUR].min()  # left
    max_fix_dur = window_metrics[AVERAGE_FIX_DUR].max()  # right
    min_fix_count = window_metrics[FIXATION_COUNTS].min()  # bottom
    max_fix_count = window_metrics[FIXATION_COUNTS].max()  # top

    axes.set_title("Overview of Participant Visual Effort")

    x_mid = (max_fix_dur + min_fix_dur) // 2
    y_mid = (max_fix_count + min_fix_count) // 2

    # Background quadrant colors
    bars = axes.bar(x=(x_mid, min_fix_dur, min_fix_dur, x_mid),
                    height=y_mid - min_fix_count,
                    bottom=(y_mid, y_mid, min_fix_count, min_fix_count),
                    width=x_mid - min_fix_dur,
                    color=get_quadrant_color_map().values(),
                    align='edge',
                    alpha=.3,
                    zorder=1)

    # Vertical line for quadrants
    axes.plot([x_mid, x_mid], [min_fix_count, max_fix_count],
              color="black",
              zorder=2)

    # Horizontal line for quadrants
    axes.plot([min_fix_dur, max_fix_dur], [y_mid, y_mid],
              color="black",
              zorder=3)

    legend = plt.legend(
        bars,
        ("Slow Comprehension\nComplexity\nImportance\nConfusion",
         "Fast Comprehension\nSimplicity\nImportance\nPossible confusion",
         "Fast Comprehension\nSimplicity\nUnimportance",
         "Slow Comprehension\nComplexity\nUnimportance"),
        loc='center left',
        bbox_to_anchor=(1, 0.5))

    for lh in legend.legendHandles:
        lh.set_alpha(1)

    axes.scatter(window_metrics[AVERAGE_FIX_DUR],
                 window_metrics[FIXATION_COUNTS],
                 zorder=4)
    axes.set_xlabel("Mean Fixation Duration (ms)", fontsize="large")
    axes.set_ylabel("Fixation Count", fontsize="large")
    axes.autoscale(tight=True)
Ejemplo n.º 15
0
def bar_chart(ax: plt.Axes,
              xvalues: list,
              yvalues: list,
              title: str,
              xlabel: str,
              ylabel: str,
              percentage=False,
              reverse=None):
    ax.set_title(title)
    ax.set_xlabel(xlabel)
    ax.set_ylabel(ylabel)
    if percentage:
        ax.set_ylim(0.0, 1.0)
    if reverse is not None:
        yvalues, xvalues = zip(*sorted(zip(yvalues, xvalues), reverse=reverse))
    ax.set_xticklabels(xvalues, rotation=90, fontsize='small')
    ax.bar(xvalues, yvalues, edgecolor='grey')
    plt.show()
Ejemplo n.º 16
0
def bar_chart(xvalues: list,
              yvalues: list,
              ax: plt.Axes = None,
              title: str = '',
              xlabel: str = '',
              ylabel: str = '',
              percentage=False,
              hidegrid=False,
              hide_xticklabels=False):
    ax = set_axes(xvalues,
                  ax=ax,
                  title=title,
                  xlabel=xlabel,
                  ylabel=ylabel,
                  percentage=percentage,
                  hidegrid=hidegrid)
    ax.bar(xvalues, yvalues, edgecolor=cfg.LINE_COLOR, color=cfg.FILL_COLOR)
    if hide_xticklabels:
        ax.set_xticklabels([])
Ejemplo n.º 17
0
def generate_fixation_plot(axes: plt.Axes, time: np.array,
                           window_metrics: pd.DataFrame):
    """
    A handy method for generating the fixation plot.

    :param window_metrics: a set of windowed data points
    :param axes: the axes to plot on
    :param time: the numpy array of times
    :return: None
    """
    plt.sca(axes)

    axes.set_title("Eye Gaze Metrics with Visual Effort Transitions Over Time")

    # Fixation count plot
    color = 'tab:red'
    axes.plot(time, window_metrics[FIXATION_COUNTS], color=color, linewidth=2)
    axes.set_xlabel("Time (minutes)", fontsize="large")
    axes.set_ylabel("Fixation Count", color=color, fontsize="large")
    axes.tick_params(axis='y', labelcolor=color)

    # Mean fixation duration plot
    ax2 = axes.twinx()
    color = 'tab:cyan'
    ax2.plot(time, window_metrics[AVERAGE_FIX_DUR], color=color, linewidth=2)
    ax2.set_ylabel("Mean Fixation Duration (ms)",
                   color=color,
                   fontsize="large")
    ax2.set_ylim(bottom=100)
    ax2.tick_params(axis='y', labelcolor=color)

    # Background quadrants
    minutes = int(WINDOW[:-1]) / 60
    width = minutes
    colors = get_quad_colors(window_metrics[QUADRANTS])
    axes.bar(time,
             window_metrics[FIXATION_COUNTS].max(),
             alpha=.3,
             width=width,
             color=colors,
             align="edge")
    set_windowed_x_axis(axes)
Ejemplo n.º 18
0
def generate_click_stream_plot(axes: plt.Axes, stimulus_data: pd.DataFrame):
    """
    Generates a line plot of all click stream related data.

    :param stimulus_data: the raw stimulus data
    :param axes: the axes to plot on
    :return: None
    """
    plt.sca(axes)

    data = stimulus_data.groupby([pd.Grouper(freq=WINDOW),
                                  MOUSE_EVENT])[MOUSE_EVENT].count().unstack()
    click_stream = stimulus_data.resample(WINDOW)[[MOUSE_EVENT,
                                                   KEY_CODE]].count()
    time = convert_date_to_time(click_stream.index)
    minutes = int(WINDOW[:-1]) / 60
    width = minutes
    axes.bar(time,
             click_stream[KEY_CODE].values,
             width=width,
             align="edge",
             label="Keyboard Events",
             edgecolor="black")
    accumulator = click_stream[KEY_CODE].values
    for column in data:
        axes.bar(time,
                 data[column],
                 width=width,
                 align="edge",
                 bottom=accumulator,
                 label=column,
                 edgecolor="black")
        accumulator += data[column]

    # Clean up plot
    axes.set_title("Click Stream Events Over Time", fontsize="large")
    axes.set_xlabel("Time (minutes)", fontsize="large")
    axes.set_ylabel("Click Stream Event Counts", fontsize="large")
    set_windowed_x_axis(axes)
    axes.legend()
Ejemplo n.º 19
0
def generate_gsr_peaks_plot(axes: plt.Axes, stimulus_data: pd.DataFrame):
    """
    Plots range-corrected peaks over two-minute windows.

    :param axes: the axes to plot on
    :param stimulus_data: the raw stimulus data
    :return: None
    """
    plt.sca(axes)

    windowed_data = stimulus_data.resample("2min").sum()[:15]
    time = convert_date_to_time(windowed_data.index)

    axes.set_title("Range-Corrected GSR Peaks Over Two-Minute Windows")
    axes.set_xlabel("Time (minutes)", fontsize="large")
    axes.set_ylabel("Peak Count")
    set_windowed_x_axis(axes)
    axes.bar(time,
             windowed_data["peaks"],
             width=2,
             align="edge",
             edgecolor="black")
Ejemplo n.º 20
0
def plot_coords(xs: Union[List[float], np.ndarray],
                ys: Union[List[float], np.ndarray],
                show: bool = None,
                kind='line',
                axes: plt.Axes = None,
                **keys) -> None:
    """
    Plot the points defined by xs and ys

    Args:
        xs: a seq. of x coords
        ys: a seq. of y coords
        kind: one of line or bar
        axes: if given, this axes is used
        kws: any keywords will be bassed to `axes.plot` or `axes.bar` depending
            on `kind`

    """
    if axes:
        if kind == 'line':
            axes.plot(xs, ys, **keys)
        elif kind == 'bar':
            axes.bar(xs, ys, **keys)
    else:
        if kind == 'line':
            plt.plot(xs, ys, **keys)
        elif kind == 'bar':
            plt.bar(xs, ys, **keys)
    plot_always_show = CONFIG['plot.always_show']
    if plot_always_show and (show is None or show is True):
        if axes:
            axes.show()
        else:
            plt.show()
    elif not plot_always_show and show is True:
        plt.show()
Ejemplo n.º 21
0
    def __draw_track__(self, rhythm_track: Track, axes: plt.Axes, **kw):
        ioi_vector = self.get_feature_extractor("ioi_vector").process(
            rhythm_track)
        onset_positions = self.get_feature_extractor(
            "onset_positions").process(rhythm_track)

        # noinspection SpellCheckingInspection
        styles = {
            'edgecolor': kw['color'],
            'facecolor': colors.to_rgba(kw['color'], 0.18),
            'linewidth': 2.0
        }

        return axes.bar(onset_positions,
                        ioi_vector,
                        width=ioi_vector,
                        align="edge",
                        **styles)
def plot_distribution(
    G: nx.Graph,
    quantity,
    as_probability_distribution=False,
    axes: plt.Axes = None,
    annotate=True,
    as_partial=(1, 1),
    attribute_name=None,
    attribute_function=None,
    attribute_function_name="",
    attribute_parent="node",
    axis_scaling="lin-lin",
    bar_width_scaling=0.9,
    bins=None,
    label=None,
    show_cumulative=False,
    plot_options_dict: dict = None,
    plot_type="scatter",
    plotting_backend="matplotlib",
    **kwargs,
):

    # Check input for errors.
    valid_quantities = (
        "attribute",
        "degree",
        "in-degree",
        "out-degree",
        "shortest path length",
    )

    if quantity not in valid_quantities:
        raise Exception('Invalid quantity: "' + quantity + '".')

    # Load quantities.
    if quantity == "attribute":
        if attribute_function is None:
            x_label = (
                f"{attribute_parent.capitalize()} attribute: " f'"{attribute_name}"'
            )
        else:
            x_label = (
                f"{attribute_function_name}("
                f"{attribute_parent} attribute: "
                f'"{attribute_name}")'
            )

        if attribute_parent == "node":
            if as_probability_distribution:
                y_label = "Probability of attribute value"
            else:
                y_label = "Number of nodes"

        else:
            if as_probability_distribution:
                y_label = "Probability of attribute value"
            else:
                y_label = "Number of edges"

        title = f'Distribution of attribute: "{attribute_name}"'

        if attribute_parent == "node":
            attributes_dict = dict(nx.get_node_attributes(G, attribute_name))
            attribute_values = list(attributes_dict.values())

        elif attribute_parent == "edge":
            attributes_dict = dict(nx.get_edge_attributes(G, attribute_name))
            attribute_values = list(attributes_dict.values())
        else:
            raise ValueError(f'Invalid attribute object: "{attribute_parent}"')

        if attribute_function is not None:
            attribute_values = list(map(attribute_function, attribute_values))

        # Make sure attribute values is a numpy array
        attribute_values = np.array(attribute_values)

        # Remove NaNs
        nan_indices = np.isnan(attribute_values)
        attribute_values = attribute_values[~nan_indices]

        if bins is None:
            distribution, bins = np.histogram(attribute_values)
        else:
            distribution, bins = np.histogram(attribute_values, bins=bins)

        if as_probability_distribution:
            distribution = distribution / len(attribute_values)

        bin_centers = [np.mean([bins[i], bins[i + 1]]) for i in range(len(bins) - 1)]

    elif quantity in ["degree", "in-degree", "out-degree"]:
        x_label = "Node degree"
        if as_probability_distribution:
            y_label = "Probability of node degree"
        else:
            y_label = "Number of nodes"

        if quantity == "degree":
            direction = None
            title = "Degree distribution"

        elif quantity == "in-degree":
            direction = "in"
            title = "In-degree distribution"

        elif quantity == "out-degree":
            direction = "out"
            title = "Out-degree distribution"

        bin_centers, distribution = w.graph.degree_distribution(
            G,
            direction=direction,
            as_probability_distribution=as_probability_distribution,
        )

    elif quantity == "shortest path length":
        bin_centers, distribution = w.graph.shortest_path_length_distribution(
            G, as_probability_distribution=as_probability_distribution
        )

        title = "Shortest path length distribution"
        x_label = "Path length"
        if as_probability_distribution:
            y_label = "Probability of path length"
        else:
            y_label = "Number of paths"

    # Plot
    if plot_options_dict is None:
        if plot_type == "scatter":
            plot_options_dict = {
                "marker": "o",
                "markerfacecolor": "black",
                "markersize": 3,
                "linestyle": "None",
                "linewidth": 0.5,
                "color": "blue",
            }
        else:
            plot_options_dict = dict()

    if "color" in kwargs:
        plot_options_dict["color"] = kwargs["color"]

    if plot_type == "bar":
        number_of_parts = as_partial[0]
        part_number = as_partial[1]
        width_bin = bin_centers[1] - bin_centers[0]
        width_all_bars = bar_width_scaling * width_bin
        width_bar = width_all_bars / number_of_parts

        adjustment = (
            np.linspace(0, width_all_bars, number_of_parts, endpoint=False)
            - width_all_bars / 2
            + width_bar / 2
        )[part_number - 1]
        bin_centers = bin_centers + adjustment

    if axis_scaling == "lin-lin":
        if plot_type == "scatter":
            x_start = min(bin_centers)
            x_end = max(bin_centers)
        elif plot_type == "bar":
            x_start = min(bin_centers) + np.min(adjustment) - 1.5 * width_bar
            x_end = max(bin_centers) + np.min(adjustment) + 1.5 * width_bar
    elif axis_scaling == "log-log":
        x_start = 1
        x_end = 10 ** np.ceil(np.log10(max(bin_centers)))
        if plotting_backend == "plotly":
            # plotly needs the log10 of the range for logplots
            x_start = np.log10(x_start)
            x_end = np.log10(x_end)
    else:
        raise Exception('Unknown axis scaling: "' + axis_scaling + '"')

    if plotting_backend == "matplotlib":
        new_figure_created = False
        if axes is None:
            new_figure_created = True
            figure = plt.figure()
            figure.set_facecolor("white")
            axes = figure.gca()
            axes.set_facecolor("white")

        if annotate:
            axes.set_title(title)
            axes.set_xlabel(x_label)
            axes.set_ylabel(y_label)

        if plot_type == "scatter":
            axes.plot(bin_centers, distribution, label=label, **plot_options_dict)
            axes.grid()
        elif plot_type == "bar":
            axes.bar(
                bin_centers,
                distribution,
                width=width_bar,
                label=label,
                **plot_options_dict,
            )
        else:
            raise Exception('Unknown plot type: "' + plot_type + '"')

        axes.set_xlim(x_start, x_end)

        if axis_scaling == "lin-lin":
            pass
        elif axis_scaling == "log-log":
            axes.set_xscale("log")
            axes.set_yscale("log")
        else:
            raise Exception('Unknown axis scaling: "' + axis_scaling + '"')

        if show_cumulative:
            cumulative_curve = axes.plot(
                bin_centers, np.cumsum(distribution), color="#eb9b34"
            )
            axes.legend((cumulative_curve[0],), ("Cumulative",))

        if as_partial[0] > 1:
            axes.legend()

        if new_figure_created:
            w.format_figure(figure)

        return axes

    elif plotting_backend == "plotly":
        if plot_type == "scatter":
            traces = [
                go.Scatter(
                    name="Distribution",
                    x=bin_centers,
                    y=distribution,
                    marker={"color": plot_options_dict["color"]},
                    mode="markers",
                    hovertemplate=f"{x_label}: %{{x}} <br> {y_label}: %{{y}}",
                )
            ]
        elif plot_type == "bar":
            traces = [
                go.Bar(
                    name="Distribution",
                    x=bin_centers,
                    y=distribution,
                    hovertemplate=f"{x_label}: %{{x}} <br> {y_label}: %{{y}}",
                )
            ]
        if show_cumulative:
            traces += [
                go.Scatter(
                    x=bin_centers,
                    y=np.cumsum(distribution),
                    name="Cumulative Distribution",
                    mode="lines",
                    hovertemplate=f"{x_label}: %{{x}} <br> Total {y_label}: %{{y}}",
                )
            ]
        figure = go.Figure(
            data=traces,
            layout=go.Layout(
                title=title,
                xaxis_title=x_label,
                yaxis_title=y_label,
                autosize=True,
                width=800,
                height=600,
                showlegend=True,
                margin={"l": 20, "r": 20, "t": 25, "b": 25},
                xaxis={
                    "type": "log" if axis_scaling == "log-log" else "linear",
                    "range": [x_start, x_end],
                },
                yaxis={"type": "log" if axis_scaling == "log-log" else "linear"},
            ),
        )

        return figure
    else:
        raise AssertionError("plotting_backeng can only be 'matplotlib' or 'plotly'")
Ejemplo n.º 23
0
    def __init__(
        self,
        counts: pd.DataFrame,
        variable: str = "cases",
        *,
        strings: Translation = NullTranslation(),
        normalize: bool = True,
        age_group_size: int = 10,
        window: int = 14,
        population_distribution: pd.Series = None,
        ax: plt.Axes = None,
        resample_kwargs: Dict[str, Any] = {},
    ):
        """
        Shows the age distribution of cases/deaths at a given date.
        Usable either to draw a static chart or to generate an animation.

        Args:
            counts:
            variable:
            normalize:
            age_group_size:
            window:
            population_distribution:
            ax:
            lang:
        """
        self.ax = ax = ax or plt.gca()
        s = strings

        data = counts[variable].drop(columns="unknown")
        data = ic.running_average(data, window=window, **resample_kwargs)
        data = ic.aggregate_age_groups(data, cuts=age_group_size)

        if normalize:
            data = data.divide(data.sum(axis=1), axis=0)
            ax.yaxis.set_major_formatter(
                mpl.ticker.PercentFormatter(xmax=1.0, decimals=0))
        self.data = data

        if normalize and population_distribution is not None:
            population = ic.aggregate_age_groups(population_distribution,
                                                 cuts=age_group_size)
            sns.barplot(
                ax=ax,
                label=s["istat_population_data_label"],
                x=population.index,
                y=population,
                facecolor='#a1c9f4',
                hatch="/",
                edgecolor="white",
            )

        age_groups = data.columns
        label = s[f"{variable}_label"]
        self.bars = ax.bar(
            x=age_groups,
            height=[0] * len(age_groups),
            label=label,
            facecolor='#4878d0',
            alpha=0.7,
        )
        self.labels, self.update_labels = add_labels_to_bars(
            self.bars,
            fmt='{:.0%}' if normalize else '{:n}',
            fontsize=10,
            ax=ax,
            color='white',
            bbox=dict(
                boxstyle=mpl.patches.BoxStyle("Round", pad=0.3),
                color='black',
                alpha=.35,
            ),
        )
        ymax = 1.05 * data.max().max()
        ax.set_ylim(0, ymax)
        ax.set_xticklabels(age_groups)
        ax.set_xlabel(s["age"])
        ax.set_ylabel("")
        ax.grid(False, which='both', axis='x')
        if normalize:
            title = s.get(f"running_{variable}_age_distribution", count=window)
        else:
            title = s.get(f"running_average_{variable}_title", count=window)
        ax.set_title(title, fontsize=14)
        if normalize and population_distribution is not None:
            ax.yaxis.set_major_formatter(
                mpl.ticker.PercentFormatter(xmax=1.0, decimals=0))
            ax.legend()

        self.date_text = ax.text(
            0.5,
            0.93,
            "",
            ha="center",
            va="center",
            transform=ax.transAxes,
            color=(1, 1, 1, 0.9),
            bbox=dict(
                boxstyle=mpl.patches.BoxStyle("Round", pad=0.4),
                color=(0, 0, 0, 0.30),
            ),
            fontsize=14,
            fontweight="semibold",
        )
        self.artists = [self.date_text, *self.bars, *self.labels]
def plot_degree_distribution(G: nx.Graph,
                             direction: str = None,
                             as_probability_distribution=False,
                             axes: plt.Axes = None,
                             annotate=['all'],
                             label=None,
                             axis_scaling='lin-lin',
                             show_cumulative=False,
                             plot_options_dict: dict = None,
                             plot_type='scatter',
                             **kwargs):

    if isinstance(annotate, str):
        annotate = [annotate]

    # Histogram
    degrees, distribution = w.graph.degree_distribution(
        G,
        direction=direction,
        as_probability_distribution=as_probability_distribution)

    # Plot
    if plot_options_dict is None:
        if plot_type == 'scatter':
            plot_options_dict = \
                {'marker': 'o', 's': 3, 'c': 'blue'}
        elif plot_type == 'bar':
            plot_options_dict = {'color': 'blue'}
        else:
            plot_options_dict = dict()

    if 'color' in kwargs:
        if plot_type == 'scatter':
            plot_options_dict['c'] = kwargs['color']
        else:
            plot_options_dict['color'] = kwargs['color']

    if 'marker_size' in kwargs:
        if plot_type == 'scatter':
            plot_options_dict['s'] = kwargs['marker_size']

    new_figure_created = False
    if axes is None:
        new_figure_created = True
        figure = plt.figure()
        figure.set_facecolor('white')
        axes = figure.gca()
        axes.set_facecolor('white')

    if direction is None:
        title = 'Degree distribution'
    elif direction == 'in':
        title = 'In-degree distribution'
    elif direction == 'out':
        title = 'Out-degree distribution'
    else:
        raise Exception('Invalid direction: "' + direction + '"')

    if as_probability_distribution:
        y_label = 'Probability of node degree'
    else:
        y_label = 'Number of nodes'

    if ('title' in annotate) or ('all' in annotate):
        axes.set_title(title)
    if ('x_label' in annotate) or ('all' in annotate):
        axes.set_xlabel('Node degree')
    if ('y_label' in annotate) or ('all' in annotate):
        axes.set_ylabel(y_label)

    if plot_type == 'scatter':
        plot = axes.scatter(degrees,
                            distribution,
                            label=label,
                            **plot_options_dict)
        axes.grid()
    elif plot_type == 'bar':
        plot = axes.bar(degrees,
                        distribution,
                        label=label,
                        **plot_options_dict)
    else:
        raise Exception('Unknown plot type: "' + plot_type + '"')

    if axis_scaling == 'lin-lin':
        if plot_type == 'scatter':
            axes.set_xlim(0, max(degrees))
        elif plot_type == 'bar':
            axes.set_xlim(-1, max(degrees))

    elif axis_scaling == 'log-log':
        axes.set_xscale('log')
        axes.set_yscale('log')
        axes.set_xlim(0.8, 10**np.ceil(np.log10(max(degrees))))
    else:
        raise Exception('Unknown axis scaling: "' + axis_scaling + '"')

    if show_cumulative:
        cumulative_curve = axes.plot(degrees,
                                     np.cumsum(distribution),
                                     color='#eb9b34')
        axes.legend((cumulative_curve[0], ), ('Cumulative', ))

    if new_figure_created:
        w.format_figure(figure)

    return axes, (degrees, distribution)
Ejemplo n.º 25
0
def plot_histogram_to_ax(ax: plt.Axes, histogram):
    ax.bar(list(histogram.keys()), list(histogram.values()))
    ax.ticklabel_format(style="sci", axis="y", scilimits=(0, 0))
    ax.set_ylabel("Number of MPR Pairs")
Ejemplo n.º 26
0
def plot_f1_metrics(run_histories: List[Run],
                    experiment_name: str,
                    metric_names: Dict[str, str],
                    target_file_paths: Optional[List[str]] = None,
                    axis: plt.Axes = None,
                    y_lims: Tuple[float, float] = None,
                    mode='box',
                    add_legend=True,
                    color=None):
    standalone_mode = axis is None

    if color is None:
        # by default we use bright orange from map 'tab20c'
        color = plt.get_cmap('tab20c')(4)

    runs_data = []
    for run_history in run_histories:
        metrics_data = []
        for metric_name in metric_names.keys():
            metric_data = []
            for fold_history in run_history.fold_histories:
                # add the metric for the last epoch
                metric_data.append(fold_history.epochs[-1].metrics[metric_name])
            metric_data = np.array(metric_data)
            if mode == 'bar':
                metric_data = np.mean(metric_data)
            metrics_data.append(metric_data)
        runs_data.append(metrics_data)

    with plt.style.context('seaborn'):
        if axis is None:
            fig: plt.Figure = plt.figure()
            fig.suptitle(experiment_name, fontsize=24)
            axis = fig.add_subplot(111)
        else:
            axis.set_title(experiment_name, fontsize=22)
        axis.set_ylabel('Metric Value', fontsize=22)
        if y_lims is not None:
            print('limits', y_lims)
            axis.set_ylim(*y_lims)
        num_metrics = None
        num_runs = len(runs_data)
        for i, metrics_data in enumerate(runs_data):
            num_metrics = len(metrics_data)
            xs = range(i * len(metrics_data) + i, (i + 1) * len(metrics_data) + i)

            max_v = .9
            min_v = .6
            colors = []
            for idx in range(num_metrics):
                if num_metrics > 1:
                    norm = idx * (max_v - min_v) / (num_metrics - 1)
                else:
                    norm = 0
                fill_color = list(colorsys.rgb_to_hls(*mc.to_rgb(color)))
                fill_color[1] = min_v + norm
                colors.append((
                    color,
                    colorsys.hls_to_rgb(*fill_color)
                ))
            line_styles = ['-', '-.', ':', '--']

            if mode == 'box':
                boxplots = axis.boxplot(
                    metrics_data,
                    meanline=True,
                    showmeans=True,
                    positions=xs,
                    widths=0.6,
                    patch_artist=True
                )

                for plot_idx in range(num_metrics):
                    dark_color = colors[plot_idx][0]
                    light_color = colors[plot_idx][1]

                    plt.setp(boxplots['boxes'][plot_idx], color=dark_color)
                    plt.setp(boxplots['boxes'][plot_idx], facecolor=light_color)
                    plt.setp(boxplots['boxes'][plot_idx], linestyle=line_styles[plot_idx])

                    plt.setp(boxplots['whiskers'][plot_idx * 2], color=dark_color)
                    plt.setp(boxplots['whiskers'][plot_idx * 2 + 1], color=dark_color)
                    plt.setp(boxplots['whiskers'][plot_idx * 2], linestyle=line_styles[plot_idx])
                    plt.setp(boxplots['whiskers'][plot_idx * 2 + 1], linestyle=line_styles[plot_idx])

                    plt.setp(boxplots['caps'][plot_idx * 2], color=dark_color)
                    plt.setp(boxplots['caps'][plot_idx * 2 + 1], color=dark_color)

                    plt.setp(boxplots['fliers'][plot_idx], markeredgecolor=dark_color)
                    plt.setp(boxplots['fliers'][plot_idx], marker='x')

                    plt.setp(boxplots['medians'][plot_idx], color=dark_color)
                    plt.setp(boxplots['means'][plot_idx], color=dark_color)

                    legend_styles = [boxplots['boxes'][idx] for idx in range(num_metrics)]
            elif mode == 'bar':
                legend_styles = []
                for plot_idx in range(num_metrics):
                    ret = axis.bar(xs[plot_idx], metrics_data[plot_idx],
                                   color=colors[plot_idx][1],
                                   edgecolor=colors[plot_idx][0],
                                   width=0.6,
                                   linewidth=1.25,
                                   linestyle=line_styles[plot_idx], )
                    legend_styles.append(ret)

        tick_offset = num_metrics * 0.5 - 0.5
        ticks = np.arange(start=tick_offset, stop=num_runs * num_metrics + num_runs + tick_offset,
                          step=num_metrics + 1.0)
        axis.set_xticks(ticks)
        for yticklabel in axis.get_yticklabels():
            yticklabel.set_fontsize(20)
        axis.set_xticklabels([r.name for r in run_histories], fontsize=20, rotation=0)
        if add_legend:
            axis.legend(legend_styles, metric_names.values(),
                        loc='lower right', fontsize=16,
                        facecolor="white", frameon=True,
                        edgecolor="black")

        if standalone_mode:
            fig.show()
            if target_file_paths is not None:
                for target_file_path in target_file_paths:
                    fig.savefig(target_file_path)
        return legend_styles
Ejemplo n.º 27
0
def plot_string_histogram(string_list: List[str], title: str, axis: plt.Axes):
    """Plot frequency of unique strings in list in matplotlib axis."""
    logging.info("Plotting frequency of unique strings")
    letter_counts = dict(collections.Counter(string_list))
    axis.bar(letter_counts.keys(), letter_counts.values())
    axis.set_title(title)
Ejemplo n.º 28
0
 def __draw_track__(self, rhythm_track: Track, axes: plt.Axes, **kw):
     occurrences, interval_durations = self.get_feature_extractor(
         "ioi_histogram").process(rhythm_track)
     return axes.bar(interval_durations, occurrences, color=kw['color'])
def errorbox(
    plot: plt.Axes,
    plot_def: PlotDef,
    xaxis: Tuple[str, str],
    yaxis: Tuple[str, str],
    firstcolor: int = 0,
    firstshape: int = 0,
    markersize: int = 6,
    use_cross: bool = False,
) -> Optional[mpl.legend.Legend]:
    """Generate a figure with errorboxes that reflect the std dev of an entry.

    Args:
        plot: a pyplot Axes object
        plot_def: a `PlotDef` that defines properties of the plot
        xaxis: a tuple of two strings
        yaxis: a tuple of two strings
        firstcolor: index of the color that should be used for the first entry
        firstshape: index of the shape that should be used for the first entry
        markersize: size of the markers
        use_cross: if True, use cross instead of boxes
    """
    # ================================ constants for plots ========================================
    colors = ["#1f77b4", "#ff7f0e", "#2ca02c", "#d62728", "#9467bd"]
    colors += ["#8c564b", "#e377c2", "#7f7f7f", "#bcbd22", "#17becf"]
    pale_colors = ["#aec7e8", "#ffbb78", "#98df8a", "#ff9896", "#c5b0d5"]
    pale_colors += ["#c49c94", "#f7b6d2", "#c7c7c7", "#dbdb8d", "#9edae5"]
    shapes = ["o", "X", "D", "s", "^", "v", "<", ">", "*", "p", "P"]
    hatch_pattern = ["O", "o", "|", "-", "/", "\\", "+", "x", "*"]

    xaxis_measure, yaxis_measure = xaxis[0], yaxis[0]
    filled_counter = firstcolor

    max_xmean = 0.0
    min_xmean = 1.0

    max_ymean = 0.0
    min_ymean = 1.0

    for i, entry in enumerate(plot_def.entries):
        if entry.do_fill:
            color = colors[filled_counter]
            filled_counter += 1
        else:
            color = pale_colors[i + firstcolor - filled_counter]
        i_shp = firstshape + i

        xmean: float = entry.values[xaxis_measure].mean()
        xstd: float = entry.values[xaxis_measure].std()
        if xmean + (0.5 * xstd) > max_xmean:
            max_xmean = xmean + (0.5 * xstd)
        if xmean - (0.5 * xstd) < min_xmean:
            min_xmean = xmean - (0.5 * xstd)

        ymean: float = entry.values[yaxis_measure].mean()
        ystd: float = entry.values[yaxis_measure].std()
        if ymean + (0.5 * ystd) > max_ymean:
            max_ymean = ymean + (0.5 * ystd)
        if ymean - (0.5 * ystd) < min_ymean:
            min_ymean = ymean - (0.5 * ystd)

        if use_cross:
            plot.errorbar(
                xmean,
                ymean,
                xerr=0.5 * xstd,
                yerr=0.5 * ystd,
                marker=shapes[i_shp % len(shapes)],
                linestyle="",  # no connecting lines
                color=color,
                ecolor="#555555",
                capsize=4,
                elinewidth=1.0,
                zorder=3 + 2 * i_shp,
                label=entry.label,
                markersize=markersize,
            )
        else:
            plot.bar(
                xmean,
                ystd,
                bottom=ymean - 0.5 * ystd,
                width=xstd,
                align="center",
                color="none",
                edgecolor=color,
                linewidth=3,
                zorder=3 + 2 * i_shp,
                hatch=hatch_pattern[i % len(hatch_pattern)],
            )
            plot.plot(
                xmean,
                ymean,
                marker=shapes[i_shp % len(shapes)],
                linestyle="",  # no connecting lines
                color=color,
                label=entry.label,
                zorder=4 + 2 * i_shp,
                markersize=markersize,
            )

    x_min = min_xmean * 0.99
    x_max = max_xmean * 1.01

    y_min = min_ymean * 0.99
    y_max = max_ymean * 1.01

    plot.set_xlim(x_min, x_max)
    plot.set_ylim(y_min, y_max)
    return common_plotting_settings(plot, plot_def, xaxis[1], yaxis[1])
Ejemplo n.º 30
0
def bar_chart(xvalues: list, yvalues: list, ax: plt.Axes = None, title: str = '',
              xlabel: str = '', ylabel: str = '', percentage=False):
    ax = set_axes(xvalues, ax=ax, title=title, xlabel=xlabel, ylabel=ylabel, percentage=percentage)
    ax.bar(xvalues, yvalues, edgecolor='#0C70B2', color='#BDDDE0')