Esempio n. 1
0
    def _tune_plot(self, temp_kwargs: Dict[str, Any]) -> None:
        """
        Change stylistic parameters of the plot.
        """
        # axes parameters
        self.ax_object.set_ylim(0, 100)
        self.ax_object.set_ylabel(
            'Cumulative % AA representation',
            fontsize=temp_kwargs["y_label_fontsize"],
            color='k',
            labelpad=0,
            rotation=90,
        )
        self.ax_object.yaxis.set_major_formatter(ticker.PercentFormatter())

        self.ax_object.set_xlabel(
            'Amino acid position',
            fontsize=temp_kwargs["x_label_fontsize"],
            color='k',
            labelpad=4,
        )
        self.ax_object.set_xticklabels(self.positions)

        plt.title(temp_kwargs['title'],
                  fontsize=temp_kwargs['title_fontsize'],
                  fontname='Arial',
                  color='k',
                  pad=20)

        # legend
        plt.legend(markerscale=0.5,
                   frameon=False,
                   framealpha=0,
                   handlelength=0.75,
                   handletextpad=0.25,
                   ncol=len(self.df_percentage),
                   columnspacing=0.75,
                   bbox_to_anchor=(0.5, 1.09),
                   loc='upper center')
def build_feature_importance(model_name, width, height):    
    model_response = client.describe_model(ModelName=model_name)
    predictions = json.loads(model_response['ModelMetrics'])['predicted_ranges']
    start_date = pd.to_datetime(model_response['EvaluationDataStartTime']).tz_localize(None)
    end_date = pd.to_datetime(model_response['EvaluationDataEndTime']).tz_localize(None)

    df = pd.DataFrame(predictions)
    expanded_results = expand_results(df)
    num_values = len(list(expanded_results.columns))
    
    colors = set_aws_stylesheet()
    rank_df = pd.DataFrame(np.mean(expanded_results), columns=['value']).sort_values(by='value', ascending=True).tail(15)
    values = list(rank_df['value'])
    threshold = 1 / num_values
    signal_color = {v: assign_color(v, threshold, colors) for v in values}
    signal_color = list(signal_color.values())
    y_pos = np.arange(rank_df.shape[0])

    fig = plt.figure(figsize=(width/dpi, height/dpi), dpi=dpi)
    ax = plt.subplot(111)
    ax.barh(y_pos, rank_df['value'], align='center', color=signal_color)
    ax.set_yticks(y_pos)
    ax.set_yticklabels(rank_df.index)
    ax.xaxis.set_major_formatter(mtick.PercentFormatter(1.0))

    # Add the values in each bar:
    for i, v in enumerate(values):
        if v > threshold:
            t = ax.text(0.001, i, f'{v*100:.2f}%', color='#000000', fontweight='bold', verticalalignment='center')
            t.set_bbox(dict(facecolor='#FFFFFF', alpha=0.5, pad=0.5, boxstyle='round4'))

    ax.vlines(x=1/num_values, ymin=-0.5, ymax=np.max(y_pos) + 0.5, linestyle='--', linewidth=2.0, color=colors[0])
    ax.vlines(x=1/num_values, ymin=-0.5, ymax=np.max(y_pos) + 0.5, linewidth=4.0, alpha=0.3, color=colors[0])
    ax.set_title('Aggregated signal importance over the evaluation period')
    
    svg_io = StringIO()
    fig.savefig(svg_io, format="svg", bbox_inches='tight')
    
    return svg_io.getvalue().replace('DejaVu Sans', 'Amazon Ember')
Esempio n. 3
0
def plot_squares(directory):

    data = load_simple(directory, 'square')

    f = list()
    for (rec, pos) in data:
        w, h = rpack.enclosing_size(rec, pos)
        a = sum(x * y for x, y in rec)
        f.append(a / (w * h))

    with PdfPages(os.path.join(args.output_dir, 'squares.pdf')) as pdf:

        fig, ax = plt.subplots(tight_layout=True)
        ax.plot(list(range(1, len(f) + 1)), f)
        ax.grid(True)
        ax.yaxis.set_major_formatter(mtick.PercentFormatter(xmax=1))
        ax.xaxis.set_major_locator(mtick.MultipleLocator(10))
        ax.set_title('Packing density, squares')
        ax.set_xlabel('Rectangle max side length (n)')
        pdf.savefig(figure=fig)
        for ext in IMG_EXT:
            plt.savefig(os.path.join(args.output_dir,
                                     f'squares_summary.{ext}'))
        fig.clf()
        plt.close()

        for n, (rec, pos) in enumerate(data, start=1):
            if n == 1:
                title = ', square 1x1'
            elif n == 2:
                title = ', squares 1x1 + 2x2'
            else:
                title = f', squares 1x1 ... {n}x{n}'
            p = PlotPacking(rec, pos, title=title)
            while p.feed():
                pass
            pdf.savefig(figure=p.fig)
            p.fig.clf()
            plt.close()
