Пример #1
0
            def draw_barchart(Time):
                df_frame = (df[df["date"].eq(Time)].sort_values(
                    by="counts", ascending=True).tail(num_of_elements))
                ax.clear()

                normal_colors = dict(
                    zip(df["country"].unique(), rgb_colors_opacity))
                dark_colors = dict(zip(df["country"].unique(),
                                       rgb_colors_dark))

                ax.barh(
                    df_frame["country"],
                    df_frame["counts"],
                    color=[normal_colors[x] for x in df_frame["country"]],
                    height=0.8,
                    edgecolor=([dark_colors[x] for x in df_frame["country"]]),
                    linewidth="6",
                )

                dx = float(df_frame["counts"].max()) / 200

                for i, (value, name) in enumerate(
                        zip(df_frame["counts"], df_frame["country"])):
                    ax.text(
                        value + dx,
                        i + (num_of_elements / 50),
                        "    " + name,
                        size=14,
                        weight="bold",
                        ha="left",
                        va="center",
                        fontdict={"fontname": "Trebuchet MS"},
                    )
                    ax.text(
                        value + dx * 10,
                        i - (num_of_elements / 50),
                        f"    {value:,.0f}",
                        size=14,
                        ha="left",
                        va="center",
                    )

                time_unit_displayed = re.sub(r"\^(.*)", r"", str(Time))
                ax.text(
                    1.0,
                    1.14,
                    time_unit_displayed,
                    transform=ax.transAxes,
                    color="#666666",
                    size=14,
                    ha="right",
                    weight="bold",
                    fontdict={"fontname": "Trebuchet MS"},
                )
                # ax.text(-0.005, 1.06, 'Number of confirmed cases', transform=ax.transAxes, size=14, color='#666666')
                ax.text(
                    -0.005,
                    1.14,
                    "Number of {} cases ".format(field),
                    transform=ax.transAxes,
                    size=14,
                    weight="bold",
                    ha="left",
                    fontdict={"fontname": "Trebuchet MS"},
                )

                ax.xaxis.set_major_formatter(
                    ticker.StrMethodFormatter("{x:,.0f}"))
                ax.xaxis.set_ticks_position("top")
                ax.tick_params(axis="x", colors="#666666", labelsize=12)
                ax.set_yticks([])
                ax.set_axisbelow(True)
                ax.margins(0, 0.01)
                ax.grid(which="major", axis="x", linestyle="-")

                plt.locator_params(axis="x", nbins=4)
                plt.box(False)
                plt.subplots_adjust(
                    left=0.075,
                    right=0.75,
                    top=0.825,
                    bottom=0.05,
                    wspace=0.2,
                    hspace=0.2,
                )
Пример #2
0
def plot_rt(result, ax, state_name):

    ax.set_title(f"{state_name}")

    palette = cm.get_cmap("RdBu_r")
    newcolors = palette(np.linspace(0, 1, 25))
    cmap = ListedColormap(newcolors)

    color_mapped = lambda y: np.clip(y, .5, 1.5) - .5

    index = result['ML'].index.get_level_values('date')
    values = result['ML'].values

    # Plot dots and line
    ax.plot(index, values, c='k', zorder=1, alpha=.25)
    ax.scatter(index,
               values,
               s=40,
               lw=.5,
               c=cmap(color_mapped(values)),
               edgecolors='k',
               zorder=2)

    # Aesthetically, extrapolate credible interval by 1 day either side
    lowfn = interp1d(
        date2num(index.to_pydatetime()),  #date2num(index),
        result['Low_95'].values,
        bounds_error=False,
        fill_value='extrapolate')

    highfn = interp1d(
        date2num(index.to_pydatetime()),  #date2num(index),
        result['High_95'].values,
        bounds_error=False,
        fill_value='extrapolate')

    extended = pd.date_range(start=pd.Timestamp('2020-03-01'),
                             end=index[-1] + pd.Timedelta(days=1))

    ax.fill_between(
        extended,
        lowfn(date2num(extended.to_pydatetime())),  #date2num(extended)
        highfn(date2num(extended.to_pydatetime())),  #date2num(extended)
        color='k',
        alpha=.1,
        lw=0,
        zorder=3)

    ax.axhline(1.0, c='k', lw=1, label='$R_t=1.0$', alpha=.25)

    # Formatting
    ax.xaxis.set_major_locator(mdates.MonthLocator())
    ax.xaxis.set_major_formatter(mdates.DateFormatter('%b'))
    ax.xaxis.set_minor_locator(mdates.DayLocator())

    ax.yaxis.set_major_locator(ticker.MultipleLocator(1))
    ax.yaxis.set_major_formatter(ticker.StrMethodFormatter("{x:.1f}"))
    ax.yaxis.tick_right()
    ax.spines['left'].set_visible(False)
    ax.spines['bottom'].set_visible(False)
    ax.spines['right'].set_visible(False)
    ax.margins(0)
    ax.grid(which='major', axis='y', c='k', alpha=.1, zorder=-2)
    ax.margins(0)
    ax.set_ylim(0.0, 5.0)
    ax.set_xlim(
        pd.Timestamp('2020-03-01'),
        result.index.get_level_values('date')[-1] + pd.Timedelta(days=1))
    fig.set_facecolor('w')
def annotate_heatmap(im,
                     data=None,
                     valfmt="{x:.1f}",
                     textcolors=["black", "black"],
                     threshold=None,
                     **textkw):
    """
    A function to annotate a heatmap.

    Parameters
    ----------
    im
        The AxesImage to be labeled.
    data
        Data used to annotate.  If None, the image's data is used.  Optional.
    valfmt
        The format of the annotations inside the heatmap.  This should either
        use the string format method, e.g. "$ {x:.2f}", or be a
        `matplotlib.ticker.Formatter`.  Optional.
    textcolors
        A list or array of two color specifications.  The first is used for
        values below a threshold, the second for those above.  Optional.
    threshold
        Value in data units according to which the colors from textcolors are
        applied.  If None (the default) uses the middle of the colormap as
        separation.  Optional.
    **kwargs
        All other arguments are forwarded to each call to `text` used to create
        the text labels.
    """

    if not isinstance(data, (list, np.ndarray)):
        data = im.get_array()
    # Normalize the threshold to the images color range.
    if threshold is not None:
        threshold = im.norm(threshold)
    # else:
    #     threshold = 0 #im.norm(data.max()) / 2.

    # Set default alignment to center, but allow it to be
    # overwritten by textkw.
    kw = dict(horizontalalignment="center",
              verticalalignment="center",
              fontsize=8)
    kw.update(textkw)

    # Get the formatter in case a string is supplied
    if isinstance(valfmt, str):
        valfmt = ticker.StrMethodFormatter(valfmt)

    # Loop over the data and create a `Text` for each "pixel".
    # Change the text's color depending on the data.
    texts = []
    for i in range(data.shape[0]):
        for j in range(data.shape[1]):
            if threshold is None:
                kw.update(color=textcolors[0])
                text = im.axes.text(j, i, valfmt(data[i, j], None), **kw)
            else:
                kw.update(color=textcolors[int(
                    im.norm(abs(data[i, j])) > threshold)])
                text = im.axes.text(j, i, valfmt(data[i, j], None), **kw)

            texts.append(text)

    return texts
Пример #4
0
 def test_basic(self, format, input, expected):
     fmt = mticker.StrMethodFormatter(format)
     assert fmt(*input) == expected
Пример #5
0
for r in rows:
    for c in cols:
        ax = fig.add_subplot(gs[i])
        ax = sns.boxplot(data=DFpivot[DFpivot["duration"] == r],
                         y=c,
                         **params,
                         palette=colorPaletteAR)
        m = int(np.ceil(DFpivot[DFpivot["duration"] == r][c].max()))
        plt.title(f"{r}\n{c}")
        plt.xlabel("NodeClasses", fontsize=14)
        for tick in ax.xaxis.get_major_ticks():
            tick.label.set_fontsize(13)
        plt.ylabel("Occupancy Score (log scale)", fontsize=14)
        ax.yaxis.set_ticks(range(m + 1))
        ax.yaxis.set_major_formatter(
            mticker.StrMethodFormatter("$10^{{{x:.0f}}}$"))
        ax.yaxis.set_ticks([
            np.log10(x) for p in range(m)
            for x in np.linspace(10**p, 10**(p + 1), 10)
        ],
                           minor=True)
        add_stat_annotation(ax,
                            data=DFpivot[DFpivot["duration"] == r],
                            **params,
                            y=c,
                            box_pairs=boxPairs,
                            test='Mann-Whitney',
                            loc='inside',
                            line_height=0,
                            text_offset=1,
                            line_offset=0.05)
all_trajects_average = pandas.Series(
    all_trajects_average[all_trajects_average.columns[0]])

all_trajects_end_vals = pandas.read_csv(
    'data/100k trajectories, 60 flips, all end values.csv',
    index_col=False,
    header=None)
all_trajects_end_vals = pandas.Series(
    all_trajects_end_vals[all_trajects_end_vals.columns[0]])

# One trajectory - one hours of flips.

fig, ax = plt.subplots()
one_trajectory[0:61].plot()
ax.set_xlabel("Number of flips")
ax.yaxis.set_major_formatter(tick.StrMethodFormatter('${x:,.0f}'))
ax.set_title("One trajectory - 60 flips")
fig.savefig('images/One trajectory - 60 flips.png')

# 20 trajectories - one hours of flips. On the same plot.

fig, ax = plt.subplots()
for i in range(20):
    make_trajectory(60).plot()
ax.set_xlabel("Number of flips")
ax.yaxis.set_major_formatter(tick.StrMethodFormatter('${x:,.0f}'))
ax.set_title("20 trajectories - 60 flips")
fig.savefig('images/20 trajectories - 60 flips.png')

