Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 36 additions & 13 deletions postreise/plot/demo/plot_bar_hbar_pie_and_shortfall.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -19,11 +19,21 @@
"\n",
"For now, there is `plot_bar`, `plot_hbar`, `plot_pie`, and `plot_shortfall`\n",
"\n",
"All of them can be run with `plot_foo(interconnect, list(scenario_ids))`\n",
"All of them can be run with `plot_foo(interconnect, time, list(scenario_ids))`\n",
"\n",
"You can also optionally add scenario names and include your own custom data\n",
"\n",
"e.g. `plot_func(interconnect, list(scenario_ids), list(scenario_names)`"
"e.g. `plot_func(interconnect, time, list(scenario_ids), list(scenario_names)`"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# Let's set a time\n",
"time = ('2016-01-01-00', '2016-12-31-23', 'utc', 'H')"
]
},
{
Expand Down Expand Up @@ -300,7 +310,7 @@
],
"source": [
"# Most basic bar plot\n",
"plot_bar('Western', ['87', '91', '89'])"
"plot_bar('Western', time, ['87', '91', '89'])"
]
},
{
Expand Down Expand Up @@ -571,14 +581,15 @@
"source": [
"# You can add scenario names and selecting specific resources to show\n",
"plot_bar('Western', \n",
" time,\n",
" ['87', '91', '89'], \n",
" ['2016 Western Base', '2030 Independent', '2030 All Match CA'],\n",
" resource_types=['wind', 'solar'])"
]
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -625,7 +636,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 8,
"metadata": {
"scrolled": true
},
Expand Down Expand Up @@ -821,14 +832,15 @@
"# Note that the custom data is mostly empty except for Western zone\n",
"\n",
"plot_bar('Western', \n",
" time,\n",
" ['87'], \n",
" ['2016 Simulated Generation'],\n",
" custom_data={'historical': historical, 'nrel_mid': NREL_mid})"
]
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 3,
"metadata": {
"scrolled": true
},
Expand Down Expand Up @@ -1129,14 +1141,15 @@
],
"source": [
"# Horizontal bar chart\n",
"plot_hbar('Western', \n",
"plot_hbar('Western',\n",
" time,\n",
" ['87', '91', '103', '89'], \n",
" ['2016 Western Base', '2030 Independent', '2030 Overbuild', '2030 All Match CA'])"
]
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 11,
"metadata": {
"scrolled": true
},
Expand Down Expand Up @@ -1377,14 +1390,15 @@
"# With plot pie you can optionally set min_percentage to combine small pie pieces into an Other category\n",
"# Other is the dark blue wedge!\n",
"plot_pie('Western', \n",
" time,\n",
" ['87', '91','103'], \n",
" ['2016 Western Base', '2030 Independent', '2030 Overbuild'],\n",
" min_percentage=2.5)\n"
]
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 12,
"metadata": {
"scrolled": true
},
Expand Down Expand Up @@ -1844,14 +1858,15 @@
],
"source": [
"# Plot a basic shortfall chart\n",
"plot_shortfall('Western', \n",
"plot_shortfall('Western',\n",
" time,\n",
" ['87', '91','103'], \n",
" ['2016 Western Base', '2030 Independent', '2030 Overbuild'])"
]
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 13,
"metadata": {
"scrolled": true
},
Expand Down Expand Up @@ -2423,12 +2438,20 @@
"# You can also provide custom generation baselines, targets and demand (TWh)\n",
"# But most of the time you don't need to worry about that\n",
"\n",
"plot_shortfall('Western', \n",
"plot_shortfall('Western',\n",
" time,\n",
" ['87', '91', '93', '89'], \n",
" ['2016 Western Base', '2030 Independent', '2030 Collaborative', '2030 All Match CA'],\n",
" is_match_CA=True,\n",
" has_collaborative_scenarios=['93'])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down
54 changes: 33 additions & 21 deletions postreise/plot/multi/plot_bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@
from postreise.plot.multi.plot_helpers import handle_plot_inputs


