<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def plot_stacked_gen(network, bus=None, resolution='GW', filename=None):
 """ Plot stacked sum of generation grouped by carrier type Parameters network : PyPSA network container bus: string Plot all generators at one specific bus. If none, sum is calulated for all buses resolution: string Unit for y-axis. Can be either GW/MW/KW Returns ------- Plot """

    if resolution == 'GW':
        reso_int = 1e3
    elif resolution == 'MW':
        reso_int = 1
    elif resolution == 'KW':
        reso_int = 0.001

    # sum for all buses
    if bus is None:
        p_by_carrier = pd.concat([network.generators_t.p[network.generators
                         [network.generators.control != 'Slack'].index],
                         network.generators_t.p.mul(
                         network.snapshot_weightings, axis=0)
                         [network.generators[network.generators.control == 
                         'Slack'].index]
                         .iloc[:, 0].apply(lambda x: x if x > 0 else 0)],
                         axis=1)\
                         .groupby(network.generators.carrier, axis=1).sum()
        load = network.loads_t.p.sum(axis=1)
        if hasattr(network, 'foreign_trade'):
            trade_sum = network.foreign_trade.sum(axis=1)
            p_by_carrier['imports'] = trade_sum[trade_sum > 0]
            p_by_carrier['imports'] = p_by_carrier['imports'].fillna(0)
    # sum for a single bus
    elif bus is not None:
        filtered_gens = network.generators[network.generators['bus'] == bus]
        p_by_carrier = network.generators_t.p.mul(network.snapshot_weightings,
            axis=0).groupby(filtered_gens.carrier, axis=1).abs().sum()
        filtered_load = network.loads[network.loads['bus'] == bus]
        load = network.loads_t.p.mul(network.snapshot_weightings, axis=0)\
            [filtered_load.index]

    colors = coloring()
#    TODO: column reordering based on available columns

    fig, ax = plt.subplots(1, 1)

    fig.set_size_inches(12, 6)
    colors = [colors[col] for col in p_by_carrier.columns]
    if len(colors) == 1:
        colors = colors[0]
    (p_by_carrier / reso_int).plot(kind="area", ax=ax, linewidth=0,
                                   color=colors)
    (load / reso_int).plot(ax=ax, legend='load', lw=2, color='darkgrey',
                           style='--')
    ax.legend(ncol=4, loc="upper left")

    ax.set_ylabel(resolution)
    ax.set_xlabel("")
    

    matplotlib.rcParams.update({'font.size': 22})



    if filename is None:
        plt.show()
    else:
        plt.savefig(filename)
        plt.close()