Пример #1
0
def plot_toss_wins_normalized():
    p = Palette()
    data = get_data()
    toss_won = [x[2] for x in data]
    toss = Counter(toss_won)
    win = Counter([x[0] for x in data])
    matches = [x[0] for x in data]
    matches.extend([x[1] for x in data])
    matches = Counter(matches)
    x_values, y_values = [], []
    for m in win:
        plt.scatter(toss[m] / matches[m], win[m] / matches[m], color=p.red())
        ha = "left"
        x_offset = 0.002
        if m in ["Punjab", "Mumbai", "Kolkata", "Gujarat"]:
            ha = "right"
            x_offset = -0.002

        plt.text(toss[m] / matches[m] + x_offset, win[m] / matches[m], m,
                 ha=ha)
        x_values.append(toss[m] / matches[m])
        y_values.append(win[m] / matches[m])

    plt.xlim(0.4, 0.65)
    plt.ylim(0.3, 0.62)
    plt.gca().set_facecolor(p.gray(shade=10))
    plt.grid(which="both", ls="--", alpha=0.5)
    plt.xlabel("number of times team has won the toss (normalized)")
    plt.ylabel("number of wins (normalized)")
    plt.title("after data is normalized to number of matches played")
    draw_correlation(x_values, y_values, 0.65)
    plt.tight_layout()
    plt.savefig("plot.png", dpi=150)
    plt.show()
Пример #2
0
def plot_connections(data,
                     max_cols=None,
                     show=True,
                     color_mapping: dict = None) -> NetworkPlot:
    p = Palette()
    all_nodes = []
    for d in data:
        for i, node in enumerate(d):
            if i == 0:
                all_nodes.append([node, d[i + 1], 1])
            elif i == len(d) - 1 and len(d) == 2:
                continue
            else:
                all_nodes.append([d[i - 1], node, 1])

    mapping_colors = {data[0][0]: p.blue(), data[0][-1]: p.red()}

    n = NetworkPlot(all_nodes)
    if max_cols is not None:
        n.max_columns = max_cols
    n.colors = p.gray(shade=30)
    if color_mapping is not None:
        n.colors_mapping = color_mapping
    else:
        n.colors_mapping = mapping_colors
    if show:
        n.show()
    return n
Пример #3
0
def plot_toss_wins():
    p = Palette()
    data = get_data()
    toss_won = [x[2] for x in data]
    toss = Counter(toss_won)
    win = Counter([x[0] for x in data])
    x_values, y_values = [], []
    for m in win:
        plt.scatter(toss[m], win[m], color=p.red())
        ha = "left"
        x_offset = 2
        if m in ["Punjab"]:
            ha = "right"
            x_offset = -2

        plt.text(toss[m] + x_offset, win[m], m, ha=ha)
        x_values.append(toss[m])
        y_values.append(win[m])

    plt.xlim(0, 130)
    plt.ylim(0, 130)
    plt.gca().set_facecolor(p.gray(shade=10))
    plt.grid(which="both", ls="--", alpha=0.5)
    plt.xlabel("number of times team has won the toss")
    plt.ylabel("number of wins")
    draw_correlation(x_values, y_values, 130)
    plt.tight_layout()
    plt.savefig("plot.png", dpi=150)
    plt.show()
Пример #4
0
def plot_match_played():
    p = Palette()
    data = get_data()
    matches = [x[0] for x in data]
    matches.extend([x[1] for x in data])
    ctr = Counter(matches)
    data_dict = defaultdict(list)
    for d in data:
        data_dict[d[0]].append(d[1])
    success = {k: len(v) for k, v in data_dict.items()}

    x_values, y_values = [], []
    for team in success:
        plt.scatter(ctr[team], success[team], color=p.blue())
        t = plt.text(ctr[team] + 3, success[team], team)
        y_values.append(success[team])
        x_values.append(ctr[team])
        manual_adjust(t)

    plt.xlim(0, 250)
    plt.ylim(0, 140)
    plt.gca().set_facecolor(p.gray(shade=10))
    plt.grid(which="both", ls="--", alpha=0.5)
    plt.xlabel("number of matches played")
    plt.ylabel("number of wins")
    draw_correlation(x_values, y_values, 250)
    plt.tight_layout()
    plt.savefig("plot.png", dpi=150)
    plt.show()