Esempio n. 4
0
 def binomial(data, n):
     x = []
     y = []
     for x2 in range(n):
         y.append(distributions.binomial_dist(data, n, x2 + 1) * 100)
         x.append(x2)
     fig, ax = plt.subplots(figsize=(12, 6))
     cm = plt.cm.get_cmap('YlGn')
     colors = cm(y)
     ax.bar(x, y, color=colors)
     ax.set_xlabel(
         'Dias en los que se llegue temprano dentro de un lapso de {} días'.
         format(n))
     plt.xticks(np.arange(min(x), max(x), 5.0))
     ax.set_ylabel('Probabilidad de que suceda')
     ax.yaxis.set_major_formatter(mtick.PercentFormatter())
     ax.set_title(
         'Distribución binomial de la probabilidad de días en los que se llegue temprano'
     )
     fig.tight_layout()
     plt.savefig('img/binomial.png')
     plt.show()
Esempio n. 5
0
 def poisson(data, n, top, state):
     x = []
     y = []
     for x2 in range(top):
         y.append(distributions.poisson_dist(data, n, x2 + 1) * 100)
         x.append(x2)
     fig, ax = plt.subplots(figsize=(12, 6))
     cm = plt.cm.get_cmap('YlOrRd')
     colors = cm(y)
     ax.bar(x, y, color=colors)
     ax.set_xlabel(
         'Cantidad de personas contagiadas dentro de una muestra de {} personas'
         .format(n))
     plt.xticks(np.arange(min(x), max(x), 5.0))
     ax.set_ylabel('Probabilidad de contagio')
     ax.yaxis.set_major_formatter(mtick.PercentFormatter())
     ax.set_title(
         'Distribución de Poisson de la probabilidad de personas contagiadas por COVID-19 en {}'
         .format(state))
     fig.tight_layout()
     plt.savefig('img/poisson.png')
     plt.show()
Esempio n. 6
0
def plot_tenure_churn(data):
    
    # ---- Churn Rate by Tenure Group
    task10 = (pd.crosstab(data['tenure_group'], data['churn_label'], normalize=True)*100).round(2)

    ax = task10.plot(kind = 'bar', color=['#53a4b1','#c34454'], figsize=(8, 6))

    # Plot Configuration
    ax.yaxis.set_major_formatter(mtick.PercentFormatter())
    plt.axes().get_xaxis().set_label_text('')
    plt.xticks(rotation = 360)
    plt.legend(['Retain', 'Churn'],fancybox=True,shadow=True)
    plt.title('Churn Rate by Tenure Group')

    # Save png file to IO buffer
    figfile = BytesIO()
    plt.savefig(figfile, format='png')
    figfile.seek(0)
    figdata_png = base64.b64encode(figfile.getvalue())
    result = str(figdata_png)[2:-1]

    return(result)
def plot(data):
  for key, param in rc_params.items():
    mpl.rc(key, **param)
  # Collect plottable keys
  modes = []
  # y = []
  for key, value in data.items():
    if key == 'epoch' or key == 'epochs':
      epochs = value
    elif isinstance(value, dict):
      modes.append(key)
  #   elif isinstance(value, (list, tuple)):
  #     y.append(key)
  # assert len(y) == 0 or len(modes) == 0
  fig, ax = plt.subplots(1, 2, figsize=(12, 6), sharex=True)

  for mode in modes:
    ax[0].plot(epochs, data[mode]['loss'], label=mode)
    ax[1].plot(epochs, data[mode]['accuracy'], label=mode)

  ax[0].set_xlabel('Epoch')
  ax[0].set_ylabel('Loss')
  ax[0].grid()
  ax[0].legend()

  ax[1].set_xlabel('Epoch')
  ax[1].set_ylabel('Accuracy')
  ax[1].yaxis.set_major_formatter(mtick.PercentFormatter(1.0))
  ax[1].grid()
  ax[1].legend()

  teacher_name = data['teacher_name'].replace('_', r'\_')
  student_name = data['student_name'].replace('_', r'\_')
  plt.suptitle(f'Teacher: {teacher_name}\nStudent: {student_name}')
  plt.tight_layout()
  # plt.subplots_adjust(top=0.9)
  plt.tight_layout(rect=[0, 0.0, 1, 0.93])

  return ax
