Skip to content
Open
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
91 changes: 86 additions & 5 deletions getting_started/08_PlottingCapabilities.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,17 @@
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import matplotlib\n",
"import matplotlib as mpl\n",
"import shutil\n",
"%matplotlib inline\n",
"import matplotlib.pyplot as plt # pip install matplotlib\n",
"import seaborn as sns # pip install seaborn\n",
"import plotly.graph_objects as go # pip install plotly\n",
"import imageio # pip install imageio\n",
"\n",
"max_iter = 5 # to save time we only assess performance on 30 iterations"
"max_iter = 5 # To save time we only assess performance on 30 iterations"
]
},
{
Expand All @@ -87,7 +89,7 @@
"source": [
"import grid2op\n",
"env_name = \"l2rpn_case14_sandbox\"\n",
"env = grid2op.make(env_name, test=True)"
"env = grid2op.make(env_name, test=True, n_busbar=3)"
]
},
{
Expand Down Expand Up @@ -174,7 +176,7 @@
"metadata": {},
"outputs": [],
"source": [
"fig_obs2 = plot_helper.plot_obs(obs, line_info=\"p\", load_info=\"v\")"
"fig_obs2 = plot_helper.plot_obs(obs, line_info=\"p\", load_info=\"v\", gen_info=None)"
]
},
{
Expand Down Expand Up @@ -231,6 +233,85 @@
"This plotting utility is a very useful tool to detect what happened, especially just before a game over."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Matplotlib - Full Customization\n",
"In PlotMatplot it is also possible to customize:\n",
"* The size of image (width and height)\n",
"* The resolution of the image (dpi)\n",
"* The size of the markers representing loads, generators, energy storage and busbars\n",
"* The color map used to color markers and/or powerlines\n",
"* The normalization of the color map\n",
"* The attribute used to color markers and/or powerlines (e.g. \"rho\" for ratio of thermal limit)\n",
"* The attribute used to display information next to the markers / powerlines (e.g. \"v\" for voltage and \"p\" for active power)\n",
"\n",
"You can either do so when first instantiating the helper PlotMatplot(), which will ensure the same process is applied each time you plot an observation. Otherwise, you can pass it as a kwarg to .plot_obs(...) for a specific observation to customize that one."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Example: Double the size of markers\n",
"plot_helper = PlotMatplot(env.observation_space, sub_radius=16, gen_radius=18, load_radius=18, bus_radius=12)\n",
"plot_helper.plot_obs(obs);"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Example: Use built-in matplotlib colormap (as str)\n",
"print(f\"Available Colormaps: {[k for k in mpl.colormaps.keys() if not k.endswith('_r')]}\")\n",
"plot_helper = PlotMatplot(env.observation_space, load_color=\"inferno\", load_color_attr=\"load_p\", gen_id=True, gen_color=\"berlin\", gen_color_attr=\"v\", bus_color=\"inferno_r\")\n",
"plot_helper.plot_obs(obs, gen_info=\"v\");"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Example: Changing the normalization on the colormaps (BoundaryNorm)\n",
"# Note: Using a list of numbers with a continuous cmap name will NOT work, since BoundaryNorm expects a discrete cmap\n",
"plot_helper = PlotMatplot(env.observation_space, line_color_norm=[0.0, 0.25, 0.5, 0.75, 1.0], \n",
" line_color=[\"darkgreen\", \"green\", \"yellow\", \"orange\", \"red\"], \n",
" line_color_attr=\"rho\")\n",
"plot_helper.plot_obs(obs);\n",
"\n",
"# Example: Changing the normalization on the colormaps (Custom Norm: PowerNorm)\n",
"plot_helper = PlotMatplot(env.observation_space, line_color_norm=mpl.colors.PowerNorm(vmin=0.0, vmax=np.max(obs.p_or), gamma=0.5),\n",
" line_color=\"cividis\", \n",
" line_color_attr=\"p\")\n",
"plot_helper.plot_obs(obs);"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Example: Changing the normalization on the colormaps (Normalize (linear))\n",
"plot_helper = PlotMatplot(env.observation_space, load_color_norm=[0.0, np.max(obs.load_p)], \n",
" load_color=\"twilight\", \n",
" load_color_attr=\"p\")\n",
"plot_helper.plot_obs(obs);\n",
"\n",
"# Example: Changing the normalization on the colormaps (BoundaryNorm)\n",
"plot_helper = PlotMatplot(env.observation_space, gen_color_norm=mpl.colors.PowerNorm(vmin=np.min(obs.gen_pmin), vmax=np.max(obs.gen_pmax), gamma=0.5), \n",
" gen_color=\"twilight\", \n",
" gen_color_attr=\"p\")\n",
"plot_helper.plot_obs(obs);"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -510,7 +591,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "venv_grid2op",
"language": "python",
"name": "python3"
},
Expand All @@ -524,7 +605,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
"version": "3.11.9"
}
},
"nbformat": 4,
Expand Down
Loading