# Now show one trajectory for one week of flips. First, create a masking which will allow the green portion of the plot.
Пример #7
0
    def _draw(self, ax, plot_timestamps, date_formatter):

        if self.time_plot:
            self._reindex(plot_timestamps)
            ax.xaxis.set_major_formatter(date_formatter)
            date_index = np.arange(len(plot_timestamps))

        lines = []

        ax2 = None
        if self.secondary_y is not None and len(self.secondary_y):
            ax2 = ax.twinx()

        for data in self.data_list:
            if _VERBOSE: print(f'plotting data: {data.name}')
            if ax2 and data.name in self.secondary_y:
                line = _plot_data(ax2, data)
            else:
                line = _plot_data(ax, data)
            lines.append(line)

        for date_line in self.date_lines:  # vertical lines on time plot
            line = draw_date_line(ax, plot_timestamps, date_line.date,
                                  date_line.line_type, date_line.color)
            if date_line.name is not None: lines.append(line)

        for horizontal_line in self.horizontal_lines:
            line = draw_horizontal_line(ax, horizontal_line.y,
                                        horizontal_line.line_type,
                                        horizontal_line.color)
            if horizontal_line.name is not None: lines.append(line)

        for vertical_line in self.vertical_lines:
            line = draw_vertical_line(ax, vertical_line.x,
                                      vertical_line.line_type,
                                      vertical_line.color)
            if vertical_line.name is not None: lines.append(line)

        self.legend_names = [data.name for data in self.data_list]
        self.legend_names += [
            date_line.name for date_line in self.date_lines
            if date_line.name is not None
        ]
        self.legend_names += [
            horizontal_line.name for horizontal_line in self.horizontal_lines
            if horizontal_line.name is not None
        ]
        self.legend_names += [
            vertical_line.name for vertical_line in self.vertical_lines
            if vertical_line.name is not None
        ]

        if self.ylim: ax.set_ylim(self.ylim)
        if (len(self.data_list) > 1
                or len(self.date_lines)) and self.display_legend:
            ax.legend([line for line in lines if line is not None], [
                self.legend_names[i]
                for i, line in enumerate(lines) if line is not None
            ],
                      loc=self.legend_loc)

        if self.log_y:
            ax.set_yscale('log')
            ax.yaxis.set_major_locator(mtick.AutoLocator())
        if self.y_tick_format:
            ax.yaxis.set_major_formatter(
                mtick.StrMethodFormatter(self.y_tick_format))

        ax.relim()
        ax.autoscale_view()

        if self.title: ax.set_title(self.title)
        if self.xlabel: ax.set_xlabel(self.xlabel)
        if self.ylabel: ax.set_ylabel(self.ylabel)
        if self.zlabel: ax.set_zlabel(self.zlabel)
Пример #8
0
    def create_daily_historical_value_plot(self, ownership=1, show=False):

        conn = sqlite3.connect(self.home_path + 'portfolio_history.db')
        cursor = conn.cursor()

        equities = [
            x[0] * ownership for x in cursor.execute('SELECT equity \
                    FROM portfolio_history \
                    ORDER BY portfolio_date').fetchall()
        ]
        dates = [
            dt.datetime.strptime(x[0], '%Y-%m-%d')
            for x in cursor.execute('SELECT portfolio_date \
                                          FROM portfolio_history \
                                          ORDER BY portfolio_date').fetchall()
        ]
        start_date = str(dates[0].date())
        dates = mdates.date2num(dates)
        upper_limit = float(max(equities)) * 1.2

        tries = 0
        url = 'https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol=SPY&outputsize=full&apikey={}'.format(
            self.av_API)
        while True:
            request = urllib2.Request(url,
                                      headers={'User-Agent': "Magic Browser"})
            temp = eval(urllib2.urlopen(request).read())
            if 'Time Series (Daily)' in temp:
                break  # exit if successful
            else:
                time.sleep(1)
                tries += 1
            if tries > 50:  # in case we're trying too many calls?
                break
        self.df = pd.DataFrame.from_dict(
            temp['Time Series (Daily)']).transpose()
        mask = self.df.index > start_date
        self.df = self.df.loc[mask]
        SPY_dates = [
            dt.datetime.strptime(x, '%Y-%m-%d') for x in self.df.index
        ]
        SPY_dates = mdates.date2num(SPY_dates)
        SPY_equities = [float(x) for x in self.df['5. adjusted close']]

        fig, ax = plt.subplots(figsize=(10, 6))
        # fig.suptitle('3-Month Portfolio Value', size='xx-large')

        ax.plot_date(dates, equities, '-', color='#3fcaff', lw=3)

        mondays = mdates.WeekdayLocator(mdates.MONDAY)
        months = mdates.MonthLocator()
        monthsFmt = mdates.DateFormatter("%b")
        ax.xaxis.set_major_locator(months)
        ax.xaxis.set_major_formatter(monthsFmt)
        ax.xaxis.set_minor_locator(mondays)
        ax.xaxis.set_minor_formatter(plt.NullFormatter())
        y_lower = math.floor(min(equities) / 1000) * 1000
        y_upper = math.ceil(max(equities) / 1000) * 1000
        increment = (y_upper - y_lower) / 5
        text_increment = increment * 0.7
        ax.set_yticks(np.arange(y_lower, y_upper, increment))
        ax.yaxis.set_major_formatter(mtick.StrMethodFormatter('${x:,.0f}'))
        ax.set_ylim(0, upper_limit)
        for tick in ax.get_xticklabels():
            tick.set_rotation(20)
        ax.grid(True)

        ratio = equities[0] / SPY_equities[0]
        SPY_relative_equities = [float(x) * ratio for x in SPY_equities]
        ax.plot_date(SPY_dates, SPY_relative_equities, '-', color='#A9A9A9')

        total_change = float(equities[-1]) - float(equities[0])
        total_perc_change = round(
            float(equities[-1]) / float(equities[0]) * 100 - 100, 1)
        if total_change >= 0:
            total_change = '+${0:,.2f}'.format(total_change)
        else:
            total_change = '-${0:,.2f}'.format(total_change)
        SPY_change = float(self.df['5. adjusted close'][-1]) - float(
            self.df['5. adjusted close'][0])
        SPY_perc_change = round(
            float(self.df['5. adjusted close'][-1]) /
            float(self.df['5. adjusted close'][0]) * 100 - 100, 1)
        if SPY_change >= 0:
            SPY_change = '+${0:,.2f}'.format(SPY_change)
        else:
            SPY_change = '-${0:,.2f}'.format(SPY_change)
        props = dict(boxstyle='round', facecolor='#3fcaff')
        s = '{} ({}%)'.format(total_change, total_perc_change)
        ax.text(dates[7],
                upper_limit - text_increment,
                'Portfolio value',
                bbox=props)
        ax.text(dates[7] + 33,
                upper_limit - text_increment,
                s,
                color='#3fcaff',
                weight=750)
        props = dict(boxstyle='round', facecolor='#D3D3D3')
        s = '{} ({}%)'.format(SPY_change, SPY_perc_change)
        ax.text(dates[7],
                upper_limit - text_increment * 2,
                'S&P value',
                bbox=props)
        ax.text(dates[7] + 33,
                upper_limit - text_increment * 2,
                s,
                color='#A9A9A9',
                weight=750)
        if SPY_perc_change > total_perc_change:
            s = 'S&P outperforming portfolio by {}%'.format(SPY_perc_change -
                                                            total_perc_change)
        else:
            s = 'Portfolio outperforming S&P by {}%'.format(total_perc_change -
                                                            SPY_perc_change)
        props = dict(boxstyle='round', facecolor='#ffffff')
        ax.text(dates[7], upper_limit - text_increment * 3, s, bbox=props)

        if show:
            plt.show()
        else:
            pylab.savefig(self.home_path +
                          'static/img/daily_historical_portfolio.png',
                          facecolor='w',
                          edgecolor='w')
            plt.close()
Пример #9
0
from matplotlib import rcParams
from matplotlib import ticker
from sklearn.linear_model import BayesianRidge
from sklearn.model_selection import train_test_split
%matplotlib inline   

#Scaling overall size
rcParams['figure.figsize'] = 20,10
#Grabbing our dataset
df = pd.read_csv('https://raw.githubusercontent.com/ObiP1/The-Future-Value-of-Homes/master/AverageHomeValues.csv')
df[df.columns[1:]] = df[df.columns[1:]].replace('[\$,]', '', regex=True).astype(float)

#Titles 
plt.title('Median Cost Of Maryland Homes', fontsize=30)
plt.ylabel('Median Price Of Home',fontsize=25)
plt.gca().yaxis.set_major_formatter(ticker.StrMethodFormatter('${x:,.0f}'))
plt.xlabel('Year', fontsize=25)

#implements the use of our data to forecast future prices
data_train = df[df.YEAR < 2020]
data_test = df[df.MED_COST >= 2020]

X_train,X_test,y_train,y_test=train_test_split(df.YEAR,df.MED_COST)
X_All = df.YEAR[:, np.newaxis]

X1=X_train
X2=X_test

#Mandatory Reshaping
X_train = X1.values.reshape(-1, 1)
X_test = X2.values.reshape(-1, 1)
Пример #10
0
import pandas as pd
import matplotlib.pyplot as plt; plt.style.use('ggplot')
import matplotlib.ticker as mtick

lbl = pd.read_csv('..\\cro55-prompt-test\\AmendedData\\LBL.csv')
colours = ['#1d1a1c', '#ee2a24']
lbl = lbl[['value', 'cell']]
lbl = lbl[lbl['value'] <= 100]

prompts = {'ctrl': [5, 25, 50], 'test': [10, 30, 60]}

## Histogram vs prompts
fig, ax = plt.subplots(1, 2, figsize=(10, 6), sharey=True)
bins = range(2, 100, 1)
currfmt = mtick.StrMethodFormatter('£{x:,.0f}')
for i, cell in enumerate(prompts.keys()):
    df = lbl[lbl['cell'] == cell]['value']
    df.plot.hist(color=colours[1], ax=ax[i], legend=False, bins=bins)
    ax[i].set_xlabel('Donation value')
    ax[i].xaxis.set_major_formatter(currfmt)
    for prompt_val in prompts[cell]:
        ax[i].vlines(prompt_val, 0, 50, color=colours[0], alpha=0.5)
    ax[i].set_title(cell)
plt.suptitle('CRO55 gift values vs prompts', fontsize=20)
fig.tight_layout(rect=[0, 0.03, 1, 0.95])
plt.savefig('Outputs\\GiftValuesvsPrompts.png')

