<commit_before>__author__ = '<NAME>'
import matplotlib.pyplot as plt
plt.ioff()
import random
import os
from matplotlib.colors import colorConverter


def plot_lists(to_plot_list, fig_options=None, ax=None, plot_min=None, plot_max=None):
    """
    Plots several functions.

    Each entry of to_plot_list defines x, y, and format options.

    Parameters
    ----------
    to_plot_list : list of dicts
        Defines the functions to plot.
        Each entry must contain at least values for "x" and
        "y", and can contain values for "type", "label" and "color".
        x : list
            A list of x values
        y : list
            A list of y values
        type : string, optional
            Either "line", in which case a line will be plotted, or "scatter",
            in which case a scatter plot will be made. line is default.
        label : string, optional
            The label for the function. Default is ""
        color : string, optional
            Which color the plot should have. If None, a random colour is
            chosen.
        minimizing : bool, optional
            Whether the plot's goal is to minimize or maximize. Default is
            minimize.
    ax : Matplotlib.Axes, optional
        Axes to continue. If None, will return a new Ax and figure.
    fig_options : dict, optional
        Options used when creating a new plot.
        "legend_loc" : string, optional
            Location for the legend. Default is "upper right"
        "x_label" : string, optional
            x label for the figure
        "y_label" : string, optional
            y label for the figure
    plot_at_least : 2-float tuple, optional
        How many percent of the values should be displayed, from above and from
        below.
    plot_min : float, optional
        Plot from this value.
    plot_max : float, optional
        Plot up to this value.

    Returns
    -------
    fig : plt.figure
        Only when no ax had been specified.
    Ax : plt.Axes
        The plot containing the plotted lists.
    """
    fig = None
    if ax is None:
        fig, ax = create_figure(fig_options)

    for p in to_plot_list:
        if not "cutoff_percent" in p:
            continue
        if fig_options.get("minimizing", True):
            plot_min_this, plot_max_this = _get_y_min_max(p["y"], (1, p.get("cutoff_percent", 1)))
        else:
            plot_min_this, plot_max_this = _get_y_min_max(p["y"], (p.get("cutoff_percent", 1), 1))
        if plot_min is None or plot_min > plot_min_this:
            plot_min = plot_min_this
        if plot_max is None or plot_max > plot_max_this:
            plot_max = plot_max_this

    _plot_lists_ax(to_plot_list, ax, plot_min=plot_min, plot_max=plot_max)
    _polish_figure(ax, fig_options)

    if fig is None:
        return ax
    else:
        return fig, ax


def _plot_lists_ax(to_plot_list, ax, plot_min=None, plot_max=None):
    """
    Plots several functions.

    Each entry of to_plot_list defines x, y, and format options.

    Parameters
    ----------
    to_plot_list: list of dicts
        Defines the functions to plot.
        Each entry must contain at least values for "x" and
        "y", and can contain values for "type", "label" and "color".
        x: list
            A list of x values
        y: list
            A list of y values
        type="line": string
            Either "line", in which case a line will be plotted, or "scatter",
            in which case a scatter plot will be made.
        label="": string
            The label for the function.
        color=random colour: string
            Which color the plot should have.
    ax : Matplotlib.Axes
        Axes to continue.
    plot_at_least : 2-float tuple, optional
        How many percent of the values should be displayed, from above and from
        below.
    plot_min : float, optional
        Plot from this value.
    plot_max : float, optional
        Plot up to this value.

    Returns
    -------
    Ax : plt.Axes
        The plot containing the plotted lists.
    """
    for p in to_plot_list:
        ax = plot_single(p, ax, plot_min=plot_min, plot_max=plot_max)
    if plot_min is not None:
        ax.set_ylim(ymin=plot_min)
    if plot_max is not None:
        ax.set_ylim(ymax=plot_max)
    return ax


def _get_y_min_max(y, plot_at_least):
    """
    Returns the maximum / minimum values which should be plotted according to
    plot_at_least.

    Parameters
    ----------
    y : list of floats
        The y values in question.

    plot_at_least : 2-tuple of floats
        The (from_below, from_above) percentage of points to show.

    Returns
    -------
    min_y_new : float
        The new minimum y value.
    max_y_new : float
        The new maximum y value.
    """
    if len(y) == 0:
        return None, None
    sorted_y = sorted(y)
    if plot_at_least[0] == 1:
        min_y_new = min(y)
    else:
        min_y_new = sorted_y[int(plot_at_least[0] * (-len(sorted_y)))]
    if plot_at_least[1] == 1:
        max_y_new = max(y)
    else:
        max_y_new = sorted_y[min(len(sorted_y)-1, int(plot_at_least[1] * len(sorted_y)))]

    return min_y_new, max_y_new

COLORS = ["g", "r", "c", "b", "m", "y"]


