<filename>rebalancer/utils.py
import matplotlib.pyplot as plt


def bar_portfolio_allocation(pf_value_in_home, target):
    target_in_pct = target * 100
    current_allocation_in_pct = (pf_value_in_home / sum(pf_value_in_home)) * 100
    ticker = pf_value_in_home.index
    # prepare figure
    x_values = range(len(target))
    x_ticklabels = []
    for i_ticker in range(len(ticker)):
        x_ticklabels.append(ticker[i_ticker] + '\n' +
                            f"[{round(pf_value_in_home[i_ticker] / 1000, 1)}k, "
                            f"{round(current_allocation_in_pct[i_ticker], 1)}%]")
    # define some edges for a beautiful chart :)
    edges_target_line = 0.2
    edges_target_annotate = 0.25
    edges_x_limit = 0.8
    # create figure
    fig, ax = plt.subplots(figsize=(10, 6))
    # create bar plot
    ax.bar(x_values, current_allocation_in_pct, 0.35, color='lightgrey')
    ax.set_ylim(0, 100)
    ax.set_xlim(min(x_values) - edges_x_limit, max(x_values) + edges_x_limit)
    ax.set_xticks(x_values)
    ax.set_xticklabels(x_ticklabels)
    ax.set_ylabel('Portfolio allocation (in %)')
    for i_target in range(len(target)):
        # create horizontal line
        ax.plot([-edges_target_line + i_target, edges_target_line + i_target],
                [target_in_pct[i_target], target_in_pct[i_target]], "k--")
        # create annotation
        plt.annotate(f"{round(target_in_pct[i_target], 1)}%",
                     (i_target + edges_target_annotate, target_in_pct[i_target] - 1))
    plt.show()
    return fig, ax

<filename>rebalancer/utils.py
import matplotlib.pyplot as plt


def bar_portfolio_allocation(pf_value_in_home, target):
    target_in_pct = target * 100
    current_allocation_in_pct = (pf_value_in_home / sum(pf_value_in_home)) * 100
    ticker = pf_value_in_home.index
    # prepare figure
    x_values = range(len(target))
    x_ticklabels = []
    for i_ticker in range(len(ticker)):
        x_ticklabels.append(ticker[i_ticker] + '\n' +
                            f"[{round(pf_value_in_home[i_ticker] / 1000, 1)}k, "
                            f"{round(current_allocation_in_pct[i_ticker], 1)}%]")
    # define some edges for a beautiful chart :)
    edges_target_line = 0.2
    edges_target_annotate = 0.25
    edges_x_limit = 0.8
    # create figure
    fig, ax = plt.subplots(figsize=(10, 6))
    # create bar plot
    ax.bar(x_values, current_allocation_in_pct, 0.35, color='lightgrey')
    ax.set_ylim(0, 100)
    ax.set_xlim(min(x_values) - edges_x_limit, max(x_values) + edges_x_limit)
    ax.set_xticks(x_values)
    ax.set_xticklabels(x_ticklabels)
    ax.set_ylabel('Portfolio allocation (in %)')
    for i_target in range(len(target)):
        # create horizontal line
        ax.plot([-edges_target_line + i_target, edges_target_line + i_target],
                [target_in_pct[i_target], target_in_pct[i_target]], "k--")
        # create annotation
        plt.annotate(f"{round(target_in_pct[i_target], 1)}%",
                     (i_target + edges_target_annotate, target_in_pct[i_target] - 1))
    plt.show()
    return fig, ax

# prepare figure # define some edges for a beautiful chart :) # create figure # create bar plot # create horizontal line # create annotation