# On vs off prompt
on_prompt = 0
for cell in prompts.keys():
Пример #11
0
def cost(costs, start_n, max_n, r, t):
    n = start_n
    # [[suppliers], [plants], [warehouses], [transportation]]
    data = np.zeros([4, t])
    for i in range(t):
        # Increase output using logistic growth (does nothing for i = 0)
        n = logistic_growth(start_n, max_n, r, i)

        # Calculate cost by segment per week and add to data
        data[0][i] = segment_cost(costs["suppliers"], n)
        data[1][i] = segment_cost(costs["plants"], n)
        data[2][i] = segment_cost(costs["warehouses"], n)
        trcosts = costs["transportation"]
        data[3][i] = n * trcosts["freightRate"] * trcosts["tripDistance"] * \
                                 trcosts["unitVolume"] / trcosts["freightVolume"]

    fig, axs = plt.subplots(2, 1)

    # Divide data through by `multiple`
    multiple = 1000
    reduced_data = data / multiple

    # Prepare for graph plotting
    suppliers = reduced_data[0]
    plants = reduced_data[1]
    warehouses = reduced_data[2]
    transportation = reduced_data[3]
    ind = [t for t in range(1, t + 1)]

    # Plot graph
    graph = axs[0]
    graph.bar(ind, suppliers, width=0.6, label="Suppliers", color="green")
    graph.bar(ind,
              plants,
              width=0.6,
              label="Plants",
              color="blue",
              bottom=suppliers)
    graph.bar(ind,
              warehouses,
              width=0.6,
              label="Warehouses",
              color="orange",
              bottom=suppliers + plants)
    graph.bar(ind,
              transportation,
              width=0.6,
              label="Transportation",
              color="red",
              bottom=suppliers + plants + warehouses)

    graph.set_xticks(ind, range(t))
    graph.set_ylabel("Cost in $%ds" % multiple)
    graph.yaxis.set_major_formatter(ticker.StrMethodFormatter('{x:,.0f}'))
    graph.set_xlabel("Week")
    graph.legend(loc="lower right")
    graph.set_title("Projected Cost per Week")

    # Add sums to original data
    data = np.append(data, [np.sum(data, axis=0)], axis=0)
    col = np.array([np.sum(data, axis=1)])
    data = np.concatenate((data, col.T), axis=1)
    formatted_data = [['{:,.0f}'.format(j) for j in i] for i in data]

    # Print data to console
    for i in range(t):
        print("Week {}: ${}".format(i + 1, formatted_data[4][i]))
    print("GRAND TOTAL: ${}".format(formatted_data[4][t]))

    segments = ["Suppliers", "Plants", "Warehouses", "Transportation"]

    # Print table
    axs[1].axis("tight")
    axs[1].axis("off")
    rows = segments
    rows.append("Total")
    columns = ["Week %d" % x for x in range(1, t + 1)]
    columns.append("Total")
    the_table = axs[1].table(cellText=formatted_data,
                             rowLabels=rows,
                             colLabels=columns,
                             loc='center')
    the_table.auto_set_font_size(False)
    the_table.set_fontsize(10)
    the_table.scale(1.3, 1.3)

    # Adjust layout to make room for the table:
    plt.subplots_adjust(left=0.3)

    plt.show()
Пример #12
0
def set_ax_format(ax,
                  axtitle=None,
                  axtitlesize=None,
                  axtitlebold=False,
                  xlabel=None,
                  ylabel=None,
                  hline=True,
                  hline_y=0.0,
                  xlim=None,
                  ylim=None,
                  xlabel_visible=True,
                  ylabel_visible=True,
                  y_axis_numeric=True,
                  minorticks='auto',
                  xticks=None,
                  xticklabels=None,
                  yticks=None,
                  yticklabels=None):
    # Backgroud and grid
    ax.set_facecolor('#d9d9d9')  # gray background
    ax.set_axisbelow(True)  # puts the grid behind the bars
    ax.grid(color='white', linestyle='dotted')
    # axtitle
    if axtitle is not None:
        weight = 'bold' if axtitlebold else 'normal'
        if axtitlesize is None:
            ax.set_title(axtitle,
                         size=plt.rcParams['font.size'] + 2,
                         weight=weight)
        else:
            ax.set_title(axtitle,
                         size=plt.rcParams['font.size'] + axtitlesize,
                         weight=weight)
    # xlabel
    if xlabel is not None and xlabel_visible:
        ax.set_xlabel(xlabel)
    else:
        ax.xaxis.label.set_visible(False)
    # ylabel
    if ylabel is not None and ylabel_visible:
        ax.set_ylabel(ylabel)
    else:
        ax.yaxis.label.set_visible(False)
    # xlim
    if xlim is not None:
        ax.set_xlim(xlim)
    # ylim
    if ylim is not None:
        ax.set_ylim(ylim)
    # hline
    if hline:
        ax.axhline(y=hline_y, color='#050505')
    if y_axis_numeric:
        (ax.get_yaxis().set_major_formatter(
            mticker.StrMethodFormatter('{x:,g}')))
    if minorticks == 'off':
        ax.minorticks_off()
    elif minorticks == 'on':
        ax.minorticks_on()
    if xticks is not None:
        ax.set_xticks(xticks)
    if yticks is not None:
        ax.set_yticks(yticks)
    if xticklabels is not None:
        ax.set_xticklabels(xticklabels)
    if yticklabels is not None:
        ax.set_yticklabels(yticklabels)
Пример #13
0
def choropleth_map(df,
                   cmap='viridis',
                   interval=None,
                   annotate=None,
                   relative=True,
                   colorbar_each_subplot=False,
                   add_percentages=True,
                   **kwargs):
    """
    Plot a choropleth map (=a map with countries colored according to a value)
    for each column of data in given pd.DataFrame.

    Parameters
    ----------
    df : pd.DataFrame or pd.Series
        Holding the values (required index: NUTS-3 codes)
    cmap : str or list, optional
        matplotlib colormap code(s)
    interval : tuple or str, optional
        if tuple: min/max-range e.g. (0, 1) | if str: find min/max autom.
    annotate: None, str or list
        If `str` or `list` used to write annotation on map, valid values are:
            'nuts3': annonate the nuts3-code
            `name` or `names`: annonate the name of the region
            `value` or `values`: annotate the passed values
            `percentage` or `percentages`: annonate the percentage of values
    relative : bool, optional
        Flag if to use relative values in <unit>/(km²) (default True) or
        to use absolutes values if False.
    colorbar_each_subplot : bool, optional
        Flag if to show a colorbar for each subplot (default False)
    add_percentages : bool, optional
        Flag if to add the percentage share into the axtitle (default True)
    """
    # Mend and/or transpose the data
    if isinstance(df, pd.Series):
        df = df.to_frame()
    if isinstance(df.index, pd.DatetimeIndex):
        df = transpose_spatiotemporal(df)
    if annotate is None or annotate == '':
        annotate = []
    if isinstance(annotate, str):
        annotate = [annotate]

    ncols = kwargs.get('ncols', 0)
    nrows = kwargs.get('nrows', 0)
    suptitle = kwargs.get('suptitle', None)
    axtitle = kwargs.get('axtitle', '')
    unit = kwargs.get('unit', '-')
    fontsize = kwargs.get('fontsize', 6)
    sep = kwargs.get('sep', '\n')
    color = kwargs.get('color', 'black')
    rem = nrows * ncols - len(df.columns)
    shape_source_api = kwargs.get('shape_source_api', True)

    if shape_source_api:
        # Perform request through RestfulAPI
        DE = database_shapes()
    else:
        # Load from local shape files
        base_year = cfg['base_year']
        if base_year >= 2016:
            nuts = 'NUTS_RG_01M_2016_4326_LEVL_3_DE'
        else:
            nuts = 'NUTS_RG_01M_2013'
        DE = (gpd.read_file(gpd.datasets.get_path(nuts)).set_index('NUTS_ID'))

    assert (isinstance(DE, gpd.GeoDataFrame))
    DF = pd.concat([DE, df], axis=1, join='inner')
    # Derive lat/lon tuple as representative point for each shape
    DF['coords'] = DF.geometry.apply(
        lambda x: x.representative_point().coords[:][0])
    DF['coords_WGS84'] = DF.to_crs({
        'init': 'epsg:4326'
    }).geometry.apply(lambda x: x.representative_point().coords[:][0])

    cols = df.columns
    unit = '\\%' if unit == '%' else unit
    if relative:  # convert to unit per km²
        DF.loc[:, cols] = DF.loc[:, cols].apply(lambda x: x / DF.fl_km2)
        unit = '1' if unit == '-' else unit
        if unit[-2:] == '/a':
            unit = '[${} / (km² × a)$]'.format(unit[:-2])
        else:
            unit = '[${} / km²$]'.format(unit)
    else:
        unit = '[${}$]'.format(unit)

    if isinstance(cmap, str):
        cmap = [cmap for c in cols]
    if len(cols) == 1:
        colorbar_each_subplot = False
    if colorbar_each_subplot:
        if isinstance(interval, tuple):
            intervals = [interval for c in cols]
        elif isinstance(interval, list):
            intervals = interval
        else:
            intervals = []
    else:
        if isinstance(interval, str) or (interval is None):
            intervals = [(DF[cols].min().min(), DF[cols].max().max())
                         for c in cols]
        else:
            intervals = [interval for c in cols]

    if nrows == 0 or ncols == 0:
        if nrows != ncols or ncols < 0 or nrows < 0:
            logger.warn('When passing `nrows` and `ncols` both (!) must be '
                        'passed as int and be greater zero. Gathering these '
                        'values now based on the given pd.DataFrame.')
        nrows, ncols, rem = gather_nrows_ncols(len(cols))
    figsize = kwargs.get('figsize', (4 * ncols * 1.05, 6 * nrows * 1.1))

    i, j = [0, 0]
    fig, ax = plt.subplots(nrows=nrows,
                           ncols=ncols,
                           squeeze=False,
                           figsize=figsize)
    for a, col in enumerate(cols):
        if j == ncols:
            i += 1
            j = 0
        if colorbar_each_subplot:
            intervals.append((DF[col].min(), DF[col].max()))
        # First layer with grey'ish countries/regions:
        DE.plot(ax=ax[i, j], color='grey')
        # Second layer: make subplot
        (DF.dropna(subset=[col], axis=0).plot(ax=ax[i, j],
                                              column=col,
                                              cmap=cmap[a],
                                              vmin=intervals[a][0],
                                              vmax=intervals[a][1]))
        if not shape_source_api:
            ax[i, j].set_xlim(5.5, 15.3)
            ax[i, j].set_ylim(47.0, 55.3)
        # Deal with axes titles
        if isinstance(axtitle, str):
            if len(cols) == 1:
                ax[i, j].set_title('{}'.format(axtitle))
            else:
                if add_percentages:
                    ratio = '{:.1f}'.format(df[col].sum() / df.sum().sum() *
                                            100.)
                    ax[i,
                       j].set_title('{} {} ({}%)'.format(axtitle, col, ratio))
                else:
                    ax[i, j].set_title('{} {}'.format(axtitle, col))
        ax[i, j].get_xaxis().set_visible(False)
        ax[i, j].get_yaxis().set_visible(False)
        # Add annotations
        for idx, row in DF.iterrows():
            s = ''
            for a, ann in enumerate(annotate):
                if a >= 1:
                    s += sep
                if ann == 'nuts3':
                    s += idx
                if ann in ['name', 'names']:
                    s += row.gen
                if ann in ['value', 'values']:
                    s += (''
                          if np.isnan(row[col]) else '{:.0f}'.format(row[col]))
                    if relative:
                        s += '/km²'
                if ann in ['percentage', 'percentages']:
                    if relative:
                        s += ('' if np.isnan(df.loc[idx, col]) else
                              '{:.2%}'.format(df.loc[idx, col] /
                                              float(df[col].sum())))
                    else:
                        s += ('' if np.isnan(row[col]) else '{:.2%}'.format(
                            row[col] / DF[col].sum()))
            txt = ax[i, j].annotate(s=s,
                                    xy=row['coords'],
                                    fontsize=fontsize,
                                    horizontalalignment='center',
                                    color=color,
                                    verticalalignment='center')
            txt.set_path_effects(
                [PathEffects.withStroke(linewidth=1, foreground='silver')])
        j += 1

    # Deactivate possibly remaining axes
    for j in range(rem):
        ax[i, -(j + 1)].axis('off')

    if isinstance(suptitle, str):
        fig.suptitle(suptitle, fontsize=20, weight='heavy')
        fig.subplots_adjust(top=0.82)

    fig.tight_layout()
    if colorbar_each_subplot:
        for a, axes in enumerate(ax.ravel().tolist()):
            sm = ScaMap(cmap=cmap[a],
                        norm=plt.Normalize(vmin=intervals[a][0],
                                           vmax=intervals[a][1]))
            sm._A = []
            cbar = fig.colorbar(sm,
                                ax=axes,
                                shrink=1.0,
                                pad=0.01,
                                fraction=0.046,
                                orientation='horizontal',
                                anchor=(0.5, 1.0),
                                format=mticker.StrMethodFormatter('{x:,g}'))
            cbar.set_label(unit)
    else:
        sm = ScaMap(cmap=cmap[0],
                    norm=plt.Normalize(vmin=intervals[0][0],
                                       vmax=intervals[0][1]))
        sm._A = []
        shr = 1.0 if ncols <= 2 else 0.5
        cbar = fig.colorbar(sm,
                            ax=ax.ravel().tolist(),
                            shrink=shr,
                            pad=0.01,
                            orientation='horizontal',
                            anchor=(0.5, 1.0),
                            format=mticker.StrMethodFormatter('{x:,g}'))
        cbar.set_label(unit)

    add_license_to_figure(fig, geotag=True)
    return fig, ax