Esempio n. 8
0
    def plot_stacked_profiles(self, constrained=True):
        """plot the repartition of the species among the profiles, normalized to
        100%

        Parameters
        ----------
        constrained : boolean, default True
            use the constrained run or not

        Returns
        -------
        ax : the axe
        """
        pmf = self.pmf

        df = pmf.get_total_specie_sum(constrained=constrained)

        df = df.sort_index(axis=1)

        colors = [get_sourceColor(c) for c in df.columns]

        fig, ax = plt.subplots(1, 1, figsize=(12, 4))
        df.plot(kind="bar", stacked=True, color=colors, ax=ax)

        xticklabels = [t.get_text() for t in ax.get_xticklabels()]
        ax.set_xticklabels(pretty_specie(xticklabels), rotation=90)
        ax.set_xlabel("")

        ax.yaxis.set_major_formatter(mticker.PercentFormatter())
        ax.set_ylabel("Normalized contribution (%)")
        ax.set_ylim(0, 100)

        h, l = ax.get_legend_handles_labels()
        h = reversed(h)
        l = reversed(l)
        ax.legend(h, l, loc="upper left", bbox_to_anchor=(1, 1), frameon=False)
        fig.subplots_adjust(bottom=0.275, top=0.96, left=0.09, right=0.83)

        return ax
Esempio n. 9
0
def pretty_plot(pdobj, percent=False, decimals=0, commas=False, save=False, **kwargs):
    import matplotlib.pyplot as plt
    import matplotlib.ticker as mtick
    
    plt.style.use('seaborn-whitegrid')
    
    import seaborn as sns
    sns.set_palette('Paired')
    
    plt.rcParams.update({
        'axes.spines.right': False,
        'axes.spines.top': False,
        'axes.spines.left': False,
        'axes.spines.left': True,
        'font.size': 15,
        'figure.figsize': (15, 7),
        'axes.grid': False,
        'figure.dpi': 300.0
    })
    
    plt.figure()
    
    ax = pdobj.plot(**kwargs)
    ax.tick_params(axis='x', pad=10)
    ax.tick_params(axis='y', pad=10)
    
    if percent:
        import matplotlib.ticker as mtick
        ax.yaxis.set_major_formatter(mtick.PercentFormatter(decimals=decimals, xmax=1))
    
    else:
        if commas:
            ax.yaxis.set_major_formatter(mtick.FuncFormatter(lambda x, p: format(int(x), ',')))
    
    if save != False:
        plt.savefig(save)
        print('figure saved: {save}'.format(save=save))
    
    return ax
