{ "cells": [ { "cell_type": "markdown", "source": [ "# Bar and stack plots with [PowerGraphics.jl](github.com/nrel-siip/PowerGraphics.jl)\n", "PowerGraphics also provides some basic specifications for plotting `SimulationResults`.\n", "This example demonstrates some simple plotting capabilities using different Plots.julia\n", "backends.\n", "\n", "The plotting capabilities use the Julia Plots package which can generate plots using\n", "several different graphics packages. We'll use GR.jl and PlotlyJS.jl.\n", "\n", "## Dependencies" ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "using SIIPExamples #for path locations\n", "pkgpath = dirname(dirname(pathof(SIIPExamples)))\n", "using PowerSystems #to load results\n", "using PowerSimulations #to load results\n", "using PowerGraphics" ], "metadata": {}, "execution_count": null }, { "cell_type": "markdown", "source": [ "### Results file\n", "If you have already run some of the other examples, you should have generated some results\n", "(If you haven't run some of the other simulaitons, you can run\n", "`include(joinpath(pkgpath, \"test\", \"3_PowerSimulations_examples\", \"2_sequential_simulations.jl\"))`).\n", "You can load the results into memory with:" ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "simulation_folder = joinpath(dirname(dirname(pathof(SIIPExamples))), \"rts-test\")\n", "simulation_folder =\n", " joinpath(simulation_folder, \"$(maximum(parse.(Int64,readdir(simulation_folder))))\")\n", "\n", "results = SimulationResults(simulation_folder);\n", "res = get_problem_results(results, \"UC\")" ], "metadata": {}, "execution_count": null }, { "cell_type": "markdown", "source": [ "## Plots\n", "By default, PowerGraphics uses the GR graphics package as the backend for Plots.jl to\n", "generate figures. This creates static plots and should execute without any extra steps.\n", "For example, we can create a plot of a particular variable in the `res` object:" ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "gr() # loads the GR backend\n", "timestamps = get_realized_timestamps(res)\n", "variables = read_realized_variables(res)\n", "\n", "plot_dataframe(variables[:P__HydroEnergyReservoir], timestamps)" ], "metadata": {}, "execution_count": null }, { "cell_type": "markdown", "source": [ "However, interactive plotting can generate much more insightful figures, especially when\n", "creating somewhat complex stacked figures. So, we can use the PlotlyJS backend for Plots,\n", "but it requires that PlotlyJS.jl is installed in your Project.toml (if in a notebook,\n", "WebIO.jl is required too). To startup the PlotlyJS backend, run:" ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "plotlyjs()" ], "metadata": {}, "execution_count": null }, { "cell_type": "markdown", "source": [ "PowerGraphics creates an un-stacked line plot by default, but supports kwargs to\n", "create a variety of different figure styles. For example, a stacked area figure can be\n", "created with the `stack = true` kwarg:" ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "plot_dataframe(variables[:P__HydroEnergyReservoir], timestamps; stack = true)" ], "metadata": {}, "execution_count": null }, { "cell_type": "markdown", "source": [ "Or a bar chart can be created with `bar = true`:" ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "plot_dataframe(variables[:P__HydroEnergyReservoir], timestamps; bar = true)" ], "metadata": {}, "execution_count": null }, { "cell_type": "markdown", "source": [ "Or a stacked bar chart..." ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "plot_dataframe(variables[:P__HydroEnergyReservoir], timestamps; bar = true, stack = true)" ], "metadata": {}, "execution_count": null }, { "cell_type": "markdown", "source": [ "PowerGraphics also supports some basic aggregation to create cleaner plots. For example,\n", "we can create a plot of the different variables:" ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "generation = get_generation_data(res)\n", "plot_pgdata(generation, stack = true)\n", "\n", "reserves = get_service_data(res)\n", "plot_pgdata(reserves)" ], "metadata": {}, "execution_count": null }, { "cell_type": "markdown", "source": [ "Another standard aggregation is available to plot demand values:" ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "plot_demand(res)" ], "metadata": {}, "execution_count": null }, { "cell_type": "markdown", "source": [ "The `plot_demand` function can also be called with the `System` rather than the `StageResults`\n", "to inspect the input data. This method can also display demands aggregated by a specified\n", "`<:Topology`:" ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "plot_demand(res.system, aggregation = Area)" ], "metadata": {}, "execution_count": null }, { "cell_type": "markdown", "source": [ "Another standard aggregation exists based on the fuel categories of the generators in the\n", "`System`" ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "plot_fuel(res, stack = true)" ], "metadata": {}, "execution_count": null }, { "cell_type": "markdown", "source": [ "---\n", "\n", "*This notebook was generated using [Literate.jl](https://github.com/fredrikekre/Literate.jl).*" ], "metadata": {} } ], "nbformat_minor": 3, "metadata": { "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.5.4" }, "kernelspec": { "name": "julia-1.5", "display_name": "Julia 1.5.4", "language": "julia" } }, "nbformat": 4 }