Пример #14
0
def plot_efficient_frontier(tr_risk=1e-6):

    # Create a simulation environment
    env = sca.MarketEnvironment()

    # Reset the enviroment with the given trader's risk aversion
    env.reset(lamb=tr_risk)

    # Get the expected shortfall and corresponding variance for the given trader's risk aversion
    tr_E = env.get_AC_expected_shortfall(env.total_shares)
    tr_V = env.get_AC_variance(env.total_shares)

    # Create empty arrays to hold our values of E, V, and U
    E = np.array([])
    V = np.array([])
    U = np.array([])

    # Set the number of plot points for our frontier
    num_points = 7000

    # Set the values of the trader's risk aversion to plot
    lambdas = np.linspace(1e-7, 1e-4, num_points)

    # Calclate E, V, U for each value of llambda
    for llambda in lambdas:
        env.reset(lamb=llambda)
        E = np.append(E, env.get_AC_expected_shortfall(env.total_shares))
        V = np.append(V, env.get_AC_variance(env.total_shares))
        U = np.append(U, env.compute_AC_utility(env.total_shares))

    # Plot E vs V and use U for the colorbar
    cm = plt.cm.get_cmap('gist_rainbow')
    sc = plt.scatter(V, E, s=20, c=U, cmap=cm)
    plt.colorbar(sc,
                 label='AC Utility',
                 format=mticker.StrMethodFormatter('${x:,.0f}'))
    ax = plt.gca()
    ax.set_facecolor('k')
    ymin = E.min() * 0.7
    ymax = E.max() * 1.1
    plt.ylim(ymin, ymax)
    yNumFmt = mticker.StrMethodFormatter('${x:,.0f}')
    xNumFmt = mticker.StrMethodFormatter('{x:,.0f}')
    ax.yaxis.set_major_formatter(yNumFmt)
    ax.xaxis.set_major_formatter(xNumFmt)
    plt.xlabel('Variance of Shortfall')
    plt.ylabel('Expected Shortfall')

    # Get the annotation label and the correction factors
    an_st, xcrf, ycrf, scrf = get_crfs(tr_risk)

    # Plot the annotation in the above plot
    plt.annotate(an_st,
                 xy=(tr_V, tr_E),
                 xytext=(tr_V * xcrf, tr_E * ycrf),
                 color='w',
                 size='large',
                 arrowprops=dict(facecolor='cyan',
                                 shrink=scrf,
                                 width=3,
                                 headwidth=10))
    plt.show()
Пример #15
0
covid.set_index(['Date'], inplace=True)
covid.columns = countries

# Section 5 - Calculating Rates per 100,000
populations = {'Canada':37664517, 'Germany': 83721496 , 'United Kingdom': 67802690 , 'US': 330548815, 'France': 65239883, 'China':1438027228}
percapita = covid.copy()
for country in list(percapita.columns):
    percapita[country] = percapita[country]/populations[country]*100000

# Section 6 - Generating Colours and Style
colors = {'Canada':'#045275', 'China':'#089099', 'France':'#7CCBA2', 'Germany':'#FCDE9C', 'US':'#DC3977', 'United Kingdom':'#7C1D6F'}
plt.style.use('fivethirtyeight')

# Section 7 - Creating the Visualization
plot = covid.plot(figsize=(12,8), color=list(colors.values()), linewidth=5, legend=False)
plot.yaxis.set_major_formatter(ticker.StrMethodFormatter('{x:,.0f}'))
plot.grid(color='#d4d4d4')
plot.set_xlabel('Date')
plot.set_ylabel('# of Cases')

# Section 8 - Assigning Colour
for country in list(colors.keys()):
    plot.text(x = covid.index[-1], y = covid[country].max(), color = colors[country], s = country, weight = 'bold')

# Section 9 - Adding Labels
plot.text(x = covid.index[1], y = int(covid.max().max())+45000, s = "COVID-19 Cases by Country", fontsize = 23, weight = 'bold', alpha = .75)
plot.text(x = covid.index[1], y = int(covid.max().max())+15000, s = "For the USA, China, Germany, France, United Kingdom, and Canada\nIncludes Current Cases, Recoveries, and Deaths", fontsize = 16, alpha = .75)
plot.text(x = percapita.index[1], y = -100000,s = 'datagy.io                      Source: https://github.com/datasets/covid-19/blob/master/data/countries-aggregated.csv', fontsize = 10)

plt.show()
Пример #16
0
def plot_rt(result, ax, state_name):
    """
    Function to plot Rt

    Arguments
    ----------
    result: expected value and HDI of posterior

    ax: matplotlib axes 

    state_name: state to be considered

    See also
    ----------
    This code is heavily based on Realtime R0
    by Kevin Systrom
    https://github.com/k-sys/covid-19/blob/master/Realtime%20R0.ipynb
    """

    ax.set_title(f"{state_name}")

    # Colors
    ABOVE = [1, 0, 0]
    MIDDLE = [1, 1, 1]
    BELOW = [0, 0, 0]
    cmap = ListedColormap(np.r_[np.linspace(BELOW, MIDDLE, 25),
                                np.linspace(MIDDLE, ABOVE, 25)])
    color_mapped = lambda y: np.clip(y, .5, 1.5) - .5

    index = result['ML'].index.get_level_values('date')
    values = result['ML'].values

    # Plot dots and line
    ax.plot(index, values, c='k', zorder=1, alpha=.25)
    ax.scatter(index,
               values,
               s=40,
               lw=.5,
               c=cmap(color_mapped(values)),
               edgecolors='k',
               zorder=2)

    # Aesthetically, extrapolate credible interval by 1 day either side
    lowfn = interp1d(date2num(index),
                     result['Low_90'].values,
                     bounds_error=False,
                     fill_value='extrapolate')

    highfn = interp1d(date2num(index),
                      result['High_90'].values,
                      bounds_error=False,
                      fill_value='extrapolate')

    extended = pd.date_range(start=pd.Timestamp('2020-03-15'),
                             end=index[-1] + pd.Timedelta(days=1))

    ax.fill_between(extended,
                    lowfn(date2num(extended)),
                    highfn(date2num(extended)),
                    color='k',
                    alpha=.1,
                    lw=0,
                    zorder=3)

    ax.axhline(1.0, c='k', lw=1, label='$R_t=1.0$', alpha=.25)

    # Formatting
    ax.xaxis.set_major_locator(mdates.MonthLocator())
    ax.xaxis.set_major_formatter(mdates.DateFormatter('%b'))
    ax.xaxis.set_minor_locator(mdates.DayLocator())

    ax.yaxis.set_major_locator(ticker.MultipleLocator(1))
    ax.yaxis.set_major_formatter(ticker.StrMethodFormatter("{x:.1f}"))
    ax.yaxis.tick_right()
    ax.spines['left'].set_visible(False)
    ax.spines['bottom'].set_visible(False)
    ax.spines['right'].set_visible(False)
    ax.margins(0)
    ax.grid(which='major', axis='y', c='k', alpha=.1, zorder=-2)
    ax.margins(0)
    ax.set_ylim(0.0, 5.0)
    ax.set_xlim(
        pd.Timestamp('2020-03-15'),
        result.index.get_level_values('date')[-1] + pd.Timedelta(days=1))
Пример #17
0
    EJ_0 += [{"beta": beta, "N": N, "val": val, "err": err}]