Esempio n. 10
0
def plot_hist(observations, reviews_per_observation, positive_reviews):
    """ Plots histogram.
    
    Parameters:
        observations (list): 2D list with the outcomes of all the experiments
        reviews_per_observation (int): Total reviews per experiment or observation. For e.g, 10 reviews per
        experiment.
    
    Returns:
        None 
    """
    freq_hist_of_outcomes = []
    for events in observations:
        freq_hist_of_outcomes.append([event.count(1) for event in events])

    # Display plots in 3 columns
    plot_rows = (len(freq_hist_of_outcomes) // 3) + 1
    fig, axes = plt.subplots(nrows=plot_rows, ncols=3, figsize=(20, 20))
    fig.subplots_adjust(hspace=0.5)

    for ax, freq_lst in zip(axes.ravel(), freq_hist_of_outcomes):
        _, _, patches = ax.hist(
            freq_lst,
            bins=[i for i in range(1, reviews_per_observation + 1)],
            edgecolor="black",
            linewidth=1.2,
            density=True)
        for thispatch in patches:
            xy = thispatch.xy
            if (xy[0] == positive_reviews):
                thispatch.set_facecolor('r')

        ax.set_title("%s Observations" % (len(freq_lst)))
        ax.set_xlabel("No of positive reviews out of %d reviews" %
                      (reviews_per_observation))
        ax.set_ylabel("Percentage")
        ax.yaxis.set_major_formatter(ticker.PercentFormatter(xmax=1))

    plt.show()
Esempio n. 11
0
def plot_internet(data):

    # ---- Internet Service Customer
    inet = pd.crosstab(
        data['internet_service'], data['churn_label'], normalize=True) * 100

    ax = inet.plot(kind='barh', color=['#53a4b1', '#c34454'], figsize=(8, 6))

    # Plot Configuration
    ax.xaxis.set_major_formatter(mtick.PercentFormatter())
    plt.legend(['Retain', 'Churn'], fancybox=True, shadow=True)
    plt.axes().get_yaxis().set_label_text('')
    plt.title('Internet Service Customer')

    # Save png file to IO buffer
    figfile = BytesIO()
    plt.savefig(figfile, format='png')
    figfile.seek(0)
    figdata_png = base64.b64encode(figfile.getvalue())
    result = str(figdata_png)[2:-1]

    return (result)
Esempio n. 12
0
def mde_plot(data):
    fig, ax = plt.subplots(figsize=(10, 5), dpi=100)
    ax.plot('MDE',
            'Days',
            data=data,
            linewidth=2,
            solid_capstyle='round',
            color='#014d64')

    # Formatting the tick labels

    ax.yaxis.set_major_formatter(mtick.FuncFormatter(y_format))
    ax.xaxis.set_major_formatter(mtick.PercentFormatter(1.0))

    # Formatting the axes labels
    ax.set_xlabel('Minimum detectable effect', **roboto)
    ax.set_ylabel('')

    # Set limit to reasonable amount of time
    if ax.get_ylim()[1] > 60:
        ax.set_ylim([0, 7 * max_runtime * 1.2])

    # Set x-lim
    x_limit = data[data['Weeks'] <= 1]['MDE'].min() * 2
    ax.set_xlim([0, x_limit])

    for week in range(1, max_runtime + 1):
        plot_mde_marker(data, week, ax)

    # Clean up layout of graph, removing borders
    ax.yaxis.grid(True)
    ax.spines['left'].set_visible(False)
    ax.spines['right'].set_visible(False)
    ax.spines['top'].set_visible(False)

    # Hiding the y-axis
    ax.axes.get_yaxis().set_visible(False)

    st.write(fig)
Esempio n. 13
0
def plot_bar_graph(df, title):
    fig, ax = plt.subplots()
    df.sort_values(by="FY21", inplace=True, ascending=False)
    labels = ["\n".join(wrap(l, 35)) for l in df.index]

    fig.suptitle(title, fontsize=12)

    _df = pd.melt(df.iloc[:, :-1], ignore_index=False).reset_index()
    sns.barplot(data=_df,
                y="index",
                x="value",
                hue="variable",
                ax=ax,
                saturation=1,
                orient="h")

    ax.legend(loc="upper center",
              bbox_to_anchor=(0.5, -0.17),
              ncol=2,
              fontsize=8)

    ax.set_xlabel("% Students Answering Positive")

    ax.set_yticklabels(labels, fontsize=10)

    ax.set_ylabel("Question")
    ax.xaxis.set_major_formatter(mtick.PercentFormatter(1, decimals=0))
    for p in ax.patches:
        width = p.get_width()  # get bar length
        ax.text(
            width + 0.01,  # set the text at 1 unit right of the bar
            p.get_y() +
            p.get_height() / 2,  # get Y coordinate + X coordinate / 2
            "{:.0%}".format(width),  # set variable to display, 2 decimals
            ha="left",  # horizontal alignment
            va="center",
        )  # vertical alignment
    return fig
def main():
    fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(20, 5))

    for idx, path in enumerate(LAYER_1_PATHS):
        x, y = read_csv(path)
        ax1.plot(x, y, label=f"Seed {idx}")

    for idx, path in enumerate(LAYER_3_PATHS):
        x, y = read_csv(path)
        ax2.plot(x, y, label=f"Seed {idx}")

    for idx, path in enumerate(LAYER_5_PATHS):
        x, y = read_csv(path)
        ax3.plot(x, y, label=f"Seed {idx}")

    ax1.set_title("1 layer generator")
    ax2.set_title("3 layer generator")
    ax3.set_title("5 layer generator")

    for ax in (ax1, ax2, ax3):
        ax.set_xticks(np.arange(0, 80001, 10000))
        ax.set_xlim(0, 80000)
        ax.set_ylim(0, 85)
        ax.grid(True, linestyle='-.')
        ax.legend(loc=4)

        # Format x ticks
        f = mtick.ScalarFormatter(useOffset=False, useMathText=True)
        g = lambda x, _: "${}$".format(f._formatSciNotation('%1e' % x))
        ax.xaxis.set_major_formatter(mtick.FuncFormatter(g))

        # Format y ticks
        ax.yaxis.set_major_formatter(mtick.PercentFormatter())

        ax.set_xlabel("Number of training batches")
        ax.set_ylabel("Test accuracy")

    plt.show()
Esempio n. 15
0
def roseplot(d,
             x,
             bin_edges,
             opening=1.0,
             dirnames=False,
             xlabel=None,
             cmap=None,
             ax=None):
    bins = len(bin_edges) - 1
    n = len(d)
    hists = np.empty((bins, NDIRS))
    lbls = []
    for i in range(bins):
        x1 = bin_edges[i]
        x2 = bin_edges[i + 1]
        dbin = d[np.logical_and(x1 <= x, x < x2)]
        *hists[i], last = np.histogram(dbin, bins=NDIRS + 1,
                                       range=RANGE)[0] / n
        hists[i, 0] += last
        lbls.append(f'{x1:.1f}–{x2:.1f}')
    if ax is None:
        ax = plt.subplot(polar=True)
    ax.set_theta_direction(-1)
    ax.set_theta_zero_location("N")
    if dirnames:
        ax.set_xticklabels(['N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW'])
    if isinstance(cmap, str) or cmap is None:
        cmap = plt.get_cmap(cmap)
    colors = cmap(np.linspace(0, 1, bins))
    bottoms = np.empty_like(hists)
    bottoms[0] = 0
    bottoms[1:] = np.cumsum(hists[:-1], 0)
    width = opening * BARWIDTH
    for hist, color, lbl, bottom in zip(hists, colors, lbls, bottoms):
        ax.bar(DIRS, hist, width=width, color=color, label=lbl, bottom=bottom)
    ax.legend(loc='center left', bbox_to_anchor=(1.2, 0.5), title=xlabel)
    ax.yaxis.set_major_formatter(mtick.PercentFormatter(xmax=1, decimals=0))
    return ax