Пример #5
0
def other_networks():
    p = Palette()
    cw = ConfigWrapper("mouse")  # Change human or mouse
    links2 = convert_data(cw.links)
    info2 = convert_data(cw.info)

    # remove prefix for gene IDs which don't have name assigned

    renames = {"ENSDARG00000": ""}

    # Color Mapping
    mapping = {
        "Chl1": p.blue(),
        "Ptprg": p.red(shade=40),
        "CA16": p.red(shade=40)
    }

    gene = "Chl1"  # Change here other genes of interest
    depth = 1  # Change here when looking for multi-level connections
    g = generate_network(gene,
                         info=info2,
                         links=links2,
                         threshold=0.4,
                         depth=depth)
    plot_network(g,
                 replace_names=renames,
                 color_mapping=mapping)
Пример #6
0
def get_all_star_stat(bioproject: str):
    data = pd.read_csv("samples.csv")
    data = data[data["BioProject"] == bioproject]
    base = "/mnt/windows/Enigma/Zebrafish/data/rnaseq/star"
    all_times = list(sorted(set(data["time"].values)))
    values = []
    for time in all_times:
        temp = data[data["time"] == time]
        temp = temp[temp["genetic_background"] == "wild type"]
        time_temp = []
        for srr in temp["Run"].values:
            url = f"{base}/{srr}/{srr}_Log.final.out"
            reads, mapped, average = star_mapping_stat(url)
            time_temp.append(reads)
        values.append(sorted(time_temp))

    p = Palette()
    b = BarGroupedPlot(values)
    b.show_legend = False
    b.add_x_ticklabels(all_times)
    b.add_x_label("Hours Post Fertilization")
    b.add_y_label("Number of Input Reads")
    b.add_group_gap(3)
    b.colors = [p.red(shade=30), p.red(), p.red(shade=70)]
    b.draw()
    b.ax.set_title("Yost Lab")
    b.ax.grid(axis="y", ls="--", zorder=0)
    b.ax.set_ylim(0, 48889779)
    b.show()
Пример #7
0
def get_color_coded_style_sheet(max_depth=1):
    output = deepcopy(overview_stylesheet)

    class_selector_template = {
        'selector': '.depth_0',  # Depth of nodes that shall apply this color selector.
        'style': {
            'background-color': '#000000',  # Color in Hex
            'line-color': '#000000'
        }
    }

    color_palette = Palette("material")
    first_color_hex = color_palette.green()
    second_color_hex = color_palette.purple()

    # Generate a random list of gradient colors in Hex
    colors = color_palette.color_between(first_color_hex, second_color_hex, max_depth + 1)

    # colors = color_palette.random(max_depth + 2)

    for depth in range(max_depth + 1):
        class_selector = deepcopy(class_selector_template)
        class_selector['selector'] = '.depth_' + str(depth)
        class_selector['style']['background-color'] = colors[depth]
        class_selector['style']['line-color'] = colors[depth]
        output.append(class_selector)

    return output
Пример #8
0
def find_gene_networks():
    p = Palette()
    cw = ConfigWrapper("zebrafish")
    links2 = convert_data(cw.links)
    info2 = convert_data(cw.info)

    # remove prefix for gene IDs which don't have name assigned

    renames = {"ENSDARG00000": ""}

    # Color Mapping
    mapping = {
        "chl1a": p.blue(),
        "ptprga": p.red(shade=40),
        "ca16b": p.red(shade=40)
    }

    gene = "cdc25"  # Change here other genes of interest
    depth = 1  # Change here when looking for multi-level connections
    g = generate_network(gene,
                         info=info2,
                         links=links2,
                         threshold=0.4,
                         depth=depth)
    plot_network(g,
                 replace_names=renames,
                 color_mapping=mapping)