####
#### plot average sign
####
beta = [f['beta'] for f in sorted(Et, key=itemgetter("beta")) if f["N"] != 0]
E_K_val = np.array([f['val'] for f in sorted(Et, key=itemgetter("beta"))])
E_K_err = np.array([f['err'] for f in sorted(Et, key=itemgetter("beta"))])
plt.errorbar(np.array(beta) * abs(t),
             E_K_val / abs(t),
             yerr=E_K_err / abs(t),
             **errorMarker,
             zorder=2)

plt.xscale("log")
plt.gca().xaxis.set_major_formatter(mticker.StrMethodFormatter("{x:.0f}"))
plt.gca().xaxis.set_minor_formatter(mticker.StrMethodFormatter("{x:.0f}"))
plt.xlabel(r"$ \beta t $")
plt.ylabel(r"$ \langle E_\mathrm{K} \rangle / t $")

####
#### indicate beta = 22, 88
####
for _beta in [22, 88]:
    index = beta.index(_beta)
    plt.plot(_beta * abs(t), E_K_val[index] / abs(t), **indicator, zorder=1)

# exact data inset
ins = inset_axes(plt.gca(), width="60%", height="60%", loc=1)
ins.plot(insetData["tbetas"], insetData["tKs"], **marker, zorder=2)
Пример #18
0
def lr_results(df,X_test,y_test,y_pred,path,fn,stats_list,mdlinst):
    df_results = df.loc[y_test.index, :]
    df_results['predOPS'] = y_pred
    df_results['error'] = 100 * ( ( df_results['OPS'] - df_results['predOPS'] ) / df_results['OPS'])
    df_results['abserror'] = np.abs(100 * ( ( df_results['OPS'] - df_results['predOPS'] ) / df_results['OPS']))
    #
    df_results['predOPS'] = df_results['predOPS']
    df_results['OPS'] = df_results['OPS']
    df_results['error'] = df_results['error']
    df_out = df_results[stats_list]
    save_stats_file(path,fn, df_out)
    #  calculate Rsquared, Adj Rsquared, MSE, RMSE and Fstatistic using my routine
    Rsquared, AdjRsquared, MSE, RMSE, Fstatistic, MeanOfError, StdOfError, AbsErrorSum = calc_regression_stats(X_test,y_test,y_pred)
    # print statistics
    print('R Squared: %1.4f' % Rsquared)
    print('Adjusted R Squared: %1.4f' % AdjRsquared)
    print('F Statistic: %1.4f' % Fstatistic)
    print('MSE: %1.4f' % MSE)
    print('RMSE: %1.4f' % RMSE)
    print('Test Observations:',format(len(y_test)))
    print('Sum of Abs Pct Error: %5.1f' % AbsErrorSum)
    print('Pct Mean Error: %1.4f' % MeanOfError)
    print('Pct Std Dev Error: %1.4f' % StdOfError)
    # print plots
    fig, ax = plt.subplots()
    ax.grid()
    acc = df_results.error
    ptile = np.percentile(acc,[15,85,2.5,97.5])
    sns.regplot(x=df_out['OPS'], y=df_out['predOPS'],
                line_kws={"color":"r","alpha":0.7,"lw":5},
                scatter_kws={"color":"b","s":8}
               )
    plt.title('Actual OPS vs. Predicted OPS')
    plt.xlabel('Actual OPS')
    plt.ylabel('Predicted OPS')
    plt.show()
    fig, ax = plt.subplots()
    ax.grid()
    plt.hist(acc,bins=25)
    plt.title('Error : Actual OPS - Predicted OPS')
    plt.xlabel('Error')
    plt.ylabel('Frequency')
    lab = 'Sampling Mean: %1.2f' % round(np.mean(df_out.error),2)
    lab1 = 'Conf Interval 15 ( %1.3f' % ptile[0] + ' )'
    lab2 = 'Conf Interval 85 ( %1.3f' % ptile[1] + ' )'
    lab3 = 'Conf Interval 2.5 ( %1.3f' % ptile[2] + ' )'
    lab4 = 'Conf Interval 97.5 ( %1.3f' % ptile[3] + ' )'
    plb.axvline(round(np.mean(df_out.error),2),label=lab, color='brown')
    plb.axvline(round(ptile[0],3), label=lab1, color='red')
    plb.axvline(round(ptile[1],3), label=lab2, color='red')
    plb.axvline(round(ptile[2],3), label=lab3, color='green')
    plb.axvline(round(ptile[3],3), label=lab4, color='green')
    leg=plt.legend()
    plt.show()
    fig, ax = plt.subplots()
    ax.grid()
    # plot QQ Plot to see if normal
    probplot(acc,dist="norm",plot=plb)
    _ = plt.title('QQ Plot of OPS Data\n',weight='bold', size=16)
    _ = plt.ylabel('Ordered Values', labelpad=10, size=14)
    _ = plt.xlabel('Theoritical Quantiles', labelpad=10, size = 14)
    ax = plt.gca()
    ax.yaxis.set_major_formatter(mtick.StrMethodFormatter('{x:1.3f}'))
    plt.xticks(np.arange(-4,5,1))
    plb.show()
    fig, ax = plt.subplots()
    ax.grid()
    plt.plot(y_pred, (y_pred-y_test), marker='.',linestyle='none',color='b')
    plt.title('Predicted OPS vs. Residuals')
    plt.xlabel('Predicted OPS')
    plt.ylabel('Residuals')
    plt.show()
    return True
Пример #19
0
area2=200
k=area/area2
case2=[i *k for i in netCashFlowY]



grafica=plt.figure(2)
ax=grafica.add_subplot(421)
ax.bar(vTimeY,netCashFlowY,color=(1.0,0.5,0.62))
ax.set_ylabel('Revenues [USD]')
ax.set_xlabel('Years',fontsize=14)
ax.set_title('Cash flow solar panels 25 years',fontsize=18)
ax.grid(color='b', linestyle='-', linewidth=0.1)
fmt = '${x:,.0f}'
tick = mtick.StrMethodFormatter(fmt)
ax.yaxis.set_major_formatter(tick) 
plt.rc('xtick',labelsize=10)
plt.rc('ytick',labelsize=10)


ax=grafica.add_subplot(422)
ax.plot(vTime,netCashFlow)
ax.get_yaxis().set_major_formatter(plt.FuncFormatter(lambda x, loc: "{:,}".format(int(x))))
ax.set_ylabel('Revenues [USD]')
ax.set_xlabel('Months')
ax.set_title('Break even point project 1',fontsize=18)
ax.grid(color='b', linestyle='-', linewidth=0.1)
fmt = '${x:,.0f}'
tick = mtick.StrMethodFormatter(fmt)
ax.yaxis.set_major_formatter(tick) 
axs0[1].xaxis.set_major_formatter(lambda x, pos: str(x - 5))

fig0.tight_layout()

# The remaining examples use Formatter objects.

fig1, axs1 = plt.subplots(7, 1, figsize=(8, 6))
fig1.suptitle('Formatter Object Formatting')

# Null formatter
setup(axs1[0], title="NullFormatter()")
axs1[0].xaxis.set_major_formatter(ticker.NullFormatter())

# StrMethod formatter
setup(axs1[1], title="StrMethodFormatter('{x:.3f}')")
axs1[1].xaxis.set_major_formatter(ticker.StrMethodFormatter("{x:.3f}"))


# FuncFormatter can be used as a decorator
@ticker.FuncFormatter
def major_formatter(x, pos):
    return f'[{x:.2f}]'


setup(axs1[2], title='FuncFormatter("[{:.2f}]".format')
axs1[2].xaxis.set_major_formatter(major_formatter)

# Fixed formatter
setup(axs1[3], title="FixedFormatter(['A', 'B', 'C', ...])")
# FixedFormatter should only be used together with FixedLocator.
# Otherwise, one cannot be sure where the labels will end up.
Пример #21
0
plt.rcParams.update({'font.size': 8})
matplotlib.rcParams['font.family'] = 'Arial'
plt.close('all')
fig = plt.figure(figsize=(5.61, 8.92 * .35))

dateRef = plotter.time2num(datetime(2005, 1, 1))
tLim = np.array([2392, 2413]) + dateRef

ax = []
for iax in np.arange(0, 3):
    ax1 = fig.add_subplot(1, 3, iax + 1)

    #xaxis
    ax1.xaxis.set_major_locator(ticker.MultipleLocator(2))
    ax1.xaxis.set_major_formatter(ticker.StrMethodFormatter('{x:.0f}'))
    ax1.set_xlabel('Longitude')
    ax1.set_xlim(-129, -123.5)

    #yaxis
    ax1.set_ylim(0, 60)
    ax1.yaxis.set_major_locator(ticker.MultipleLocator(5))
    if iax == 0:
        ax1.set_ylabel('Std. isopycnal depth [m]')
        ax1.yaxis.set_major_formatter(ticker.StrMethodFormatter('{x:.0f}'))
    else:
        ax1.yaxis.set_major_formatter(ticker.NullFormatter())
        None

    ax1.grid(color=(.5, .5, .5), linestyle='--', linewidth=.5)
Пример #22
0
import matplotlib.ticker as ticker
import datetime
import matplotlib.dates as mdates
import numpy as np
import matplotlib as mpl
import matplotlib.style
from cycler import cycler

mpl.rcParams['axes.prop_cycle'] = cycler(color='bgrcmyk')

df = pd.read_csv("/home/john/Projects/PrincipalScraper/403b.csv", usecols=['Date','Total Balance','Gain or Loss'], parse_dates=['Date'])
df.set_index('Date',inplace=True)

fig, ax = plt.subplots(figsize=(15,7))
df.plot(ax=ax)

fmt = '${x:,.0f}'
tick = ticker.StrMethodFormatter(fmt)
ax.yaxis.set_major_formatter(tick)

start, end = ax.get_ylim()
ax.yaxis.set_ticks(np.arange(500, end, 1000))

ax.set_title("403b Total Balance and Capital Gain or Loss")
ax.grid(which='major',axis='both')
ax.xaxis.set_major_locator(mdates.MonthLocator())
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m'))