def plot_single(to_plot, ax=None, fig_options=None, plot_min=None, plot_max=None):
    """
    Plots a single function.<commit_msg>Added support for NaNs to plot_utils.<commit_after>__author__ = '<NAME>'
import matplotlib.pyplot as plt
plt.ioff()
import random
import os
from matplotlib.colors import colorConverter


def plot_lists(to_plot_list, fig_options=None, ax=None, plot_min=None, plot_max=None):
    """
    Plots several functions.

    Each entry of to_plot_list defines x, y, and format options.

    Parameters
    ----------
    to_plot_list : list of dicts
        Defines the functions to plot.
        Each entry must contain at least values for "x" and
        "y", and can contain values for "type", "label" and "color".
        x : list
            A list of x values
        y : list
            A list of y values
        type : string, optional
            Either "line", in which case a line will be plotted, or "scatter",
            in which case a scatter plot will be made. line is default.
        label : string, optional
            The label for the function. Default is ""
        color : string, optional
            Which color the plot should have. If None, a random colour is
            chosen.
        minimizing : bool, optional
            Whether the plot's goal is to minimize or maximize. Default is
            minimize.
    ax : Matplotlib.Axes, optional
        Axes to continue. If None, will return a new Ax and figure.
    fig_options : dict, optional
        Options used when creating a new plot.
        "legend_loc" : string, optional
            Location for the legend. Default is "upper right"
        "x_label" : string, optional
            x label for the figure
        "y_label" : string, optional
            y label for the figure
    plot_at_least : 2-float tuple, optional
        How many percent of the values should be displayed, from above and from
        below.
    plot_min : float, optional
        Plot from this value.
    plot_max : float, optional
        Plot up to this value.

    Returns
    -------
    fig : plt.figure
        Only when no ax had been specified.
    Ax : plt.Axes
        The plot containing the plotted lists.
    """
    fig = None
    if ax is None:
        fig, ax = create_figure(fig_options)

    for p in to_plot_list:
        if not "cutoff_percent" in p:
            continue
        if fig_options.get("minimizing", True):
            plot_min_this, plot_max_this = _get_y_min_max(p["y"], (1, p.get("cutoff_percent", 1)))
        else:
            plot_min_this, plot_max_this = _get_y_min_max(p["y"], (p.get("cutoff_percent", 1), 1))
        if plot_min is None or plot_min > plot_min_this:
            plot_min = plot_min_this
        if plot_max is None or plot_max > plot_max_this:
            plot_max = plot_max_this

    _plot_lists_ax(to_plot_list, ax, plot_min=plot_min, plot_max=plot_max)
    _polish_figure(ax, fig_options)

    if fig is None:
        return ax
    else:
        return fig, ax


def _plot_lists_ax(to_plot_list, ax, plot_min=None, plot_max=None):
    """
    Plots several functions.

    Each entry of to_plot_list defines x, y, and format options.

    Parameters
    ----------
    to_plot_list: list of dicts
        Defines the functions to plot.
        Each entry must contain at least values for "x" and
        "y", and can contain values for "type", "label" and "color".
        x: list
            A list of x values
        y: list
            A list of y values
        type="line": string
            Either "line", in which case a line will be plotted, or "scatter",
            in which case a scatter plot will be made.
        label="": string
            The label for the function.
        color=random colour: string
            Which color the plot should have.
    ax : Matplotlib.Axes
        Axes to continue.
    plot_at_least : 2-float tuple, optional
        How many percent of the values should be displayed, from above and from
        below.
    plot_min : float, optional
        Plot from this value.
    plot_max : float, optional
        Plot up to this value.

    Returns
    -------
    Ax : plt.Axes
        The plot containing the plotted lists.
    """
    for p in to_plot_list:
        ax = plot_single(p, ax, plot_min=plot_min, plot_max=plot_max)
    if plot_min is not None:
        ax.set_ylim(ymin=plot_min)
    if plot_max is not None:
        ax.set_ylim(ymax=plot_max)
    return ax


def _get_y_min_max(y, plot_at_least):
    """
    Returns the maximum / minimum values which should be plotted according to
    plot_at_least.

    Parameters
    ----------
    y : list of floats
        The y values in question.

    plot_at_least : 2-tuple of floats
        The (from_below, from_above) percentage of points to show.

    Returns
    -------
    min_y_new : float
        The new minimum y value.
    max_y_new : float
        The new maximum y value.
    """
    if len(y) == 0:
        return None, None
    sorted_y = sorted([x for x in y if str(x) != 'nan'])
    if plot_at_least[0] == 1:
        min_y_new = min(y)
    else:
        min_y_new = sorted_y[int(plot_at_least[0] * (-len(sorted_y)))]
    if plot_at_least[1] == 1:
        max_y_new = max(y)
    else:
        max_y_new = sorted_y[min(len(sorted_y)-1, int(plot_at_least[1] * len(sorted_y)))]

    return min_y_new, max_y_new

COLORS = ["g", "r", "c", "b", "m", "y"]


def plot_single(to_plot, ax=None, fig_options=None, plot_min=None, plot_max=None):
    """
    Plots a single function.
{'fraction_non_alphanumeric': 0.08412168428862919, 'fraction_numerical': 0.0027824151363383415, 'mean_word_length': 2.0941176470588236, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4420696', 'n_tokens_mistral': 3348, 'n_tokens_neox': 3259, 'n_words': 1187}