Esempio n. 16
0
    def make_graph_prob(self, name, values, num):
        """
        項目ごとに確率分布のグラフを作成する

         parameters
        ----------
        name : result directory

        values : dict

        returns
        ----------
        """
        fig = plt.figure(figsize=(10,5))
        colorlist = ["r", "g", "b", "c", "m", "brown", "grey", "darkblue"]
        ax = fig.add_subplot(1, 1, 1)
        key_num = len(values.keys())
        if not key_num:
            return
        key = list(values)
        all_results = np.zeros((key_num, len(values[key[0]])))
        for i in range(key_num):
            all_results[i] = values[key[i]]
        mean = np.mean(all_results, axis=0)
        std = np.std(all_results, axis=0)
        plt.plot(range(mean.shape[0]), mean, linestyle='solid', color=colorlist[num])
        
        if re.search('accuracy', name) and not self._regression:
            plt.fill_between(range(mean.shape[0]) ,np.clip(mean - std,0,1), np.clip(mean + std,0,1),facecolor=colorlist[num],alpha=0.3)
            ax.yaxis.set_major_formatter(ticker.PercentFormatter(xmax=1.0))
        else:
            plt.fill_between(range(mean.shape[0]) ,mean - std, mean + std, facecolor=colorlist[num],alpha=0.3)
        plt.grid(which='major',color='gray',linestyle='-')
        plt.xlabel("epoch")
        plt.ylabel(name)
        plt.savefig(self.log_dir + '/{}_prob.png'.format(name))
        plt.close()
        return
Esempio n. 17
0
def bg_deriv_hist(df, bins, path):
    '''
    Save a histogram of the blood glucose derivatives to disk.

    Parameters
    ----------
    df: pandas DataFrame
        dataframe
    bins: list of int
        List of integers representing bin boundaries

    Returns
    -------
    None
    '''
    data = h_get_bg_derivative(df)
    fig = plt.figure(figsize=(10,10))
    gs = gridspec.GridSpec(1,1)
    #   subplot areas
    bg_deriv = plt.subplot(gs[:,:])
    bg_deriv.set_title("Glucose Derivative by Count")
    counts = pd.cut(data['Derivative'], bins=bins).value_counts(sort=False)
    x = counts.index.values.astype(str)
    y = counts/counts.sum() * 100

    bg_deriv.grid(axis='y', which='both')
    bg_deriv.bar(x,y,width=0.95)
    bg_deriv.set_title("Glucose Derivative by Count")
    bg_deriv.set_ylabel("Percentage of vals in range")
    bg_deriv.yaxis.set_major_formatter(mtick.PercentFormatter())
    bg_deriv.set_xlabel("Range")
    for label in bg_deriv.xaxis.get_ticklabels():
        label.set_rotation(90)
    plt.savefig(
        path + 'bg_deriv_hist.png', 
        dpi=DPI, 
        transparent=False)
    plt.close(fig)
Esempio n. 18
0
def img_draw(category: list, plot_params: list, use_percent=True):
    """
    the image creation function
    :param category: list, the data of the x axis
    :param plot_params: list, the items are smaller list, each list contains the data of each plot,
    the label of each plot and the type, there will be two types: 1 refers to plot, 2 refers to bar
    :param use_percent: set the y axis using percent format
    :return: the byte stream of the image
    """
    imgdata = BytesIO()
    plt.rcParams['font.sans-serif'] = ['SimHei']
    plt.rcParams['axes.unicode_minus'] = False
    xLocator = MultipleLocator(1)
    fig, axe = plt.subplots(figsize=(7, 4))
    for info in plot_params:
        if info[2] == 1:  # use the plot method
            axe.plot(category, info[0], label=info[1])
        elif info[2] == 2:  # use the bar method
            axe.bar(category, info[0], label=info[1])
    axe.xaxis.set_major_locator(xLocator)

    if use_percent:
        yticks = mtick.PercentFormatter(xmax=1, decimals=0)
        axe.yaxis.set_major_formatter(yticks)

    # box = axe.get_position()
    # axe.set_position([box.x0 - box.width * 0.03, box.y0 + box.height * 0.1,
    #                        box.width, box.height * 0.9])
    # axe.legend(loc='upper center', bbox_to_anchor=(0.5, -0.05), ncol=len(plot_params),
    #            fontsize=9, frameon=False)
    axe.legend(loc='best', fontsize=9, frameon=False)

    fig.savefig(imgdata, format='png')
    imgdata.seek(0)
    plt.clf()
    plt.cla()
    plt.close()
    return imgdata
