-
Notifications
You must be signed in to change notification settings - Fork 6
80 plotting #81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
80 plotting #81
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
330a86c
Add plotting submodule, remove plotting from mesh class
90dbb5e
Add triplot and collections tutorials
3387e9c
Rename to plot, add deprecation warnings, remove typing
df23efc
Add issue reference to CHANGELOG
9169fc3
Remove function for formatting axes from tutorials.
9bd9c9e
Make axes formatting function private to avoid micromanaging users' …
1f11e1e
Reorganize functions so that high level one are at the top
9af6f81
Fix version number in the changelog
10b1203
Fix and unify docstrings
11a9fa5
Merge branch 'development' into 80-plotting
jacklovell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| import matplotlib.pyplot as plt | ||
skuba31 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| import numpy as np | ||
| from matplotlib.collections import PolyCollection | ||
|
|
||
|
|
||
| def plot_quadrangle_mesh(mesh, solps_data=None, ax=None): | ||
| """ | ||
| Plot the quadrangle mesh grid geometry to a matplotlib figure. | ||
|
|
||
| :param mesh: SOLPSMesh object | ||
| :param solps_data: Data array defined on the SOLPS mesh (optional) | ||
| :param ax: matplotlib axes (optional) | ||
| :return: matplotlib axes | ||
| """ | ||
| if ax is None: | ||
| _, ax = plt.subplots(constrained_layout=True) | ||
|
|
||
| if solps_data is None: | ||
| collection_mesh = create_quadrangle_polycollection(mesh, facecolor="none", edgecolor='b', linewidth=0.5) | ||
| else: | ||
| collection_mesh = create_quadrangle_polycollection(mesh, solps_data) | ||
| ax.add_collection(collection_mesh) | ||
|
|
||
| ax = format_matplotlib_axes(ax, mesh) | ||
| return ax | ||
|
|
||
|
|
||
| def create_quadrangle_polycollection(mesh, solps_data: np.ndarray = None, **collection_kw): | ||
skuba31 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| """ | ||
| Creates a matplotlib PolyCollection object from the quadrangle mesh. | ||
|
|
||
| If solps_data is provided, it is used to colour the faces of the quadrangles in the mesh. | ||
|
|
||
| :param mesh: SOLPSMesh object | ||
| :param solps_data: Optional[np.ndarray] - Data array defined on the SOLPS mesh | ||
| :param collection_kw: Keyword arguments for the PolyCollection | ||
|
|
||
| :return: matplotlib.collections.PolyCollection | ||
| """ | ||
| verts = mesh.vertex_coordinates[mesh.quadrangles] | ||
| collection_mesh = PolyCollection(verts, **collection_kw) | ||
| if solps_data is not None: | ||
| collection_mesh.set_array(solps_data[mesh.quadrangle_to_grid_map[:, 0], mesh.quadrangle_to_grid_map[:, 1]]) | ||
| return collection_mesh | ||
|
|
||
|
|
||
| def format_matplotlib_axes(ax: plt.Axes, mesh=None) -> plt.Axes: | ||
skuba31 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| """ | ||
| Format the matplotlib axes for a SOLPS mesh plot. | ||
|
|
||
| :param ax: matplotlib axes | ||
| :param mesh: SOLPSMesh object (optional) | ||
| :return: matplotlib axes | ||
| """ | ||
| ax.set_aspect(1) | ||
| ax.set_xlabel("R [m]") | ||
| ax.set_ylabel("z [m]") | ||
| if mesh is not None: | ||
| ax.set_xlim(mesh.mesh_extent["minr"], mesh.mesh_extent["maxr"]) | ||
| ax.set_ylim(mesh.mesh_extent["minz"], mesh.mesh_extent["maxz"]) | ||
| return ax | ||
|
|
||
|
|
||
| def plot_triangle_mesh(mesh, solps_data: np.ndarray = None, ax: plt.Axes = None) -> plt.Axes: | ||
skuba31 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| """ | ||
| Plot the triangle mesh grid geometry to a matplotlib figure. | ||
|
|
||
| :param mesh: SOLPSMesh object | ||
| :param solps_data: Data array defined on the SOLPS mesh | ||
| :param ax: matplotlib axes (optional) | ||
| :return: matplotlib axes | ||
| """ | ||
| if ax is None: | ||
| _, ax = plt.subplots(constrained_layout=True) | ||
|
|
||
| if solps_data is None: | ||
| collection_mesh = create_triangle_polycollection(mesh, facecolor="none", edgecolor='b', linewidth=0.5) | ||
| else: | ||
| collection_mesh = create_triangle_polycollection(mesh, solps_data) | ||
| ax.add_collection(collection_mesh) | ||
|
|
||
| ax = format_matplotlib_axes(ax, mesh) | ||
| return ax | ||
|
|
||
|
|
||
| def create_triangle_polycollection(mesh, solps_data: np.ndarray = None, **collection_kw) -> PolyCollection: | ||
| """ | ||
| Creates a matplotlib PolyCollection object from the triangle mesh. | ||
|
|
||
| If solps_data is provided, it is used to colour the faces of the triangles in the mesh. | ||
|
|
||
| :param mesh: SOLPSMesh object | ||
| :param solps_data: Data array defined on the SOLPS mesh | ||
| :return: matplotlib.collections.PolyCollection | ||
| """ | ||
| verts = mesh.vertex_coordinates[mesh.triangles] | ||
| collection_mesh = PolyCollection(verts, **collection_kw) | ||
| if solps_data is not None: | ||
| collection_mesh.set_array(solps_data[mesh.triangle_to_grid_map[:, 0], mesh.triangle_to_grid_map[:, 1]]) | ||
| return collection_mesh | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| # Copyright 2014-2020 United Kingdom Atomic Energy Authority | ||
| # | ||
| # Licensed under the EUPL, Version 1.1 or – as soon they will be approved by the | ||
| # European Commission - subsequent versions of the EUPL (the "Licence"); | ||
| # You may not use this work except in compliance with the Licence. | ||
| # You may obtain a copy of the Licence at: | ||
| # | ||
| # https://joinup.ec.europa.eu/software/page/eupl5 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software distributed | ||
| # under the Licence is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR | ||
| # CONDITIONS OF ANY KIND, either express or implied. | ||
| # | ||
| # See the Licence for the specific language governing permissions and limitations | ||
| # under the Licence. | ||
| import os | ||
| import matplotlib.pyplot as plt | ||
|
|
||
| from cherab.solps import load_solps_from_raw_output | ||
| from cherab.solps.plotting import create_quadrangle_polycollection, create_triangle_polycollection, format_matplotlib_axes | ||
|
|
||
|
|
||
| plt.rcParams['figure.figsize'] = [5, 10] # default figure size | ||
|
|
||
| # Load the simulation. | ||
| demos_directory = os.path.dirname(os.path.dirname(__file__)) | ||
| simulation_directory = os.path.join(demos_directory, 'data', 'raw') | ||
| print('Loading simulation...') | ||
| sim = load_solps_from_raw_output(simulation_directory) | ||
| mesh = sim.mesh | ||
|
|
||
| # plot quadrangle and triangle meshes | ||
| # plot the quadrangle b2 mesh | ||
| collection_qm = create_quadrangle_polycollection(mesh, facecolor="none", edgecolor='b', linewidth=0.2) | ||
|
|
||
| fig_qmesh = plt.figure() | ||
| ax_qmesh = plt.subplot(111) | ||
| ax_qmesh.set_title("Quadrangle B2 Mesh") | ||
| ax_qmesh.add_collection(collection_qm) | ||
| format_matplotlib_axes(ax_qmesh, mesh) | ||
|
|
||
| #plot the quadrangle b2 mesh with b2 ion temperature values | ||
| collection_qti = create_quadrangle_polycollection(mesh, solps_data=sim.ion_temperature) | ||
| fig_qti = plt.figure() | ||
| ax_qti = plt.subplot(111) | ||
| ax_qti.set_title("B2 Ion Temperature") | ||
| ax_qti.add_collection(collection_qti) | ||
| cax_qti = ax_qti.inset_axes([1.05, 0, 0.05, 1]) | ||
| fig_qti.colorbar(collection_qti, cax=cax_qti, label="Ion Temperature [eV]") | ||
| format_matplotlib_axes(ax_qti, mesh) | ||
|
|
||
| # plot the triangle B2 mesh | ||
| collection_tm = create_triangle_polycollection(mesh, facecolor="none", edgecolor='g', linewidth=0.25) | ||
|
|
||
| fig_tmesh = plt.figure() | ||
| ax_tmesh = plt.subplot(111) | ||
| ax_tmesh.set_title("Cherab Triangle Mesh") | ||
| ax_tmesh.add_collection(collection_tm) | ||
| format_matplotlib_axes(ax_tmesh, mesh) | ||
|
|
||
| # plot the triangle B2 mesh with b2 ion temperature values | ||
| collection_tti = create_triangle_polycollection(mesh, solps_data=sim.ion_temperature, edgecolors='face') | ||
|
|
||
| fig_tti = plt.figure() | ||
| ax_tti = plt.subplot(111) | ||
| ax_tti.set_title("Cherab Triangle mesh with Ion Temperature") | ||
| ax_tti.add_collection(collection_tti) | ||
| cax_tti = ax_tti.inset_axes([1.05, 0, 0.05, 1]) | ||
| fig_tti.colorbar(collection_tti, cax=cax_tti, label="Ion Temperature [eV]") | ||
| format_matplotlib_axes(ax_tti, mesh) | ||
|
|
||
| plt.show() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| # Copyright 2014-2020 United Kingdom Atomic Energy Authority | ||
| # | ||
| # Licensed under the EUPL, Version 1.1 or – as soon they will be approved by the | ||
| # European Commission - subsequent versions of the EUPL (the "Licence"); | ||
| # You may not use this work except in compliance with the Licence. | ||
| # You may obtain a copy of the Licence at: | ||
| # | ||
| # https://joinup.ec.europa.eu/software/page/eupl5 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software distributed | ||
| # under the Licence is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR | ||
| # CONDITIONS OF ANY KIND, either express or implied. | ||
| # | ||
| # See the Licence for the specific language governing permissions and limitations | ||
| # under the Licence. | ||
| import os | ||
|
|
||
| import matplotlib.pyplot as plt | ||
| from matplotlib.tri import Triangulation | ||
|
|
||
| from cherab.solps import load_solps_from_raw_output, SOLPSSimulation | ||
| from cherab.solps.plotting import format_matplotlib_axes | ||
|
|
||
|
|
||
| # Load the simulation. | ||
| demos_directory = os.path.dirname(os.path.dirname(__file__)) | ||
| simulation_directory = os.path.join(demos_directory, 'data', 'raw') | ||
| print('Loading simulation...') | ||
|
|
||
| sim: SOLPSSimulation = load_solps_from_raw_output(simulation_directory) | ||
skuba31 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| mesh = sim.mesh | ||
|
|
||
| # prepare data for triangulation plots using matplotlib.tri | ||
| tri = Triangulation(mesh.vertex_coordinates[:, 0], mesh.vertex_coordinates[:, 1], mesh.triangles) | ||
| ion_temperature_tri = sim.ion_temperature[mesh.triangle_to_grid_map[:, 0], mesh.triangle_to_grid_map[:, 1]] | ||
|
|
||
|
|
||
| # plot mesh | ||
| fig_mesh = plt.figure() | ||
| ax_mesh = fig_mesh.add_subplot(111) | ||
| ax_mesh.set_title("Mesh") | ||
| ax_mesh.triplot(tri, lw=0.2) | ||
| format_matplotlib_axes(ax_mesh, mesh) | ||
|
|
||
|
|
||
| # plot ion temperature | ||
| fig_ion_temperature = plt.figure() | ||
| ax_ion_temperature = fig_ion_temperature.add_subplot(111) | ||
| tpc = ax_ion_temperature.tripcolor(tri, ion_temperature_tri) | ||
| fig_ion_temperature.colorbar(tpc, ax=ax_ion_temperature, label="Ion Temperature [eV]") | ||
| format_matplotlib_axes(ax_ion_temperature, mesh) | ||
|
|
||
| plt.show() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.