def plot_bar(interconnect, scenario_ids=None, scenario_names=None, \
def plot_bar(interconnect, time, scenario_ids=None, scenario_names=None, \
custom_data=None, resource_types=None):
"""Plots any number of scenarios as bar charts with
"""Plots any number of scenarios as bar charts with
two columns per scenario - defaults to generation and capacity

:param interconnect: either 'Western' or 'Texas'
:type interconnect: string
:param time: time related parameters. 1st element is the starting
date. 2nd element is the ending date (left out). 3rd element is the
timezone, only *'utc'*, *'US/Pacific'* and *'local'* are possible. 4th
element is the frequency, which can be *'H'* (hour), *'D'* (day), *'W'*
(week) or *'auto'*.
:type time: tuple
:param scenario_ids: list of scenario ids, defaults to None
:type scenario_ids: list(string), optional
:param scenario_names: list of scenario names of same len as scenario ids,
Expand All @@ -19,33 +25,39 @@ def plot_bar(interconnect, scenario_ids=None, scenario_names=None, \
:param custom_data: hand-generated data, defaults to None
:type custom_data: dict {'scenario_id': {
'label': 'scenario_name',
'gen': {'label': 'Generation', 'unit': 'TWh', 'data': {'zone_name':
'gen': {'label': 'Generation', 'unit': 'TWh', 'data': {'zone_name':
{'resource_type': float value(s), ...}, ...}},
'cap': {'label': 'Capacity', 'unit': 'GW', 'data': {'zone_name':
'cap': {'label': 'Capacity', 'unit': 'GW', 'data': {'zone_name':
{'resource_type': float value(s), ...}, ...}}},
...}, optional
NOTE: If you want to plot scenario data and custom data together,
NOTE: If you want to plot scenario data and custom data together,
custom data MUST be in TWh for generation and GW for capacity.
We may add a feature to check for and convert to equal units
We may add a feature to check for and convert to equal units
but it's not currently a priority
:param resource_types: list of resource types to show, defaults to None
:type resource_types: list(string), optional
"""
zone_list, graph_data = handle_plot_inputs(
interconnect, scenario_ids, scenario_names, custom_data)
interconnect, time, scenario_ids, scenario_names, custom_data)
for zone in zone_list:
ax_data_list = _construct_bar_ax_data(zone, graph_data, resource_types)
_construct_bar_visuals(zone, ax_data_list)
print(f'\nDone\n')


def plot_hbar(interconnect, scenario_ids=None, scenario_names=None, \
def plot_hbar(interconnect, time, scenario_ids=None, scenario_names=None, \
custom_data=None, resource_types=None):
"""Plots any number of scenarios as horizontal bar charts with
"""Plots any number of scenarios as horizontal bar charts with
two columns per scenario - defaults to generation and capacity

:param interconnect: either 'Western' or 'Texas'
:type interconnect: string
:param time: time related parameters. 1st element is the starting
date. 2nd element is the ending date (left out). 3rd element is the
timezone, only *'utc'*, *'US/Pacific'* and *'local'* are possible. 4th
element is the frequency, which can be *'H'* (hour), *'D'* (day), *'W'*
(week) or *'auto'*.
:type time: tuple
:param scenario_ids: list of scenario ids, defaults to None
:type scenario_ids: list(string), optional
:param scenario_names: list of scenario names of same len as scenario ids,
Expand All @@ -54,20 +66,20 @@ def plot_hbar(interconnect, scenario_ids=None, scenario_names=None, \
:param custom_data: hand-generated data, defaults to None
:type custom_data: dict {'scenario_id': {
'label': 'scenario_name',
'gen': {'label': 'Generation', 'unit': 'TWh', 'data': {'zone_name':
'gen': {'label': 'Generation', 'unit': 'TWh', 'data': {'zone_name':
{'resource_type': float value(s), ...}, ...}},
'cap': {'label': 'Capacity', 'unit': 'GW', 'data': {'zone_name':
'cap': {'label': 'Capacity', 'unit': 'GW', 'data': {'zone_name':
{'resource_type': float value(s), ...}, ...}}},
...}, optional
NOTE: If you want to plot scenario data and custom data together,
NOTE: If you want to plot scenario data and custom data together,
custom data MUST be in TWh for generation and GW for capacity.
We may add a feature to check for and convert to equal units
We may add a feature to check for and convert to equal units
but it's not currently a priority
:param resource_types: list of resource types to show, defaults to None
:type resource_types: list(string), optional
"""
zone_list, graph_data = handle_plot_inputs(
interconnect, scenario_ids, scenario_names, custom_data)
interconnect, time, scenario_ids, scenario_names, custom_data)
for zone in zone_list:
ax_data_list = _construct_bar_ax_data(zone, graph_data, resource_types)
_construct_hbar_visuals(zone, ax_data_list)
Expand All @@ -82,12 +94,12 @@ def _construct_bar_ax_data(zone, scenarios, user_set_resource_types):
:param scenarios: the scenario data to format
:type scenarios: dict {'scenario_id': {
'label': 'scenario_name',
'gen': {'label': 'Generation', 'unit': 'TWh', 'data': {'zone_name':
'gen': {'label': 'Generation', 'unit': 'TWh', 'data': {'zone_name':
{'resource_type': float value(s), ...}, ...}},
'cap': {'label': 'Capacity', 'unit': 'GW', 'data': {'zone_name':
'cap': {'label': 'Capacity', 'unit': 'GW', 'data': {'zone_name':
{'resource_type': float value(s), ...}, ...}}},
...}
:param user_set_resource_types: list of resource types to show,
:param user_set_resource_types: list of resource types to show,
defaults to None
:type user_set_resource_types: list(string), optional
:return: a list of labels and values for each axis of the plot
Expand Down Expand Up @@ -129,9 +141,9 @@ def _get_bar_resource_types(zone, scenarios):
:param scenarios: the scenario data to format
:type scenarios: dict {'scenario_id': {
'label': 'scenario_name',
'gen': {'label': 'Generation', 'unit': 'TWh', 'data': {'zone_name':
'gen': {'label': 'Generation', 'unit': 'TWh', 'data': {'zone_name':
{'resource_type': float value(s), ...}, ...}},
'cap': {'label': 'Capacity', 'unit': 'GW', 'data': {'zone_name':
'cap': {'label': 'Capacity', 'unit': 'GW', 'data': {'zone_name':
{'resource_type': float value(s), ...}, ...}}},
...}
:return: a sorted list of resource types to plot
Expand Down Expand Up @@ -179,15 +191,15 @@ def _construct_bar_visuals(zone, ax_data_list):
horizontalalignment='right')
ax.set_yticks([])
ax.set_ylim(top=1.3*ax.get_ylim()[1])

ax.legend(bbox_to_anchor=(-.03, -.4), loc='upper left', fontsize=16)

for p in ax.patches:
b = p.get_bbox()
ax.annotate(_get_bar_display_val(b.y1), ((b.x1 + b.x0)/2, \
b.y1 + 0.02*ax.get_ylim()[1]), fontsize=10, \
rotation='horizontal', horizontalalignment='center')

axes[1].get_legend().remove()


Expand Down
Loading