def genre_boxplot(lst, column):

    #Create a figure
    fig, ax = plt.subplots(figsize=figsize_nb)

    #Create a boxplot for all genres for a specific column, in this case column is ROI. Do now show outliers, increase the line thickness for all parts of the boxplot, and set the colors for the lines.
    ax.boxplot([
        lst[1][column], lst[6][column], lst[2][column], lst[0][column],
        lst[5][column], lst[4][column], lst[3][column], lst[7][column]
    ],
               showfliers=False,
               boxprops=dict(linewidth=2.0, color='blue'),
               whiskerprops=dict(linestyle='-', linewidth=2.0, color='black'),
               medianprops=dict(linewidth=2.0, color='red'),
               capprops=dict(linewidth=2.0))

    #Create axis labels and a title and shift x-axis label down
    ax.set_title(
        'Distribution of Returns on Investment for Movie Genres for all Budget Ranges'
    )
    ax.set_ylabel('Return on Investment')
    ax.set_xlabel('Movie Genre')
    ax.xaxis.labelpad = 20
    fig.suptitle("")

    #Format axis labels to contain percent signs, change labels on x-axis tick marks, and rotate the labels
    ax.yaxis.set_major_formatter(mtick.PercentFormatter())
    plt.xticks([1, 2, 3, 4, 5, 6, 7, 8], [
        'Adventure', 'Thriller', 'Comedy', 'Action', 'Family', 'Drama',
        'Documentary', 'Other'
    ])
    plt.xticks(rotation=25)

    #Show horizontal grid lines
    plt.grid(which='major', axis='x')

    #Show plot
    return plt.show()
Esempio n. 20
0
def plot_lin_reg_for_cfr_for_region(data, region='Global', t=7):
    '''
    Plot linear regression for current fatality rate over time for region.
       
    Parameters: 
        data (dict): clean data set including all types and regions
        region (str): region string, optional
        t (int): time, optional
    '''
    cfr_df = cfr_for_region(data, region, t)
    lf_df = lin_reg_for_time_series(cfr_df)
    
    fig, ax = plt.subplots()
    ax.yaxis.set_major_formatter(mtick.PercentFormatter(1))

    if lf_df is not None: 
        ax.plot(lf_df, color='red')
    else:
        print('Could not calculate CFR Linear Regression for this region.')
    ax.plot(cfr_df, color='gray')

    fig.autofmt_xdate()
    plt.show()
Esempio n. 21
0
def plot_phone(data):
    
    # ---- Phone Service Customer
    plot_phone_res=pd.crosstab(data['phone_service'],
                data['churn_label']
               )
    #ax = plot_phone_res.plot(kind = 'barh', color=['#53a4b1','#c34454'], figsize = (8,6))
    ax = plot_phone_res.plot(kind = 'barh', color=warna, figsize = (8,6))

    # Plot Configuration
    ax.xaxis.set_major_formatter(mtick.PercentFormatter())
    plt.legend(['Retain', 'Churn'],fancybox=True,shadow=True)
    plt.axes().get_yaxis().set_label_text('')
    plt.title('Phone Service Customer')
    
    # Save png file to IO buffer
    figfile = BytesIO()
    plt.savefig(figfile, format='png', transparent=True)
    figfile.seek(0)
    figdata_png = base64.b64encode(figfile.getvalue())
    result = str(figdata_png)[2:-1]

    return(result)
Esempio n. 22
0
def display_average_daily_gain():
    fig = plt.figure()
    plt.style.use('ggplot')

    ax = fig.add_subplot(111)

    x = days_of_the_week
    average_daily_gain = [round(i, len(x)) for i in days_of_the_week_mean]
    y_colors = ['green' if i > 0 else "red" for i in average_daily_gain]
    x_pos = [i for i, _ in enumerate(x)]
    ax.bar(x_pos, average_daily_gain, color=y_colors)
    ax.yaxis.set_major_formatter(mtick.PercentFormatter())

    plt.xlabel("Day of the week")
    plt.ylabel("Average Percentage gain (Close - Open)")
    plt.title("{0} data from {1} to {2}".format(stock_symbol,
                                                start_date.date(),
                                                end_date.date()))

    plt.xticks(x_pos, x)

    plt.show()
    plt.close()