Пример #9
0
def plot_field_decision():
    p = Palette()
    data = get_data()
    dec = [x[2] for x in data if x[3] == "field"]
    dec = Counter(dec)
    win = Counter([x[0] for x in data])

    x_values, y_values = [], []
    for m in win:
        plt.scatter(dec[m], win[m], color=p.yellow())
        ha = "left"
        x_offset = 1
        if m in ["Hyderabad", "Punjab"]:
            ha = "right"
            x_offset = -1
        plt.text(dec[m] + x_offset, win[m], m, ha=ha)
        x_values.append(dec[m])
        y_values.append(win[m])

    plt.xlim(0, 80)
    plt.ylim(0, 130)
    plt.gca().set_facecolor(p.gray(shade=10))
    plt.grid(which="both", ls="--", alpha=0.5)
    plt.xlabel("number of time team decided to field after winning the toss")
    plt.ylabel("number of wins")
    draw_correlation(x_values, y_values, 80)
    plt.tight_layout()
    plt.savefig("plot.png", dpi=150)
    plt.show()
Пример #10
0
def save_history(hist, name):
    train_loss = hist.history['loss']
    val_loss = hist.history['val_loss']
    p = Palette()
    plt.plot(train_loss, color=p.red(), label='Train Loss')
    plt.plot(val_loss, color=p.blue(), label='Validation Loss')
    plt.title("Train and Validation Loss Curve")
    plt.legend()
    plt.savefig(name)
Пример #11
0
def volcano_plot(filename: str,
                 *,
                 ax: plt.Axes = None,
                 lfc_col: str = DESEQ2_LOG2_CHANGE,
                 pad_col: str = DESEQ2_PADJ,
                 threshold: float = 0.05,
                 use_threshold: bool = True,
                 color_col: str = None,
                 normal_color: Union[str, tuple] = Palette().blue(),
                 sign_color: Union[str, tuple] = Palette().red(),
                 scatter_options: dict = None,
                 add_labels: bool = True):
    d = pd.read_csv(filename)
    # Sanity check
    try:
        t = d[lfc_col], d[pad_col]  # Temporary variable to check
    except KeyError as e:
        raise KeyError(f"Please check your input data. Your input data "
                       f"should have columns for log fold change "
                       f"'{DESEQ2_LOG2_CHANGE}' "
                       f"and adjusted p value '{DESEQ2_PADJ}' column. If "
                       f"your column names are different, please provide "
                       f"them with argument 'lfc_col' and 'pad_col'"
                       f"") from e

    d = d.fillna(0)  # type: pd.DataFrame
    # Generate temporary column name
    temp = f"temp_col{np.random.randint(1000, 9000)}"
    colors = f"temp_color{np.random.randint(1000, 8000)}"

    d[temp] = d[pad_col].map(lambda x: -np.log10(x))
    d[colors] = normal_color

    # Use different color for values below threshold
    if use_threshold:
        d.loc[d[pad_col] < threshold, colors] = sign_color

    # If explicit color column is given, use those colors
    if color_col is not None:
        colors = color_col

    # If no axes is given, generate default one
    if ax is None:
        _, ax = plt.subplots()

    opts = {"marker": "."}
    if scatter_options is not None:
        opts = {**opts, **scatter_options}

    ax.scatter(d[lfc_col], d[temp], color=d[colors], **opts)

    if add_labels:
        ax.set_ylabel("-Log$_{10}$ Adj P value")
        ax.set_xlabel("Log$_2$ Fold change")
Пример #12
0
def plot_pca():
    p = Palette()
    data = get_data()
    win = Counter()
    toss = Counter()
    bat = Counter()
    field = Counter()
    ground = Counter()
    grounds = {"Jaipur": "Rajasthan",
               "Rajkot": "Gujarat",
               "Chandigarh": "Punjab"}
    for d in data:
        win.update({d[0]})
        if d[0] == d[2]:
            toss.update({d[0]})
        if d[3] == "bat":
            bat.update({d[0]})
        else:
            field.update({d[0]})

        if d[0] == d[4][1]:
            ground.update({d[0]})
        if d[4][1] in grounds.keys():
            if d[0] == grounds[d[4][1]]:
                ground.update({grounds[d[4][1]]})

    values = defaultdict(list)
    labels = []
    for m in win:
        labels.append(m)
        values[m].append(win[m])
        values[m].append(toss[m])
        values[m].append(bat[m])
        values[m].append(field[m])
        values[m].append(ground[m])

    values = np.array(list(values.values())).transpose()
    pca = PCA()
    pca.fit_transform(values)
    var = pca.components_
    plt.scatter(var[0, :], var[1, :], color=p.red(shade=40))
    for i, m in enumerate(labels):
        plt.annotate(m, xy=(var[0, i] + 0.005, var[1, i]), )
    percentages = [round(x, 2) for x in pca.explained_variance_ratio_ * 100]
    plt.xlabel(f"PC1: {percentages[0]} %")
    plt.ylabel(f"PC2: {percentages[1]} %")
    plt.xlim(0, 0.51)
    plt.gca().set_facecolor(p.gray(shade=10))
    # plt.grid(which="both", ls="--", color=p.black(), alpha=0.5)
    plt.tight_layout()
    plt.savefig("plot.png", dpi=150)
    plt.show()