plt.xticks(rotation=90, horizontalalignment="center")
plt.show()
Пример #23
0
@pytest.mark.parametrize("proj,gcrs,xloc,xfmt,xloc_expected,xfmt_expected", [
    (ccrs.PlateCarree(), ccrs.PlateCarree(), [10, 20], None,
     mticker.FixedLocator, LongitudeFormatter),
    (ccrs.PlateCarree(), ccrs.Mercator(), [10, 20], None, mticker.FixedLocator,
     classic_formatter),
    (ccrs.PlateCarree(), ccrs.PlateCarree(), mticker.MaxNLocator(nbins=9),
     None, mticker.MaxNLocator, LongitudeFormatter),
    (ccrs.PlateCarree(), ccrs.Mercator(), mticker.MaxNLocator(nbins=9), None,
     mticker.MaxNLocator, classic_formatter),
    (ccrs.PlateCarree(), ccrs.PlateCarree(), None, None, LongitudeLocator,
     LongitudeFormatter),
    (ccrs.PlateCarree(), ccrs.Mercator(), None, None,
     classic_locator.__class__, classic_formatter),
    (ccrs.PlateCarree(), ccrs.PlateCarree(), None,
     mticker.StrMethodFormatter('{x}'), LongitudeLocator,
     mticker.StrMethodFormatter),
])
def test_gridliner_default_fmtloc(proj, gcrs, xloc, xfmt, xloc_expected,
                                  xfmt_expected):
    plt.figure()
    ax = plt.subplot(111, projection=proj)
    gl = ax.gridlines(crs=gcrs, draw_labels=False, xlocs=xloc, xformatter=xfmt)
    plt.close()
    assert isinstance(gl.xlocator, xloc_expected)
    assert isinstance(gl.xformatter, xfmt_expected)


def test_gridliner_line_limits():
    fig = plt.figure()
    ax = plt.subplot(1, 1, 1, projection=ccrs.NorthPolarStereo())
Пример #24
0
# params = dict(
#     data=tGR,
#     hue="nodeClass",
#     s=8
# )

boxPairs = [("con", "ind"), ("con", "non"), ("ind", "non")]
ax = sns.violinplot(data=cre,
                    y="nlog",
                    x="nodeClass",
                    palette=["#63b7af", "#abf0e9", "#d4f3ef"],
                    cut=0)
# plt.yscale('log')
plt.xlabel("NodeClasses")
plt.ylabel("# samples CRE found in")
ax.yaxis.set_major_formatter(mticker.StrMethodFormatter("$10^{{{x:.0f}}}$"))
ax.yaxis.set_ticks([
    np.log10(x) for p in range(0, 10)
    for x in np.linspace(10**p, 10**(p + 1), 10)
],
                   minor=True)
add_stat_annotation(ax,
                    data=cre,
                    y="nlog",
                    x="nodeClass",
                    box_pairs=boxPairs,
                    test='Mann-Whitney')

fig.savefig("/groups/lackgrp/ll_members/berkay/DHS/creOverARBS/creARBS.pdf")
Пример #25
0
    def _create(self, **kwargs):
        workspace = 'FGDB_Kosten.gdb'
        table = 'Gesamtkosten_nach_Traeger'
        self.title = (u"{}: Aufteilung der Gesamtkosten "
                      u"auf die Kostenträger".format(
                          self.tbx.par.get_projectname()))
        y_label = u"Kosten der erstmaligen Herstellung, \nBetriebs- und Unterhaltungskosten in den \nersten 20 Jahren sowie Erneuerungskosten \n(anteilig für die ersten 20 Jahre)"

        df_shares = self.tbx.table_to_dataframe(
            table, workspace=workspace
        )

        df_shareholders = self.tbx.table_to_dataframe(
            'Kostentraeger', workspace='FGDB_Kosten_Tool.gdb',
            is_base_table=True
        )
        categories = df_shareholders['Kostentraeger']
        cols = df_shareholders['spalte']

        pos_idx = np.arange(len(categories))

        #bar_width = 0.5

        figure, ax = self.plt.subplots(figsize=(12, 5))
        colors = self.colors  #self.plt.cm.Paired(np.linspace(0, 1, len(df_shares)))

        summed = np.zeros(len(cols))

        for j, (index, net_share) in enumerate(df_shares.iterrows()):
            data = []
            for i, col in enumerate(cols):
                data.append(net_share[col])
            patches = ax.bar(pos_idx, data, bottom=summed, color=colors[j])
            for i, rect in enumerate(patches.get_children()):
                value = data[i]
                bottom = summed[i]
                if value != 0:
                    color = 'black'
                    if j in [1, 2]:
                        color = 'white'
                    ax.text(i, bottom + value/2., u"{0:,} €".format(int(value)),
                            ha='center', va='center', color=color)
                    #,
                            #bbox=dict(facecolor='white',
                                      #edgecolor='white', boxstyle="round"))

            summed += data

        ax.set_xticks(pos_idx)
        ax.set_xticklabels(categories)
        ax.set_title(self.title)
        ax.set_ylabel(y_label, rotation=90, labelpad=15)
        fmt = u'{x:,.0f} €'
        tick = mticker.StrMethodFormatter(fmt)
        #tick = mticker.FuncFormatter(format_func)
        ax.yaxis.set_major_formatter(tick)
        ax.yaxis.grid(True, which='major')

        box = ax.get_position()
        ax.set_position([box.x0 + box.width * 0.2, box.y0 + box.height * 0.25,
                         box.width * 0.8, box.height * 0.75])

        # Put the legend to the right of the current axis
        ax.legend(df_shares['Netz'], loc='center left',
                  bbox_to_anchor=(0, -0.35))
        # didn't find a way to pass custom colors directly
        for color, handle in zip(colors, ax.get_legend().legendHandles):
            handle.set_color(color)
        return ax
def plot_rt(result, ax, state_name):

    ax.set_title(state_name)

    # Colors
    ABOVE = [1, 0, 0]
    MIDDLE = [1, 1, 1]
    BELOW = [0, 0, 0]
    cmap = ListedColormap(np.r_[np.linspace(BELOW, MIDDLE, 25),
                                np.linspace(MIDDLE, ABOVE, 25)])
    color_mapped = lambda y: np.clip(y, .5, 1.5) - .5

    index = result['R'].index.get_level_values('Time Stamp')
    values = result['R'].values

    # Plot dots and line
    ax.plot(index, values, c='k', zorder=1, alpha=.25)
    ax.scatter(index,
               values,
               s=40,
               lw=.5,
               c=cmap(color_mapped(values)),
               edgecolors='k',
               zorder=2)

    # Aesthetically, extrapolate credible interval by 1 day either side
    lowfn = interp1d(date2num(index),
                     result['Lower'].values,
                     bounds_error=False,
                     fill_value='extrapolate')

    highfn = interp1d(date2num(index),
                      result['Upper'].values,
                      bounds_error=False,
                      fill_value='extrapolate')

    extended = pd.date_range(start=pd.Timestamp('2020-03-16'),
                             end=index[-1] + pd.Timedelta(days=1))

    ax.fill_between(extended,
                    lowfn(date2num(extended)),
                    highfn(date2num(extended)),
                    color='k',
                    alpha=.1,
                    lw=0,
                    zorder=3)

    ax.axhline(1.0, c='k', lw=1, label='$R_t=1.0$', alpha=.25)

    # Formatting
    ax.xaxis.set_major_locator(mdates.MonthLocator())
    ax.xaxis.set_major_formatter(mdates.DateFormatter('%b'))
    ax.xaxis.set_minor_locator(mdates.DayLocator())

    ax.yaxis.set_major_locator(ticker.MultipleLocator(1))
    ax.yaxis.set_major_formatter(ticker.StrMethodFormatter("{x:.1f}"))
    ax.yaxis.tick_right()
    ax.spines['left'].set_visible(False)
    ax.spines['bottom'].set_visible(False)
    ax.spines['right'].set_visible(False)
    ax.margins(0)
    ax.grid(which='major', axis='y', c='k', alpha=.1, zorder=-2)
    ax.margins(0)
    ax.set_ylim(0.0, 10.0)
    ax.set_xlim(
        pd.Timestamp('2020-03-16'),
        result.index.get_level_values('Time Stamp')[-1] + pd.Timedelta(days=1))
Пример #27
0
def plot_trade_list(lq_time = 60, nm_trades = 60, tr_risk = 1e-6, show_trl = False):
    
    # Create simulation environment
    env = sca.MarketEnvironment()

    # Reset the environment with the given parameters
    env.reset(liquid_time = lq_time, num_trades = nm_trades, lamb = tr_risk)

    # Get the trading list from the environment
    trade_list = env.get_trade_list()
    
    # Add a zero at the beginning of the trade list to indicate that at time 0 we don't sell any stocks
    new_trl = np.insert(trade_list, 0, 0)

    # We create a dataframe with the trading list and trading trajectory
    df = pd.DataFrame(data = list(range(nm_trades + 1)),  columns = ['Trade Number'], dtype = 'float64')
    df['Stocks Sold'] = new_trl
    df['Stocks Remaining'] = (np.ones(nm_trades + 1) * env.total_shares) - np.cumsum(new_trl)

    # Create a figure with 2 plots in 1 row
    fig, axes = plt.subplots(nrows = 1, ncols = 2)
    
    # Make a scatter plot of the trade list
    df.iloc[1:].plot.scatter(x = 'Trade Number', y = 'Stocks Sold', c = 'Stocks Sold', colormap = 'gist_rainbow',
                                                 alpha = 1, sharex = False, s = 50, colorbar = False, ax = axes[0])
    
    # Plot a line through the points of the scatter plot of the trade list
    axes[0].plot(df['Trade Number'].iloc[1:], df['Stocks Sold'].iloc[1:], linewidth = 2.0, alpha = 0.5)
    axes[0].set_facecolor(color = 'k')
    yNumFmt = mticker.StrMethodFormatter('{x:,.0f}')
    axes[0].yaxis.set_major_formatter(yNumFmt)
    axes[0].set_title('Trading List')

    # Make a scatter plot of the number of stocks remaining after each trade
    df.plot.scatter(x = 'Trade Number', y = 'Stocks Remaining', c = 'Stocks Remaining', colormap = 'gist_rainbow',
                                                 alpha = 1, sharex = False, s = 50, colorbar = False, ax = axes[1])
    
    # Plot a line through the points of the scatter plot of the number of stocks remaining after each trade
    axes[1].plot(df['Trade Number'], df['Stocks Remaining'], linewidth = 2.0, alpha = 0.5)
    axes[1].set_facecolor(color = 'k')
    yNumFmt = mticker.StrMethodFormatter('{x:,.0f}')
    axes[1].yaxis.set_major_formatter(yNumFmt)
    axes[1].set_title('Trading Trajectory')
    
    # Set the spacing between plots
    plt.subplots_adjust(wspace = 0.4)
    plt.show()
    
    print('\nNumber of Shares Sold: {:,.0f}\n'.format(new_trl.sum()))
    
    if show_trl:
        
        # Since we are not selling fractional shares we round up the shares in the trading list
        rd_trl = round_trade_list(new_trl)