Esempio n. 23
0
def region_corr(df, ax=None):
    title = 'Correttezza'
    if ax is None:
        f = plt.figure(figsize=(9, 8))
        ax = f.add_subplot(111)
    df = df.sort_values('Regione')
    ax.barh(df.Regione, df.Ratio * 100, color=co.ab_colors['giallo'])
    for i, i_name in enumerate(df.Regione):
        i_lbl = "{0}".format(i_name)
        ax.text(1, i, i_lbl, color="k", va="center", size=16)

    __barh_ax_set(ax, title)
    ax.xaxis.set_major_formatter(ticker.PercentFormatter())
    ax.set_xticks([0, 50, 75, 100])
    xgrid = ax.xaxis.get_gridlines()
    xgrid[1].set_color('r')
    xgrid[1].set_ls('--')
    xgrid[1].set_lw(2)
    xgrid[2].set_color('r')
    xgrid[2].set_ls('--')
    xgrid[2].set_lw(2)
    ax.set_xlim([0, 100])
    ax.set_yticks([])
Esempio n. 24
0
def plot_demographic_data(qualifier, ranges):
    """
    Plot demographic data for the given qualifier.

    Arguments:
        qualifier: a String, representing one of the six dictionary keys in
            data_dimensions.
        ranges: a List of Lists, containing a list of data for each axis in
            the graph.
    """

    # Initialize the graph
    fig = plt.figure()
    ax = fig.add_axes([0, 0, 1, 1])
    ax.bar(ranges[0], ranges[1])
    ax.yaxis.set_major_formatter(plts.PercentFormatter(decimals=0))

    # Assign axis labels and a title and then show the graph.
    plt.xlabel(graph_titles[qualifier][0])
    plt.xticks(rotation=90, horizontalalignment="left")
    plt.ylabel(graph_titles[qualifier][1])
    plt.title(graph_titles[qualifier][2])
    plt.show()
def HB_Genres_Graph(DataFrame):

    # Create a figure
    fig = plt.figure(figsize=figsize_nb)

    # Use sns.barplot with 'Genre' on the x axis and 'Median ROI' on the y axis. Give it a unique color
    ax = sns.barplot(data=DataFrame, x='Genre', y='Median ROI', color='blue')

    # Give the graph a title and lables
    ax.set(title='Median ROI for High Budget Films by Genre',
           xlabel='Genres',
           ylabel='Return on Investment')

    # Change the xticks so that the tick labels are not on each other
    ax.set_xticklabels(ax.get_xticklabels(),
                       rotation=45,
                       horizontalalignment='right')

    # Change the yticks so that the labels are in %
    ax.yaxis.set_major_formatter(mtick.PercentFormatter())

    # return the figure
    return plt.show()
Esempio n. 26
0
def plot_pcnt_change_since_2016(df):
    """First written for Summary of Part 1"""
    SCDAO_cases_series = pd.Series(SCDAO_cases)[year_range]
    df.loc["Total Cost of Carceral State in Suffolk"] = df.sum()
    df.loc["Cost Per SCDAO Case"] = df.sum()/SCDAO_cases_series
    df.loc["Number of SCDAO Cases"] = SCDAO_cases_series
    df.loc["MA GAA"] = pd.Series(MA_GAA)[year_range]/10**6
    for row in df.index:
        df.loc[row + " Fractional Change"] = [x / df.loc[row, 2016] for x in df.loc[row]]
    melted = df[df.index.str.contains("Fractional")].reset_index().melt(id_vars=["Category"])
    palette = sns.color_palette("Paired", melted["Category"].nunique())
    fig, ax = plt.subplots(1, 1)
    plt.ticklabel_format(style='plain', axis='y')
    p = sns.lineplot(x="variable", y="value", palette=palette,
                     hue="Category", sort=False, data=melted)
    p.set_title("Percent Change Since 2016", fontsize=24)
    ax.legend(frameon=False, loc="upper left", fontsize=16)
    plt.xticks(year_range)
    p.set_ylabel("Percent Change", fontsize=20)
    ax.yaxis.set_major_formatter(mtick.PercentFormatter())
    p.set_xlabel("")
    p.tick_params(labelsize=20)
    plt.show()
Esempio n. 27
0
def figureTLSCipherAdoption(axis, rankingName, rankingNameLong, ipVersion,
                            tlsVersion, category):
    global cipher
    df = cipher.loc[((cipher['RANKING_NAME'] == rankingName) &
                     (cipher['IP_VERSION'] == ipVersion)), :].copy()
    df = df.dropna()
    df = df.groupby(by='CIPHER', as_index=False).agg({
        'TOTAL_TIME': 'count'
    }).sort_values(by='CIPHER')

    labels = []
    for cipherName in df['CIPHER'].tolist():
        cipherNameNew = cipherName.split('_')
        cipherNameNew = ' '.join(cipherNameNew[:3]) + '\n' + ' '.join(
            cipherNameNew[3:])
        labels.append(cipherNameNew)

    axis.bar(df['CIPHER'], df['TOTAL_TIME'])
    axis.set_xlabel('Cipher')
    axis.set_ylabel('Adoption rate of sample')
    axis.yaxis.set_major_formatter(ticker.PercentFormatter(100))
    plt.xticks(rotation=90)
    plt.xticks(range(0, len(labels)), labels)
