forked from PyPSA/PyPSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_plotting.py
More file actions
189 lines (128 loc) · 4.51 KB
/
test_plotting.py
File metadata and controls
189 lines (128 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Feb 1 13:13:59 2022.
@author: fabian
"""
import matplotlib.pyplot as plt
import networkx as nx
import numpy as np
import pandas as pd
import pytest
from pypsa.plot import add_legend_circles, add_legend_lines, add_legend_patches
try:
import cartopy
import cartopy.crs as ccrs
cartopy_present = True
except ImportError as e:
cartopy_present = False
@pytest.mark.parametrize("margin", (None, 0.1))
@pytest.mark.parametrize("jitter", (None, 1))
def test_plot_standard_params_wo_geomap(ac_dc_network, margin, jitter):
n = ac_dc_network
n.plot(geomap=False, margin=margin, jitter=jitter)
plt.close()
@pytest.mark.skipif(not cartopy_present, reason="Cartopy not installed")
@pytest.mark.parametrize("margin", (None, 0.1))
@pytest.mark.parametrize("jitter", (None, 1))
def test_plot_standard_params_w_geomap(ac_dc_network, margin, jitter):
n = ac_dc_network
n.plot(geomap=True, margin=margin, jitter=jitter)
plt.close()
def test_plot_on_axis_wo_geomap(ac_dc_network):
n = ac_dc_network
fig, ax = plt.subplots()
n.plot(ax=ax, geomap=False)
plt.close()
@pytest.mark.skipif(not cartopy_present, reason="Cartopy not installed")
def test_plot_on_axis_w_geomap(ac_dc_network):
n = ac_dc_network
fig, ax = plt.subplots()
with pytest.raises(AssertionError):
n.plot(ax=ax, geomap=True)
plt.close()
def test_plot_bus_circles(ac_dc_network):
n = ac_dc_network
bus_sizes = n.generators.groupby(["bus", "carrier"]).p_nom.mean()
bus_sizes[:] = 1
bus_colors = pd.Series(["blue", "red", "green"], index=n.carriers.index)
n.plot(bus_sizes=bus_sizes, bus_colors=bus_colors, geomap=False)
plt.close()
# Retrieving the colors from carriers also should work
n.carriers["color"] = bus_colors
n.plot(bus_sizes=bus_sizes)
plt.close()
def test_plot_with_bus_cmap(ac_dc_network):
n = ac_dc_network
buses = n.buses.index
colors = pd.Series(np.random.rand(len(buses)), buses)
n.plot(bus_colors=colors, bus_cmap="coolwarm", geomap=False)
plt.close()
def test_plot_with_line_cmap(ac_dc_network):
n = ac_dc_network
lines = n.lines.index
colors = pd.Series(np.random.rand(len(lines)), lines)
n.plot(line_colors=colors, line_cmap="coolwarm", geomap=False)
plt.close()
def test_plot_layouter(ac_dc_network):
n = ac_dc_network
n.plot(layouter=nx.layout.planar_layout, geomap=False)
plt.close()
def test_plot_map_flow(ac_dc_network):
n = ac_dc_network
branches = n.branches()
flow = pd.Series(range(len(branches)), index=branches.index)
n.plot(flow=flow, geomap=False)
plt.close()
n.lines_t.p0.loc[:, flow.Line.index] = 0
n.lines_t.p0 += flow.Line
n.plot(flow="mean", geomap=False)
plt.close()
n.plot(flow=n.snapshots[0], geomap=False)
plt.close()
def test_plot_map_line_colorbar(ac_dc_network):
n = ac_dc_network
norm = plt.Normalize(vmin=0, vmax=10)
n.plot(line_colors=n.lines.index.astype(int), line_cmap="viridis", line_norm=norm)
plt.colorbar(plt.cm.ScalarMappable(cmap="viridis", norm=norm), ax=plt.gca())
def test_plot_map_bus_colorbar(ac_dc_network):
n = ac_dc_network
norm = plt.Normalize(vmin=0, vmax=10)
n.plot(bus_colors=n.buses.x, bus_cmap="viridis", bus_norm=norm)
plt.colorbar(plt.cm.ScalarMappable(cmap="viridis", norm=norm), ax=plt.gca())
def test_plot_legend_lines(ac_dc_network):
n = ac_dc_network
fig, ax = plt.subplots()
n.plot(ax=ax, geomap=False)
add_legend_lines(
ax,
[2, 5],
["label a", "label b"],
patch_kw=dict(alpha=0.5),
legend_kw=dict(frameon=False),
)
plt.close()
def test_plot_legend_patches(ac_dc_network):
n = ac_dc_network
fig, ax = plt.subplots()
n.plot(ax=ax, geomap=False)
add_legend_patches(
ax,
["r", "g", "b"],
["red", "green", "blue"],
legend_kw=dict(frameon=False),
)
plt.close()
def test_plot_legend_circles_no_geomap(ac_dc_network):
n = ac_dc_network
fig, ax = plt.subplots()
n.plot(ax=ax, geomap=False)
add_legend_circles(ax, 1, "reference size")
plt.close()
@pytest.mark.skipif(not cartopy_present, reason="Cartopy not installed")
def test_plot_legend_circles_geomap(ac_dc_network):
n = ac_dc_network
fig, ax = plt.subplots(subplot_kw={"projection": ccrs.PlateCarree()})
n.plot(ax=ax, geomap=True)
add_legend_circles(ax, [1, 0.5], ["reference A", "reference B"])
plt.close()