Пример #13
0
def draw_distance_flow():
    """
    Draws flow diagram of the content-based filtering
    """
    p = Palette()
    g = pgv.AGraph(dpi=150, pad=0.1, directed=True, fontname="IBM Plex Sans")

    g.node_attr["style"] = "filled"
    g.node_attr["shape"] = "box"
    g.node_attr["fillcolor"] = p.green(shade=20)
    g.node_attr["margin"] = "0.1"

    g.add_node("a",
               label=_generate_label(
                   "LabelEncoder",
                   "converts arbitrary words to categorical variables", 3))

    g.add_node("b",
               label=_generate_label(
                   "CountVector",
                   "counts the number of words of each category"))

    g.add_node("c1", label="tags", shape="plain", fillcolor=None)
    g.add_node("c2", label="categories", shape="plain", fillcolor=None)
    g.add_node("c3", label="rating", shape="plain", fillcolor=None)

    g.add_node("d",
               label=_generate_label("Distance Matrix", "cosine distance"))

    g.add_node("e",
               label=_generate_label("Sort and Filter",
                                     "according to distance w.r.t. input", 2))

    g.add_node("f", label="Prediction", fillcolor=p.blue(shade=30))

    g.add_edge("c", "a", minlen=3)
    g.add_edge("a", "b")
    g.add_edge("b", "d")
    g.add_edge("d", "e")
    g.add_edge("e", "f", minlen=3)

    g.add_edge("c1", "c")
    g.add_edge("c2", "c")
    g.add_edge("c3", "c")
    g.add_node("c", label="Bag of Words", fillcolor=p.blue(shade=30))
    g.add_node("c")

    g.add_subgraph(["c", "a"], rank="same")
    g.add_subgraph(["e", "f"], rank="same")
    g.layout("dot")
    g.draw("plot.png")
Пример #14
0
def set_random_primary_colors(groups):
    p = Palette("material")
    random_colors = p.random(no_of_colors=len(groups))
    i = 0
    for group in groups:
        if "color" in group or ("custom_attributes" in group and
                                "primary_color" in group["custom_attributes"]):
            continue
        custom_attributes = {}
        if "custom_attributes" in group:
            custom_attributes = group["custom_attributes"]

        custom_attributes["primary_color"] = random_colors[i]
        group["custom_attributes"] = custom_attributes
        i += 1
Пример #15
0
def draw_map():
    p = Palette("brewer")
    projection = crs.Mercator()
    cm = BrewerMap(matplotlib).rd_yl_bu(is_qualitative=True, no_of_colors=10)
    fig = plt.figure(figsize=(11, 9))
    ax = plt.subplot(111, projection=projection)
    ax.add_feature(cfeature.BORDERS, alpha=0.5)
    ax.add_feature(cfeature.COASTLINE, alpha=0.5)
    data = get_data()
    shapes = get_shapes()
    for country in shapes:
        ct = country.attributes['ADM0_A3']
        if ct in data:
            ax.add_geometries([country.geometry],
                              crs.PlateCarree(),
                              fc=cm(data[ct]),
                              alpha=0.8)
        else:
            ax.add_geometries([country.geometry],
                              crs.PlateCarree(),
                              fc=p.gray(shade=30),
                              alpha=0.8)

    # Colorbar
    divider = make_axes_locatable(ax)
    ax_cb = divider.new_horizontal(size="3%", pad=0.2, axes_class=plt.Axes)
    fig.add_axes(ax_cb)

    sm = plt.cm.ScalarMappable(cmap=cm, norm=plt.Normalize(0, 1))
    cb = plt.colorbar(sm, cax=ax_cb)
    cb.ax.set_yticklabels(["{:.1%}".format(i) for i in cb.get_ticks()])
    text = "Percentage of population using at least\nbasic drinking-water " \
           "services in 2017"
    ax.annotate(text,
                xy=(0.95, 0.05),
                xycoords="axes fraction",
                ha="right",
                va="bottom",
                fontsize=12,
                bbox=dict(fc=p.white(), pad=12, alpha=0.9))
    ax.annotate("Gray: Data not available.\nData: WHO World Health Statistics",
                xy=(1, -0.02),
                ha="right",
                xycoords="axes fraction",
                va="top",
                color=p.gray())
    plt.savefig("plot.png", dpi=150)
    plt.show()