#         rd_trl = new_trl

        # We create a dataframe with the modified trading list and trading trajectory
        df2 = pd.DataFrame(data = list(range(nm_trades + 1)),  columns = ['Trade Number'], dtype = 'float64')
        df2['Stocks Sold'] = rd_trl
        df2['Stocks Remaining'] = (np.ones(nm_trades + 1) * env.total_shares) - np.cumsum(rd_trl)

        return df2.style.hide_index().format({'Trade Number': '{:.0f}', 'Stocks Sold': '{:,.0f}', 'Stocks Remaining': '{:,.0f}'})
Пример #28
0
def df_plot_1(df:pd.DataFrame):
      # -----------------------------------------------------------------------------
      # pandas.DataFrame.plot
      # -----------------------------------------------------------------------------

      """
      kind : str

      'line' : line plot (default)
      'bar' : vertical bar plot
      'barh' : horizontal bar plot
      'hist' : histogram
      'box' : boxplot
      'kde' : Kernel Density Estimation plot
      'density' : same as 'kde'
      'area' : area plot
      'pie' : pie plot
      'scatter' : scatter plot
      'hexbin' : hexbin plot
      """

      # Set color and style
      # sns.set()
      print(df)
      #    App  Feature1  Feature2  Feature3  Feature4  Feature5  Feature6  Feature7  Feature8
      # 0  SHA         0         0         1         1         1         0         1         0
      # 1  LHA         1         5         1         1         0         1         1         2
      # 2  DRA         1         7         0         3         4         6         1         0
      # 3  FRA         1         4         2         1         1         0         1         1
      # 4  BRU         0         0         1         0         1         0         0         0
      # 5  PAR         2         1         1         1         1         4         1         2
      # 6  AER         4         2         1         1         0         1         1         0
      # 7  SHE         1         4         3         1         0         0         1         0

      print(df.set_index('App').T)
      # App       SHA  LHA  DRA  FRA  BRU  PAR  AER  SHE
      # Feature1    0    1    0    1    0    0    0    0
      # Feature2    0    0    0    0    0    1    0    0
      # Feature3    1    1    0    1    1    1    1    0
      # Feature4    1    1    0    1    0    1    1    1
      # Feature5    1    0    0    1    1    1    0    0
      # Feature6    0    1    0    0    0    0    1    0
      # Feature7    1    1    1    1    0    1    1    1
      # Feature8    0    0    0    1    0    0    0    0

      # df.set_index('App').T.plot(kind='bar', stacked=True)

      axs = df.hist(by='Feature1', column=['Feature3','Feature6'], figsize=(10,5), grid=True)

      for i, x in enumerate(axs.flat):
            # Set Title
            x.set_title("Sub Title for Each", weight='bold', size=12, pad=20)

            # Set x-axis label
            x.set_xlabel("xlabel", labelpad=5, weight='normal', size=10)

            # Set y-axis label
            x.set_ylabel("Sessions", labelpad=5, weight='normal', size=10)

            x.legend(
                  labels = ('Feature3', 'Feature6'), 
                  loc = 'best' # https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.legend.html
            )

            # Despine
            x.spines['right'].set_visible(False)
            x.spines['top'].set_visible(False)
            x.spines['left'].set_visible(True)

            # Switch off ticks
            x.tick_params(axis="both", which="both", bottom="off", top="off", labelbottom="on", left="off", right="off", labelleft="on", color='#999999')
            x.tick_params(axis='x', rotation=0)

            # Draw horizontal axis lines
            vals = x.get_yticks()
            for tick in vals:
                  x.axhline(y=tick, linestyle='dashed', alpha=0.4, color='#eeeeee', zorder=1)

            # Format y-axis label
            x.yaxis.set_major_formatter(ticker.StrMethodFormatter('{x:,g} ->'))
      
      plt.tight_layout()
      plt.show()
Пример #29
0
def handle_data_draw_chart(t, num):
    # 第一步,加载历史疫情数据
    # 通过pandas.read_json()
    # datas = pd.read_json('../spider/datas/all_countris_datas.json')
    datas = get_history_data()
    # print(datas)

    # 第二步,得到要展示的数据, 先只对累积确诊人数 进行展示 即 confirmedCount
    # 得到的数据格式:每一行包括所有国家当日累积确诊人数
    pivot_table_data = pd.pivot_table(data=datas,
                                      index=['dateId'],
                                      values=data_type_dict.get(t),
                                      columns='country_name',
                                      fill_value=0)
    # print(pivot_table_data)
    # 查看有多少个国家数据 总共有215个国家或地区数据
    # print(pivot_table_data.columns)

    # 第三步,需要给每一个国家分配一个颜色
    country_colors = generate_country_colors(pivot_table_data.columns)
    # print(country_colors)

    # 第四步, 创建画布
    # fig表示一个画布, ax表示为子图
    fig, ax = plt.subplots(figsize=(16, 9))

    plt.box(False)
    # 用来设置x轴刻度数字格式
    xf = mticker.StrMethodFormatter('{x:,.0f}')

    # 内部函数,用来负责具体的图形绘制
    # 以天为单位进行绘制
    def _draw_barh_chart(day):
        # 需要清理上一次绘制的图形,得到一个干净的画板
        ax.clear()

        # 根据传入的day 日期,从透视表中取出这一天的所有国家的累积确诊人数的数据
        # 对数据进行降序排序
        # 取出top n
        day_datas = pivot_table_data.loc[day].sort_values(
            ascending=False).head(num).sort_values()
        # print(day_datas)
        # 通过top n 个国家的名称,取到它们对应的颜值值
        day_country_colors = [country_colors[name] for name in day_datas.index]

        # 在ax中绘制图形
        ax.barh(day_datas.index, day_datas.values, color=day_country_colors)

        # 显示出累积确诊人数数字
        for i, value in enumerate(day_datas.values):
            ax.text(value,
                    i,
                    f'{value:,.0f}',
                    ha='left',
                    va='center',
                    size=14,
                    color=day_country_colors[i])

        # 设置网络线的样式
        ax.grid(which='major', axis='x', linestyle='-.')
        # 把x轴刻度数字放到图的顶部
        ax.xaxis.set_ticks_position('top')
        # 设置x轴刻度数字的显示格式
        ax.xaxis.set_major_formatter(xf)

        # 设置标题
        ax.set_title(f'Top {num} 国家{data_type_dict_str.get(t)}',
                     fontsize=25,
                     color='black')

        # 显示日期
        str2date = datetime.datetime.strptime(
            str(day), '%Y%m%d')  # 将 day 转换为 datetime 格式
        date2str = str2date.strftime('%Y-%m-%d')
        ax.text(0.9,
                0.2,
                date2str,
                transform=ax.transAxes,
                size=30,
                color='red',
                ha='right')

    # _draw_barh_chart(20201201)
    ani = mpl.animation.FuncAnimation(fig,
                                      _draw_barh_chart,
                                      frames=pivot_table_data.index,
                                      repeat=False,
                                      interval=50)

    # 第一个输出格式:mp4
    # 注意事项:
    #需要安装ffmpeg http://ffmpeg.org/download.html
    """
    在windows系统下:
    ffmpegpath = os.path.abspath("./ffmpeg/bin/ffmpeg.exe")
    matplotlib.rcParams["animation.ffmpeg_path"] = ffmpegpath
    writer = animation.FFMpegWriter()
    anim.save(fname,writer = writer)
    """
    # writer = mpl.animation.FFMpegWriter()
    # ani.save('barh.mp4', writer=writer)

    # 第二种输出格式: GIF
    #注意事项:
    # 需要安装imagemagick https://imagemagick.org/script/download.php
    # mac 上可以通过 brew install imagemagick
    # ani.save('barh.gif', writer='imagemagick')

    plt.show()