Esempio n. 28
0
def plot_demographic_churn(data):

    # ---- Churn Rate by Demographic
    demographic_churn = pd.crosstab(
        index=[data['gender'], data['senior_citizen'], data['dependents']],
        columns=data['churn_label'],
        normalize=True) * 100

    ax = demographic_churn.plot(kind='barh',
                                color=['#53a4b1', '#c34454'],
                                figsize=(10, 6),
                                stacked=True)

    # Plot Configuration
    label = [
        'Single Adult Woman', 'Adult Woman w/ Family', 'Single Senior Woman',
        'Senior Woman w/ Family', 'Single Adult Man', 'Adult Man w/ Family',
        'Single Senior Man', 'Senior Man w/ Family'
    ]

    ax.xaxis.set_major_formatter(mtick.PercentFormatter())
    plt.axes().get_xaxis().set_label_text('')
    plt.axes().get_yaxis().set_label_text('demographic')
    plt.xticks(rotation=360)
    plt.legend(['Retain', 'Churn'], fancybox=True, shadow=True)
    plt.title('Churn Rate by Customer Demographic')
    ax.set_yticklabels(label)
    plt.grid()

    # Save png file to IO buffer
    figfile = BytesIO()
    plt.savefig(figfile, format='png')
    figfile.seek(0)
    figdata_png = base64.b64encode(figfile.getvalue())
    result = str(figdata_png)[2:-1]

    return (result)
def graph_utility_scaled_cap_colour(metrics: Metrics, path_prefix: str):
    fig = plt.figure()
    ax = fig.gca()

    sources_utilities = {(b.source, b.capability) for b in metrics.buffers}

    grouped_utility = {
        (asrc, acap): [
            (b.t, b.utility / b.max_utility)

            for b in metrics.buffers
            if asrc == b.source and acap == b.capability
        ]
        for (asrc, acap) in sources_utilities
    }

    sequential_cmaps = [seaborn.mpl_palette(name, n_colors=len(metrics.agent_names)) for name in ("Greens", "Purples")]
    cmap_for_cap = {
        c: sequential_cmaps[c]
        for c in {int(c[1:]) for c in metrics.capability_names}
    }

    for ((src, cap), utilities) in sorted(grouped_utility.items(), key=lambda x: x[0]):
        X, Y = zip(*utilities)
        ax.plot(X, Y, label=f"{src} {cap}", color=cmap_for_cap[int(cap[1:])][metrics.agent_names.index(src)])

    ax.set_ylim(0, 1)

    ax.set_xlabel('Time (secs)')
    ax.set_ylabel('Normalised Utility (\\%)')
    ax.yaxis.set_major_formatter(ticker.PercentFormatter(xmax=1, symbol=''))

    ax.legend(bbox_to_anchor=(1.5, 1), loc="upper right", ncol=2)

    savefig(fig, f"{path_prefix}norm-utility-cc.pdf")

    plt.close(fig)
Esempio n. 30
0
def make_figure(data, samples):
    import matplotlib.ticker as mtick

    fig = plt.figure(figsize=(15, 9))
    grid = plt.GridSpec(2, 3, wspace=0.1, hspace=0.25)
    axes = (
        plt.subplot(grid[0, :]),
        plt.subplot(grid[1, 0]),
        plt.subplot(grid[1, 1]),
        plt.subplot(grid[1, 2]),
    )
    plot_1988(data, samples, ax=axes[0])
    plot_trend(data, samples, ax=axes[1])
    plot_seasonality(data, samples, ax=axes[2])
    plot_week(data, samples, ax=axes[3])

    for ax in axes:
        ax.axhline(y=1, linestyle="--", color="gray", lw=1)
        if not ax.get_subplotspec().is_first_row():
            ax.set_ylim(0.65, 1.35)

        if not ax.get_subplotspec().is_first_col():
            ax.set_yticks([])
            ax.set_ylabel("")
        else:
            ax.yaxis.set_major_formatter(mtick.PercentFormatter(xmax=1))
            ax.set_ylabel("Relative number of births")

    axes[0].set_title("Special day effect")
    axes[0].set_xlabel("Day of year")
    axes[1].set_title("Long term trend")
    axes[1].set_xlabel("Year")
    axes[2].set_title("Year seasonality")
    axes[2].set_xlabel("Day of year")
    axes[3].set_title("Day of week effect")
    axes[3].set_xlabel("Day of week")
    return fig