Пример #16
0
    def __init__(self, data, fig: plt.Figure = None, log: Log = None):
        if log is None:
            log = Log()
        if fig is None:
            fig = plt.figure()

        self._log = log
        self.data = data
        self.palette = Palette()
        self._space = None
        self.fig = fig  # type: plt.Figure

        # Options
        self.use_dijkstra = True
        self.colors = None
        self.colors_mapping = None
        self.node_width = 1
        self.node_height = 1
        self.node_gap = 1
        self.node_placement = None
        self.line_decoration = True
        self.max_columns = None
        self.aspect_ratio = None
        self.font_size = 13

        self._ax = None
        self._text_options = None
        self._line_options = None
        self._fig_drawn = False
        self._all_nodes = None

        self._log.info("Network Plot is initialized")
Пример #17
0
def plot_fold_change(nr: NameResolver, genes: list):
    p = Palette()
    lab = "winata"
    method = "star"
    con = [24, 72]
    fn = nr.deseq2_results(method=method, lab=lab, condition=con, lfc=True)
    d = pd.read_csv(fn)
    d = convert_id_to_gene(nr, d)
    d = d[d['gene_id'].isin(genes)].fillna(1989)
    d = d[d[DESEQ2_PADJ] <= 0.05]
    d = d[d[DESEQ2_LOG2_CHANGE] > -1]
    d = d[d[DESEQ2_LOG2_CHANGE] < 1]
    plt.hist(d['log2FoldChange'].values, bins=50, color=p.ultramarine())
    plt.ylabel("Frequency")
    plt.xlabel("Log$_2$Fold Change")
    plt.title(f"Genes with Log$_2$Fold change (p<0.05)\n{lab} {method} {con}")
    plt.savefig("genes.png", dpi=300)
    plt.show()
Пример #18
0
def plot_ground():
    p = Palette()
    data = get_data()
    winner = Counter()
    gr = Counter()
    grounds = {"Jaipur": "Rajasthan",
               "Rajkot": "Gujarat",
               "Chandigarh": "Punjab"}
    for d in data:
        if d[0] == d[4][1]:
            gr.update({d[0]})
            winner.update({d[0]})
        if d[1] == d[4][1]:
            gr.update({d[1]})
        if d[4][1] in grounds.keys():
            gr.update({grounds[d[4][1]]})
            if d[0] == grounds[d[4][1]]:
                winner.update({d[0]})

    x_values, y_values = [], []
    for m in winner:
        plt.scatter(gr[m], winner[m], color=p.aqua())
        ha = "left"
        x_offset = 1
        va = "center"
        if m in ["Punjab", "Mumbai"]:
            ha = "right"
            va = "center"
            x_offset = -1
        plt.text(gr[m] + x_offset, winner[m], m, ha=ha, va=va)
        x_values.append(gr[m])
        y_values.append(winner[m])

    plt.xlim(30, 90)
    plt.ylim(20, 60)
    plt.gca().set_facecolor(p.gray(shade=10))
    plt.grid(which="both", ls="--", alpha=0.5)
    plt.xlabel("number of time team played on home ground")
    plt.ylabel("number of wins on home ground")
    draw_correlation(x_values, y_values, 90)
    plt.tight_layout()
    plt.savefig("plot.png", dpi=150)
    plt.show()
