|
1 | 1 | import matplotlib.pyplot as plt |
2 | | -from matplotlib.collections import EllipseCollection |
3 | | -from matplotlib.collections import PatchCollection |
4 | | -from matplotlib.patches import Polygon |
| 2 | +from matplotlib import collections |
| 3 | +from matplotlib import patches |
| 4 | +from shapely import geometry |
5 | 5 | import matplotlib.colors as mcolors |
6 | 6 | from matplotlib import cm |
7 | 7 |
|
8 | 8 |
|
9 | 9 | def _plot_field_layout(X, Y, Z, L_min): |
10 | 10 | """Plot field layout.""" |
| 11 | + # Collector heights is illustrated with colors from a colormap |
| 12 | + norm = mcolors.Normalize(vmin=min(Z)-0.000001, vmax=max(Z)+0.000001) |
11 | 13 | # 0.000001 is added/subtracted for the limits in order for the colormap |
12 | 14 | # to correctly display the middle color when all tracker Z coords are zero |
13 | | - norm = mcolors.Normalize(vmin=min(Z)-0.000001, vmax=max(Z)+0.000001) |
14 | 15 | cmap = cm.viridis_r |
15 | 16 | colors = cmap(norm(Z)) |
16 | 17 | fig, ax = plt.subplots(figsize=(6, 6), subplot_kw={'aspect': 'equal'}) |
17 | | - # Plot a circle with a diameter equal to L_min |
18 | | - ax.add_collection(EllipseCollection(widths=L_min, heights=L_min, |
19 | | - angles=0, units='xy', |
20 | | - facecolors=colors, |
21 | | - edgecolors=("black",), |
22 | | - linewidths=(1,), |
23 | | - offsets=list(zip(X, Y)), |
24 | | - transOffset=ax.transData)) |
| 18 | + # Plot a circle for each neighboring collector (diameter equals L_min) |
| 19 | + ax.add_collection(collections.EllipseCollection( |
| 20 | + widths=L_min, heights=L_min, angles=0, units='xy', facecolors=colors, |
| 21 | + edgecolors=("black",), linewidths=(1,), offsets=list(zip(X, Y)), |
| 22 | + transOffset=ax.transData)) |
25 | 23 | # Similarly, add a circle for the origin |
26 | | - ax.add_collection(EllipseCollection(widths=L_min, heights=L_min, |
27 | | - angles=0, units='xy', |
28 | | - facecolors='red', |
29 | | - edgecolors=("black",), |
30 | | - linewidths=(1,), offsets=[0, 0], |
31 | | - transOffset=ax.transData)) |
| 24 | + ax.add_collection(collections.EllipseCollection( |
| 25 | + widths=L_min, heights=L_min, angles=0, units='xy', facecolors='red', |
| 26 | + edgecolors=("black",), linewidths=(1,), offsets=[0, 0], |
| 27 | + transOffset=ax.transData)) |
32 | 28 | plt.axis('equal') |
33 | 29 | fig.colorbar(cm.ScalarMappable(norm=norm, cmap=cmap), ax=ax, shrink=0.8, |
34 | 30 | label='Relative tracker height (vertical)') |
| 31 | + # Set limits |
35 | 32 | lower_lim = min(min(X), min(Y)) - L_min |
36 | 33 | upper_lim = max(max(X), max(Y)) + L_min |
37 | 34 | ax.set_ylim(lower_lim, upper_lim) |
38 | 35 | ax.set_xlim(lower_lim, upper_lim) |
39 | 36 |
|
40 | 37 |
|
41 | | -def _plot_shading(collector_geometry, unshaded_geomtry, shade_geometries): |
| 38 | +def _polygons_to_patch_collection(geometries, **kwargs): |
| 39 | + """Convert Shapely Polygon or MultiPolygon to matplotlib PathCollection. |
| 40 | +
|
| 41 | + kwargs are passed to PatchCollection |
| 42 | + """ |
| 43 | + # Convert geometries to a MultiPolygon if it is a Polygon |
| 44 | + if isinstance(geometries, geometry.Polygon): |
| 45 | + geometries = geometry.MultiPolygon([geometries]) |
| 46 | + exteriors = [patches.Polygon(g.exterior) for g in geometries] |
| 47 | + path_collection = collections.PatchCollection(exteriors, **kwargs) |
| 48 | + return path_collection |
| 49 | + |
| 50 | + |
| 51 | +def _plot_shading(active_collector_geometry, unshaded_geometry, |
| 52 | + shading_geometries, L_min): |
42 | 53 | """Plot the shaded and unshaded area for a specific solar position.""" |
43 | | - shade_exterios = [Polygon(g.exterior) for g in shade_geometries] |
44 | | - shade_patches = PatchCollection(shade_exterios, facecolor='blue', |
45 | | - linewidth=0.5, alpha=0.5) |
46 | | - collector_patch = PatchCollection( |
47 | | - [Polygon(collector_geometry.exterior)], |
48 | | - facecolor='red', linewidth=0.5, alpha=0.5) |
49 | | - unshaded_patch = PatchCollection([Polygon(unshaded_geomtry.exterior)], |
50 | | - facecolor='green', linewidth=0.5, |
51 | | - alpha=0.5) |
52 | | - fig, ax = plt.subplots(1, 2, subplot_kw=dict(aspect='equal')) |
53 | | - ax[0].add_collection(collector_patch, autolim=True) |
54 | | - ax[0].add_collection(shade_patches, autolim=True) |
55 | | - ax[1].add_collection(unshaded_patch, autolim=True) |
56 | | - ax[0].set_xlim(-6, 6), ax[1].set_xlim(-6, 6) |
57 | | - ax[0].set_ylim(-2, 2), ax[1].set_ylim(-2, 2) |
| 54 | + active_patches = _polygons_to_patch_collection( |
| 55 | + active_collector_geometry, facecolor='red', linewidth=0.5, alpha=0.5) |
| 56 | + unshaded_patches = _polygons_to_patch_collection( |
| 57 | + unshaded_geometry, facecolor='green', linewidth=0.5, alpha=0.5) |
| 58 | + shading_patches = _polygons_to_patch_collection( |
| 59 | + shading_geometries, facecolor='blue', linewidth=0.5, alpha=0.5) |
| 60 | + |
| 61 | + fig, axes = plt.subplots(1, 2, subplot_kw=dict(aspect='equal')) |
| 62 | + axes[0].set_title('Unshaded and shading areas') |
| 63 | + axes[0].add_collection(active_patches, autolim=True) |
| 64 | + axes[0].add_collection(shading_patches, autolim=True) |
| 65 | + axes[1].set_title('Unshaded area') |
| 66 | + axes[1].add_collection(unshaded_patches, autolim=True) |
| 67 | + for ax in axes: |
| 68 | + ax.set_xlim(-L_min, L_min) |
| 69 | + ax.set_ylim(-L_min, L_min) |
58 | 70 | plt.show() |
0 commit comments