Пример #30
0
def make_infographic(df, ward):
    print("Working on pie chart from ward {}".format(ward))
    matplotlib.rcParams['pdf.fonttype'] = 42
    matplotlib.rcParams['ps.fonttype'] = 42
    matplotlib.rcParams['text.latex.preamble'] = [r'\usepackage{lmodern}']
    plt.rc('text', usetex=True)

    font = {'weight': 'normal', 'size': 10}

    matplotlib.rc('font', **font)

    fpath = "Raleway-Regular.ttf"
    prop = fm.FontProperties(fname=fpath, weight='regular')
    fpath = "Raleway-Black.ttf"
    prop = fm.FontProperties(fname=fpath, weight='bold')
    """
    #Code to put everything on one chart
    fig = plt.figure(figsize=(10.0, 5.625))
    gs = gridspec.GridSpec(45,80)
    pie_chart = fig.add_subplot(gs[9:39,1:38])
    """

    fig1, pie_chart = plt.subplots()

    ### start pie chart ###

    #dict for how to aggregate data, grouping with a count, aggregating
    pie_df = df
    pie_df = pie_df.groupby(["donor_type_size"])
    pie_agg = {'amount': ['sum'], 'donor_type_size': ['count', 'max']}
    pie_df = pie_df.agg(pie_agg)

    #adding a range index
    pie_df = pie_df.reset_index(drop=True)
    #Totals for center of chart
    total_amount = df['amount'].sum()
    total_donors = df['amount'].count()

    # The slices will be ordered and plotted counter-clockwise.
    labels = [
        "\\bf \Large \sffamily \${:,d}\n{} Donors".format(
            list(pie_df['amount']['sum'])[x],
            list(pie_df['donor_type_size']['count'])[x])
        for x in range(len(pie_df.index))
    ]
    labels[2] = re.sub(r'\d+ Donors', '*not itemized', labels[2])
    sizes = list(pie_df['amount']['sum'])
    colors = [
        '#0190EF', '#122547', '#F3A712', '#D62259', '#6C0A0C', '#00D9B8',
        '#00865B'
    ]

    pie_chart.pie(sizes, labels=labels, colors=colors)

    #draw a circle at the center of pie to make it look like a donut
    centre_circle = plt.Circle((0, 0),
                               0.50,
                               color='white',
                               fc='white',
                               linewidth=1.25)
    pie_chart.add_artist(centre_circle)

    # Set aspect ratio to be equal so that pie is drawn as a circle.
    plt.axis('equal')

    pie_chart.text(0,
                   0,
                   'Total\n\\bf \Huge \sffamily \${:,d}\n{} Donors'.format(
                       total_amount, total_donors),
                   horizontalalignment='center',
                   verticalalignment='center')

    plt.tight_layout()
    plt.savefig(os.path.join('infographics', 'ward_{}_pie.pdf'.format(ward)),
                dpi=1000)
    plt.close()

    ### start bar graph ###
    print("Working on bar graph from ward {}.".format(ward))
    """
    #Code to put everything on one chart
    bar_graph = fig.add_subplot(gs[10:38,41:78])
    """
    fig1, bar_graph = plt.subplots()

    bar_df = df

    bar_df['amount'].replace('', 0)
    #initialize arrays, order is within_ward, in_Chicago_outside_ward, in_IL_outside_Chicago, outside_IL
    small_donations = [pie_df.iat[2, 0], 0, 0, 0, 0]
    small_ind_amt = [0, 0, 0, 0, 0]
    large_ind_amt = [0, 0, 0, 0, 0]
    small_bus_amt = [0, 0, 0, 0, 0]
    large_bus_amt = [0, 0, 0, 0, 0]
    small_org_amt = [0, 0, 0, 0, 0]
    large_org_amt = [0, 0, 0, 0, 0]
    small_ind_cnt = [0, 0, 0, 0, 0]
    large_ind_cnt = [0, 0, 0, 0, 0]
    small_bus_cnt = [0, 0, 0, 0, 0]
    large_bus_cnt = [0, 0, 0, 0, 0]
    small_org_cnt = [0, 0, 0, 0, 0]
    large_org_cnt = [0, 0, 0, 0, 0]

    #loop to populate arrays
    for index, row in bar_df.iterrows():
        if df.loc[index, 'donation_location'] == "within_ward":
            if df.loc[index, 'donor_type'] == 'Individual':
                if df.loc[index, 'donation_size'] == 'between $175 and $500':
                    small_ind_cnt[1] += 1
                    small_ind_amt[1] += float(df.loc[index, 'amount'])
                elif df.loc[index, 'donation_size'] == 'over $500':
                    large_ind_cnt[1] += 1
                    large_ind_amt[1] += float(df.loc[index, 'amount'])
            elif df.loc[index, 'donor_type'] == 'Business':
                if df.loc[index, 'donation_size'] == 'between $175 and $500':
                    small_bus_cnt[1] += 1
                    small_bus_amt[1] += float(df.loc[index, 'amount'])
                elif df.loc[index, 'donation_size'] == 'over $500':
                    large_bus_cnt[1] += 1
                    large_bus_amt[1] += float(df.loc[index, 'amount'])
            elif df.loc[index, 'donor_type'] == 'Political Group':
                if df.loc[index, 'donation_size'] == 'between $175 and $500':
                    small_org_cnt[1] += 1
                    small_org_amt[1] += float(df.loc[index, 'amount'])
                elif df.loc[index, 'donation_size'] == 'over $500':
                    large_org_cnt[1] += 1
                    large_org_amt[1] += float(df.loc[index, 'amount'])
        elif df.loc[index, 'donation_location'] == 'in_Chicago_outside_ward':
            if df.loc[index, 'donor_type'] == 'Individual':
                if df.loc[index, 'donation_size'] == 'between $175 and $500':
                    small_ind_cnt[2] += 1
                    small_ind_amt[2] += float(df.loc[index, 'amount'])
                elif df.loc[index, 'donation_size'] == 'over $500':
                    large_ind_cnt[2] += 1
                    large_ind_amt[2] += float(df.loc[index, 'amount'])
            elif df.loc[index, 'donor_type'] == 'Business':
                if df.loc[index, 'donation_size'] == 'between $175 and $500':
                    small_bus_cnt[2] += 1
                    small_bus_amt[2] += float(df.loc[index, 'amount'])
                elif df.loc[index, 'donation_size'] == 'over $500':
                    large_bus_cnt[2] += 1
                    large_bus_amt[2] += float(df.loc[index, 'amount'])
            elif df.loc[index, 'donor_type'] == 'Political Group':
                if df.loc[index, 'donation_size'] == 'between $175 and $500':
                    small_org_cnt[2] += 1
                    small_org_amt[2] += float(df.loc[index, 'amount'])
                elif df.loc[index, 'donation_size'] == 'over $500':
                    large_org_cnt[2] += 1
                    large_org_amt[2] += float(df.loc[index, 'amount'])
        elif df.loc[index, 'donation_location'] == 'in_IL_outside_Chicago':
            if df.loc[index, 'donor_type'] == 'Individual':
                if df.loc[index, 'donation_size'] == 'between $175 and $500':
                    small_ind_cnt[3] += 1
                    small_ind_amt[3] += float(df.loc[index, 'amount'])
                elif df.loc[index, 'donation_size'] == 'over $500':
                    large_ind_cnt[3] += 1
                    large_ind_amt[3] += float(df.loc[index, 'amount'])
            elif df.loc[index, 'donor_type'] == 'Business':
                if df.loc[index, 'donation_size'] == 'between $175 and $500':
                    small_bus_cnt[3] += 1
                    small_bus_amt[3] += float(df.loc[index, 'amount'])
                elif df.loc[index, 'donation_size'] == 'over $500':
                    large_bus_cnt[3] += 1
                    large_bus_amt[3] += float(df.loc[index, 'amount'])
            elif df.loc[index, 'donor_type'] == 'Political Group':
                if df.loc[index, 'donation_size'] == 'between $175 and $500':
                    small_org_cnt[3] += 1
                    small_org_amt[3] += float(df.loc[index, 'amount'])
                elif df.loc[index, 'donation_size'] == 'over $500':
                    large_org_cnt[3] += 1
                    large_org_amt[3] += float(df.loc[index, 'amount'])
        elif df.loc[index, 'donation_location'] == 'outside_IL':
            if df.loc[index, 'donor_type'] == 'Individual':
                if df.loc[index, 'donation_size'] == 'between $175 and $500':
                    small_ind_cnt[4] += 1
                    small_ind_amt[4] += float(df.loc[index, 'amount'])
                elif df.loc[index, 'donation_size'] == 'over $500':
                    large_ind_cnt[4] += 1
                    large_ind_amt[4] += float(df.loc[index, 'amount'])
            elif df.loc[index, 'donor_type'] == 'Business':
                if df.loc[index, 'donation_size'] == 'between $175 and $500':
                    small_bus_cnt[4] += 1
                    small_bus_amt[4] += float(df.loc[index, 'amount'])
                elif df.loc[index, 'donation_size'] == 'over $500':
                    large_bus_cnt[4] += 1
                    large_bus_amt[4] += float(df.loc[index, 'amount'])
            elif df.loc[index, 'donor_type'] == 'Political Group':
                if df.loc[index, 'donation_size'] == 'between $175 and $500':
                    small_org_cnt[4] += 1
                    small_org_amt[4] += float(df.loc[index, 'amount'])
                elif df.loc[index, 'donation_size'] == 'over $500':
                    large_org_cnt[4] += 1
                    large_org_amt[4] += float(df.loc[index, 'amount'])

    # The position of the bars on the x-axis
    r = [0, 1, 2, 3, 4]

    # Names of group and bar width
    names = ['Under \$175', 'Ward', 'Chicago', 'Illinois', 'United States']
    barWidth = .6

    bar_graph.bar(r, small_donations, color='#F3A712', width=barWidth)
    # Create small_ind_amt bars
    bar_graph.bar(r, small_ind_amt, color='#D62259', width=barWidth)
    # Create large_ind_amt
    bar_graph.bar(r,
                  large_ind_amt,
                  bottom=small_ind_amt,
                  color='#6C0A0C',
                  width=barWidth)
    # Create small_bus_amt
    bar_graph.bar(r,
                  small_bus_amt,
                  bottom=np.add(small_ind_amt, large_ind_amt),
                  color='#0190EF',
                  width=barWidth)
    # Create large_bus_amt
    bar_graph.bar(r,
                  large_bus_amt,
                  bottom=np.array(
                      [small_bus_amt, small_ind_amt,
                       large_ind_amt]).sum(axis=0),
                  color='#122547',
                  width=barWidth)
    # Create small_org_amt
    bar_graph.bar(r,
                  small_org_amt,
                  bottom=np.array([
                      large_bus_amt, small_bus_amt, small_ind_amt,
                      large_ind_amt
                  ]).sum(axis=0),
                  color='#00D9B8',
                  width=barWidth)
    # Create large_org_amt
    bar_graph.bar(r,
                  large_org_amt,
                  bottom=np.array([
                      small_org_amt, large_bus_amt, small_bus_amt,
                      small_ind_amt, large_ind_amt
                  ]).sum(axis=0),
                  color='#00865B',
                  width=barWidth)

    # Y axis
    ax = plt.gca()
    fmt = '\${x:,.0f}'
    tick = ticker.StrMethodFormatter(fmt)
    ax.yaxis.set_major_formatter(tick)

    # X axis
    plt.xticks(r, names)

    # ticks
    plt.tick_params(
        axis='both',  # changes apply to the x-axis
        which='both',  # both major and minor ticks are affected
        bottom=False,  # ticks along the bottom edge are off
        top=False,  # ticks along the top edge are off
        left=False)

    #spines
    ax.spines['top'].set_visible(False)
    ax.spines['left'].set_visible(False)
    ax.spines['right'].set_visible(False)
    ax.spines['bottom'].set_color('#989898')

    #put labels on top of bars
    heights = np.array([
        small_donations, large_org_amt, small_org_amt, large_bus_amt,
        small_bus_amt, small_ind_amt, large_ind_amt
    ]).sum(axis=0)
    donor_numbers = np.array([
        small_ind_cnt, large_ind_cnt, small_bus_cnt, large_bus_cnt,
        small_org_cnt, large_org_cnt
    ]).sum(axis=0)
    labels = [
        "\\bf \Large \sffamily \${:,.0f}\n{} Donors".format(
            heights[i], donor_numbers[i]) for i in range(len(heights))
    ]
    labels[0] = re.sub(r'\d+ Donors', '*not itemized', labels[0])
    xs = [0, 1, 2, 3, 4]
    for height, label, x in zip(heights, labels, xs):
        ax.text(x, height + 10, label, ha='center', va='bottom')

    #title
    #plt.title("Donations By Location", y=1.01)

    # Show graphic
    plt.tight_layout()
    plt.savefig(os.path.join('infographics', 'ward_{}_bar.pdf'.format(ward)),
                dpi=1000)
    plt.close()
    """