Пример #19
0
def plot_flow():
    """
    Generates the flow diagram of the Content-based and user-based filtering
    """
    p = Palette()
    g = pgv.AGraph(directed=True, dpi=150, pad=0.1, fontname="IBM Plex Sans")
    g.add_node("c", label="Tags, Categories and Ratings", shape="plaintext")
    g.add_node("b",
               label="<Distance Matrix<BR/> "
               "<FONT POINT-SIZE='10'>(content based "
               "filtering)</FONT>>",
               style="filled",
               margin="0.2",
               fillcolor=p.blue(shade=20),
               shape="box")
    g.add_node("a", label="Selected Movie", shape="plaintext")
    g.add_node("e", label="Similar Movies", shape="plaintext")
    g.add_node("d", label=r"Selected Movies\rand Ratings\r", shape="plaintext")
    g.add_node("f",
               label="<Neural Network<BR/>"
               "<FONT POINT-SIZE='10'>(user based "
               "filtering)</FONT>>",
               style="filled",
               margin="0.2",
               fillcolor=p.blue(shade=20),
               shape="box")
    g.add_node("g", label="Movies user\lmight like\l", shape="plaintext")
    g.add_node("h", label="Users and Ratings", shape="plaintext")

    g.add_edge("c", "b", style="dashed")
    g.add_edge("d", "f", minlen=3)
    g.add_edge("a", "b", minlen=3)
    g.add_edge("b", "e", minlen=3)
    g.add_edge("f", "g", minlen=3)
    g.add_edge("f", "h", dir="back", style="dashed")
    g.add_edge("b", "f", style="invis")

    g.add_subgraph(["a", "b", "e"], rank="same", name="s1", label="ss")
    g.add_subgraph(["d", "f", "g"], rank="same", name="s2")

    g.layout("dot")
    g.draw("plot.png")
Пример #20
0
def draw_network_flow():
    """
    Draws flow diagram for user-based filtering
    """
    p = Palette()
    g = pgv.AGraph(dpi=150,
                   pad=0.1,
                   rankdir="LR",
                   directed=True,
                   fontname="IBM Plex Sans")

    g.node_attr["style"] = "filled"
    g.node_attr["shape"] = "box"
    g.node_attr["fillcolor"] = p.green(shade=20)
    g.node_attr["margin"] = "0.1"
    g.node_attr["height"] = 0.9

    g.add_node("a1", label="user-id", shape="plain", fillcolor=None)
    g.add_node("a2", label="movie-id", shape="plain", fillcolor=None)

    g.add_node("a",
               label=_generate_label(
                   "User-Movie Matrix",
                   "Generates vectors representing user and movies", 3),
               fillcolor=p.blue(shade=30))
    g.add_node("b", label="Neural Network")

    g.add_node("c",
               label=_generate_label(
                   "Prediction",
                   "Rating of input movie based on user's rating history", 3),
               fillcolor=p.blue(shade=30))

    g.add_edge("a", "b")
    g.add_edge("b", "c")
    g.add_edge("a1", "a")
    g.add_edge("a2", "a")
    g.layout("dot")
    g.draw("plot.png")
Пример #21
0
def plot_plasmid_features(
    plasmid_length: int,
    features: List[Dict[str, Any]],
    figure_width: int = 5,
    palette: Optional[Palette] = None,
) -> Tuple[SubplotBase, Tuple[Any, Any]]:
    """Plots features in a circular dna sequence.

    Args:
        plasmid_length (int): Number of nucleotide bases of the plasmid sequence

        features (List[Dict[str, Any]]): Features as obtained from TeselaGen DNA Sequence object

        figure_width (int, optional): Width size of figure. Defaults to 5.

        palette (Optional[Palette], optional): A SecretColors color palette. \
            Defaults to None, meaning `Palette("material")` will be used.

    Returns:
        Tuple[AxesSubplot, Tuple[Any, Any]]: Axes and a tuple with Graphic features data
    """
    # Define random color palette
    if palette is None:
        palette = Palette("material")
    colors = palette.cycle()

    # From 'forward' create a 'strand' field if does not exist
    if 'strand' not in features[0]:
        features = deepcopy(features)
        for feat in features:
            feat.update({'strand': 1 * feat['forward']})

    # Create feat objects
    plot_feats = [
        GraphicFeature(
            start=feat['start'],
            end=feat['end'],
            strand=feat['strand'],
            label=feat['name'],
            color=next(colors),
        ) for i, feat in enumerate(features)
    ]

    # Make graphic record and plot
    record = CircularGraphicRecord(sequence_length=plasmid_length,
                                   features=plot_feats)
    ax, _ = record.plot(figure_width=figure_width)

    # return ax, (features_levels, labels_data)
    return record.plot(ax)
Пример #22
0
def plot_network(data,
                 max_cols=None,
                 show=True,
                 color_mapping: dict = None,
                 replace_names: dict = None) -> NetworkPlot:
    p = Palette()
    network_data = []
    if len(data) > len(p.get_color_list):
        colors = p.random(no_of_colors=len(data))
    else:
        colors = p.get_color_list[:len(data)]
        if len(data) == 1:
            colors = [colors]

    mapping_colors = {}

    for pair in data:
        mapping_colors[pair[0]] = colors[len(mapping_colors.keys())]
        for gene in pair[1]:
            network_data.append([
                _replace_parts(pair[0], replace_names),
                _replace_parts(gene, replace_names), 1
            ])

    n = NetworkPlot(network_data)
    if max_cols is not None:
        n.max_columns = max_cols
    n.colors = p.gray(shade=30)
    if color_mapping is None:
        n.colors_mapping = mapping_colors
    else:
        n.colors_mapping = color_mapping
    n.line_decoration = False

    if show:
        n.show()
    return n
Пример #23
0
def get_data():
    p = Palette()
    _, ax = plt.subplots(1, 1)
    df = pd.read_csv("data/budget.csv")
    df["color"] = p.gray(shade=15)
    df = df.set_index("entity")
    df.loc["Scientific Departments", "color"] = p.red()
    df = df.reset_index()
    df = df.sort_values(by="amount", ascending=False)
    squarify.plot(df["amount"].to_list(),
                  label=df["entity"].to_list(),
                  color=df["color"].to_list(),
                  pad=True)
    show = df.head(7)["entity"].to_list()
    for child in ax.get_children():
        if isinstance(child, Text):
            child.set_fontsize("13")
            child.set_fontname("IBM Plex Sans")
            if child.get_text() not in show:
                child.set_text("")
            else:
                child.set_fontsize(11)
                child.set_color(p.gray())
                if child.get_text() == "Rural Development":
                    child.set_text("Rural\nDevelopment")
                elif child.get_text() == "Transfer to States":
                    child.set_text("Transfer\nto States")
    handles = [
        Patch(fc=p.red(), label="Scientific Departments"),
        Patch(fc=p.gray(shade=15), label="Other Categories"),
    ]
    ax.set_axisbelow(True)
    plt.legend(handles=handles,
               loc="lower center",
               ncol=3,
               bbox_to_anchor=(0.5, -0.15))
    plt.title("The Union Budget 2021 Expenditure Estimates")
    plt.annotate("Source: indiabudget.gov.in",
                 xy=(0.5, -0.18),
                 xycoords="axes fraction",
                 ha="center",
                 color=p.gray())
    plt.tight_layout()
    plt.axis('off')
    plt.savefig("plot.png", dpi=150)
    plt.show()
Пример #24
0
def plot_movies():
    """
    Generates histogram of movies available in MovieLens Database
    """
    p = Palette()
    plt.rcParams['axes.facecolor'] = p.gray(shade=10)
    df = get_movies()
    df = df.fillna("")
    df = df[df[YEAR] != ""]
    df[YEAR] = df[YEAR].map(
        lambda x: "".join([y for y in x if str(y).isnumeric()]))
    df[YEAR] = pd.to_numeric(df[YEAR])
    df = df[df[YEAR] < 3020]
    total_movies = len(df)
    df = df.groupby(by=YEAR).count().sort_index().reset_index()
    data = dict(zip(df[YEAR], df[MOVIE_ID]))
    plt.bar(range(0, len(data.keys())),
            data.values(),
            width=1,
            color=p.aqua(shade=40),
            zorder=3)
    labels = [int(x) for x in data.keys()][::5]
    plt.xticks(range(0, len(labels) * 5, 5), labels, rotation=90)
    plt.ylabel("Frequency")
    plt.xlabel("Year", labelpad=10)
    plt.grid(ls="--", color=p.gray(shade=30), zorder=-1)
    rc('text', usetex=True)
    plt.annotate(f"MovieLens Dataset\n"
                 r"\small{(Total movies: " + str(total_movies) + r")}",
                 (0.1, 0.8),
                 xycoords="axes fraction",
                 fontsize=13,
                 bbox=dict(fc=p.white(), lw=0.1, pad=10))

    plt.tight_layout()
    plt.savefig("plot.png", dpi=150)
    plt.show()
Пример #25
0
#  Data from
#  https://www.alltime-athletics.com

from collections import Counter, defaultdict

import cartopy.crs as crs
import cartopy.feature as cfeature
import cartopy.io.shapereader as shpreader
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
from SecretColors import Palette
from SecretColors.cmaps import TableauMap
from scipy.stats import gaussian_kde

p = Palette()
color_shades = [20, 30, 40, 50]
colors = [p.green(shade=x) for x in color_shades]
cm = TableauMap(matplotlib).from_list(colors)


def get_data(filename):
    c = 0
    data = []
    ctr = Counter()
    with open(filename) as f:
        for line in f:
            line = line.strip().split(" ")
            line = [x for x in line if len(x) > 0]
            time = float(line[1].replace("A", ""))
            name = ""
Пример #26
0
#  Author: Rohit Suratekar
#  Organisation: SecretBiology
#  Website: https://github.com/secretBiology/SecretPlots
#  Licence: MIT License
#  Creation: 19/09/19, 10:41 AM
#
#
# All Shapes will be accessed from this script

import matplotlib.patches as patches
import matplotlib.pyplot as plt
import numpy as np
from SecretColors import Palette
from matplotlib.path import Path

palette = Palette()


class Rectangle:
    def __init__(self,
                 x,
                 y,
                 width,
                 height,
                 rotation: float = 0.0,
                 align: str = "center",
                 **kwargs):
        self._x = x
        self._y = y
        self._height = height
        self._width = width
Пример #27
0
#  data:
#  https://www.who.int/malaria/areas/elimination/malaria-free-countries/en/

import pandas as pd
import csv
import cartopy.crs as crs
import cartopy.feature as cfeature
import cartopy.io.shapereader as shpreader
import matplotlib
import matplotlib.pyplot as plt
from SecretColors import Palette
from matplotlib.patches import Patch

matplotlib.rc("font", family="IBM Plex Sans")

p = Palette('brewer')


def get_data() -> pd.DataFrame:
    df = pd.read_csv("data/malaria_certified.csv")
    df = df.set_index("country")
    countries = pd.read_csv("data/country-codes.csv")
    countries = countries.set_index("name")
    df = pd.concat([df, countries], axis=1, join="inner")
    col = "Alpha-3 code"
    df = df[[col, "certified", "never"]]
    df = df.rename_axis("country")
    df = df.reset_index()
    return df

Пример #28
0
# Shape file source:
# Shijith Kunhitty
# https://groups.google.com/forum/#!topic/datameet/12L5jtjUKhI

import cartopy.crs as ccrs
import geopandas
import matplotlib
import matplotlib.animation as animation
import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt
import pandas as pd
from SecretColors import Palette
from SecretColors.cmaps import ColorMap
from matplotlib.animation import FuncAnimation

p = Palette()

color_list = p.blue(no_of_colors=20, starting_shade=30, ending_shade=100)
bm = ColorMap(matplotlib).from_list(color_list, is_qualitative=True)


class MetaDivision:
    """
    Simple class for holding data
    """
    def __init__(self, row):
        self.name = row["ST_NM"]
        self.geometry = row["geometry"]
        self.value = None

    @property
Пример #29
0
 def palette(self) -> Palette:
     if self._palette is None:
         self._palette = Palette(show_warning=False)
     return self._palette
Пример #30
0
#  Analysis related to world unemployment data
#  Data Source: https://data.worldbank.org/indicator/SL.UEM.TOTL.ZS

import cartopy.crs as crs
import cartopy.feature as cfeature
import cartopy.io.shapereader as shpreader
import matplotlib
import matplotlib.animation as animation
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from SecretColors import Palette
from SecretColors.cmaps import BrewerMap
from matplotlib.animation import FuncAnimation

p = Palette()
cm = BrewerMap(matplotlib).pu_rd(is_qualitative=True)


def draw_map(i, ax, countries):
    ax.clear()
    year = i + 2000
    data = get_data(year)
    if len(data) == 0:
        return
    max_um = max(data.values())
    ax.add_feature(cfeature.OCEAN, color=p.blue(shade=30))
    ax.add_feature(cfeature.BORDERS, alpha=0.5)
    ax.add_feature(cfeature.COASTLINE, alpha=0.5)

    for country in countries: