-
Notifications
You must be signed in to change notification settings - Fork 25
Add plotting tutorials to the main tutorials branch #1903
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
PProfizi
merged 67 commits into
doc/new-tutorials-section
from
tutorials/plot/firsts-plotting-tutorials
May 20, 2025
Merged
Changes from 39 commits
Commits
Show all changes
67 commits
Select commit
Hold shift + click to select a range
73c9412
add new basic tutorial and jupyter_ sphinx extension
luisaFelixSalles 63cad96
add "plotting meshes" example
luisaFelixSalles a123130
add files for the others examples
luisaFelixSalles 7329a32
update index page for plotting tuts
luisaFelixSalles 9dc8eaf
add "plotting data on the meshe" example
luisaFelixSalles e80136f
update "plotting data on the meshe" example
luisaFelixSalles 1bc25f2
updates
luisaFelixSalles 03c96f4
updates on the index page of the plotting section
luisaFelixSalles 8ea0102
updates
luisaFelixSalles d6254f1
add plotting_data_on_specific_placements.rst tut
luisaFelixSalles 120b858
updates
luisaFelixSalles 4cf090a
add plotting_a_graph.rst tut
luisaFelixSalles 23665c5
updates
luisaFelixSalles f89405f
add plot meshes container in plotting_meshes.rst tutorial
luisaFelixSalles 2a99022
add plot meshes container in plotting_meshes.rst tutorial update
luisaFelixSalles 907c32d
add plotting_data_on_deformed_mesh.rst tutorial
luisaFelixSalles 6a36fbd
updates
luisaFelixSalles a2b474b
erase the adapted examples
luisaFelixSalles e98dc3d
add separate plotting_data_on_geometry_elements.rst and plotting_data…
luisaFelixSalles 0d0a6ab
update substitution text
luisaFelixSalles 5c6b9c5
use only the jupyter sphinx extension
luisaFelixSalles 2295ee2
updates the examples package references
luisaFelixSalles bec041e
update the plotting_meshes.rst to the tutorials guidelines
luisaFelixSalles 0aff9a8
update the text of the plotting_meshes.rst to the tutorial
luisaFelixSalles 56fa451
update the plotting_data_on_the_mesh.rst to the tutorials guidelines
luisaFelixSalles 80888bb
update the text of the plotting_meshes.rst tutorial
luisaFelixSalles 75c94d1
updates the text of the plotting_data_on_the_mesh.rst tutorial
luisaFelixSalles 46d0970
updates the text of the plotting_data_on_the_mesh.rst tutorial
luisaFelixSalles 5dd91d9
add the plotting_deformed_meshes.rst tutorial
luisaFelixSalles 6a1b4ca
update the plotting_data_on_the_mesh.rst to the tutorials guidelines
luisaFelixSalles 0329518
update the plotting_meshes.rst to the tutorials guidelines
luisaFelixSalles 2dee2e4
update the plotting_data_on_specific_path.rst to the tutorials guidel…
luisaFelixSalles becbaa5
update the plotting_data_on_deformed_mesh.rst to the tutorials guidel…
luisaFelixSalles 7189c8f
update the plotting_data_on_geometry_elements.rst to the tutorials gu…
luisaFelixSalles b0ba1d6
update the plotting_a_graph.rst to the tutorials guidelines
luisaFelixSalles 9dcec80
add new links and substitution text to the links_and_refs.rst file
luisaFelixSalles ac5a0e8
update the card of the plotting a graph tutorial in the index.rst page
luisaFelixSalles 185bd3f
delete file basic_tutorial.rst
luisaFelixSalles e690c15
Revert "erase the adapted examples"
luisaFelixSalles 1e92e95
Apply suggestions from code review
luisaFelixSalles 937745f
updates the files names, references and titles
luisaFelixSalles 7b27bea
updates the download buttons
luisaFelixSalles a303f3c
updates the text of the plot_mesh.rst tutorial
luisaFelixSalles 41eea94
updates the text of the plot_deformed_mesh.rst tutorial
luisaFelixSalles c498986
updates the text of the plot_data_on_a_mesh.rst tutorial
luisaFelixSalles f7613bf
updates the text of the plot_data_on_custom_geometry.rst tutorial
luisaFelixSalles 27050fd
updates the text of the plot_data_on_custom_path.rst tutorial
luisaFelixSalles 9ae5c0a
Merge branch 'doc/new-tutorials-section' into tutorials/plot/firsts-p…
PProfizi 42a58eb
Merge branch 'doc/new-tutorials-section' into tutorials/plot/firsts-p…
PProfizi a13d0f6
Changes to tutorials/plot/index.rst
PProfizi de058e5
Changes to tutorials/plot/index.rst
PProfizi 31bb36d
Reword transform data tutorials
PProfizi 2109861
Merge remote-tracking branch 'origin/tutorials/plot/firsts-plotting-t…
PProfizi 62fd48a
Rework plot_a_graph.rst
PProfizi 847fe1c
Fix create_a_mesh_from_scratch.rst
PProfizi ed27c40
Finish plot_a_graph.rst
PProfizi 0e0d672
Update plot_mesh.rst
PProfizi 6a3f17b
Update plot_mesh.rst switch example file
PProfizi 4ea5383
Update mesh plot tutorials
PProfizi e7e0902
Fix reference to plot_data_on_a_mesh.rst
PProfizi d720d11
Refactor plot tutorials list -> one tutorial for contours, one for de…
PProfizi 581431f
Refactor plot tutorials list -> one tutorial for contours, one for de…
PProfizi 6200aae
Rework plot_mesh.rst, plot_deformed_mesh.rst, and plot_contour.rst
PProfizi 3f63bae
Merge remote-tracking branch 'origin/tutorials/plot/firsts-plotting-t…
PProfizi 0d8aae0
Fixes to links in plot_a_graph.rst
PProfizi c5bf7d5
Remove plot_data_on_custom_path.rst from plot tutorials (it is a mapp…
PProfizi e1db276
Remove plot_data_on_custom_geometry.rst from plot tutorials (it is a …
PProfizi 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
252 changes: 252 additions & 0 deletions
252
doc/source/user_guide/tutorials/plot/plotting_a_graph.rst
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,252 @@ | ||
.. _ref_plotting_a_graph: | ||
|
||
======================== | ||
Plotting data on a graph | ||
======================== | ||
|
||
.. |Line| replace:: :class:`Line <ansys.dpf.core.geometry.Line>` | ||
.. |mapping| replace:: :class:`mapping <ansys.dpf.core.operators.mapping.on_coordinates.on_coordinates>` | ||
.. |Line.path| replace:: :func:`Line.path<ansys.dpf.core.geometry.Line.path>` | ||
.. |min_max_fc| replace:: :class:`min_max_fc <ansys.dpf.core.operators.min_max.min_max_fc.min_max_fc>` | ||
|
||
This tutorial explains how to plot a graph with data in DPF. | ||
|
||
The current |DpfPlotter| module don't have method to plotting graphs. Thus, you need to import the | ||
`matplotlib <matplotlib_github_>`_ library to plot a graph with PyDPF-Core. | ||
|
||
There is a large range of graphs you can plot. Here, we plot: | ||
|
||
- :ref:`Results data vs. space position graph <ref_graph_result_space>` | ||
- :ref:`Results data vs. time graph <ref_graph_result_time>` | ||
|
||
.. _ref_graph_result_space: | ||
|
||
Results data vs. space position | ||
------------------------------- | ||
|
||
In this tutorial, we plot the norm of the displacement results on a |Line|. For more information about how | ||
this object can be defined, see the :ref:`ref_plotting_data_on_specific_placements` tutorial. | ||
|
||
Define the results data | ||
^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
First, import a results file. For this tutorial, you can use the one available in the |Examples| module. | ||
For more information about how to import your own result file in DPF, see | ||
the :ref:`ref_tutorials_import_data` tutorials section. | ||
|
||
.. jupyter-execute:: | ||
|
||
# Import the ``ansys.dpf.core`` module | ||
from ansys.dpf import core as dpf | ||
# Import the examples module | ||
from ansys.dpf.core import examples | ||
# Import the operators module | ||
from ansys.dpf.core import operators as ops | ||
# Import the geometry module | ||
from ansys.dpf.core import geometry as geo | ||
|
||
# Import the ``matplotlib.pyplot`` module | ||
import matplotlib.pyplot as plt | ||
|
||
# Define the result file path | ||
result_file_path_1 = examples.find_static_rst() | ||
|
||
The results will be mapped over a defined set of coordinates. Thus, we need the spatial support to | ||
those coordinates: the mesh. The mesh object in DPF is a |MeshedRegion|. | ||
|
||
You can obtain a |MeshedRegion| by creating your own from scratch or by getting it from a result file. | ||
For more information, see the :ref:`ref_tutorials_create_a_mesh_from_scratch` and | ||
:ref:`ref_tutorials_get_mesh_from_result_file` tutorials. | ||
|
||
Here, we extract it from the result file. | ||
|
||
.. jupyter-execute:: | ||
|
||
# Create the model | ||
model_1 = dpf.Model(data_sources=result_file_path_1) | ||
|
||
# Extract the mesh | ||
meshed_region_1 = model_1.metadata.meshed_region | ||
|
||
Extract the results to be plotted on the graph. In this tutorial, we plot the norm of the | ||
displacement results over time. | ||
|
||
.. jupyter-execute:: | ||
|
||
# Get the displacement results | ||
disp_results_1 = model_1.results.displacement.eval() | ||
|
||
Define the line | ||
^^^^^^^^^^^^^^^ | ||
|
||
Create a |Line| passing through the mesh diagonal. | ||
|
||
.. jupyter-execute:: | ||
|
||
# Create the Line object | ||
line_1 = geo.Line(coordinates=[[0.0, 0.06, 0.0], [0.03, 0.03, 0.03]], | ||
n_points=50 | ||
) | ||
|
||
Map the results to the line | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
Map the displacement results to the |Line| using the |mapping| operator. This operator | ||
retrieves the results of the entities located in the given coordinates. If the given coordinates don't | ||
match with any entity coordinate, the operator interpolates the results inside elements with shape functions. | ||
|
||
The displacement results are defined in a *`nodal`* location. Thus, each node has a coordinate in the | ||
mesh and a corresponding displacement data. | ||
|
||
The |mapping| operator takes the coordinates stored in a |Field|. Thus, we must create a |Field| with the | ||
|Line| coordinates. | ||
|
||
.. jupyter-execute:: | ||
|
||
# Get the coordinates field | ||
line_coords_field = line_1.mesh.nodes.coordinates_field | ||
|
||
# Map the line coordinates with the displacement results | ||
mapped_disp_line = ops.mapping.on_coordinates(fields_container=disp_results_1, | ||
coordinates=line_coords_field, | ||
create_support=True, | ||
mesh=meshed_region_1 | ||
).eval()[0] | ||
|
||
Plot the graph | ||
^^^^^^^^^^^^^^ | ||
|
||
Plot a graph of the norm of the displacement results along the |Line| length using the | ||
`matplotlib <matplotlib_github_>`_ library. | ||
|
||
To get the |Line| length you can use the |Line.path| method. It gives the 1D line coordinates, based on | ||
the points where the line was discretized. | ||
|
||
.. jupyter-execute:: | ||
|
||
# Define the norm of the displacement results | ||
norm_disp = ops.math.norm(field=mapped_disp_line).eval() | ||
|
||
# Define the point coordinates on the line length | ||
line_length_points = line_1.path | ||
|
||
# Define the plot figure | ||
plt.plot(line_length_points, norm_disp.data) | ||
|
||
# Graph formating | ||
plt.xlabel("Line length"); plt.ylabel("Displacement norm field"); plt.title("Displacement evolution on the line") | ||
|
||
# Display the graph | ||
plt.show() | ||
|
||
.. _ref_graph_result_time: | ||
|
||
Results data vs. time | ||
--------------------- | ||
|
||
In this tutorial, we plot the displacement results over time for a transient analysis. | ||
For more information about using PyDPF-Core with a transient analysis, see the :ref:`static_transient_examples` examples. | ||
|
||
Define the results data | ||
^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
First, import a transient results file. For this tutorial, you can use the one available in the |Examples| module. | ||
For more information about how to import your own result file in DPF, see | ||
the :ref:`ref_tutorials_import_data` tutorials section. | ||
|
||
.. jupyter-execute:: | ||
|
||
# Import the ``ansys.dpf.core`` module | ||
from ansys.dpf import core as dpf | ||
# Import the examples module | ||
from ansys.dpf.core import examples | ||
# Import the operators module | ||
from ansys.dpf.core import operators as ops | ||
|
||
# Import the ``matplotlib.pyplot`` module | ||
import matplotlib.pyplot as plt | ||
|
||
# Define the result file path | ||
result_file_path_2 = examples.download_transient_result() | ||
|
||
The results will be mapped over a defined path of coordinates. Thus, we need the spatial support to | ||
those coordinates: the mesh. The mesh object in DPF is a |MeshedRegion|. | ||
|
||
You can obtain a |MeshedRegion| by creating your own from scratch or by getting it from a result file. | ||
For more information, see the :ref:`ref_tutorials_create_a_mesh_from_scratch` and | ||
:ref:`ref_tutorials_get_mesh_from_result_file` tutorials. | ||
|
||
Here, we extract it from the result file. | ||
|
||
.. jupyter-execute:: | ||
|
||
# Create the model | ||
model_2 = dpf.Model(data_sources=result_file_path_2) | ||
|
||
# Extract the mesh | ||
meshed_region_2 = model_2.metadata.meshed_region | ||
|
||
Extract the results to be plotted on the graph. Here, we plot the maximum and minimum | ||
displacement results over time. | ||
|
||
First extract the displacement results for all the time frequencies. | ||
|
||
.. jupyter-execute:: | ||
|
||
# Get the displacement results | ||
disp_results_2 = model_2.results.displacement.on_all_time_freqs.eval() | ||
|
||
Next, define the minimal and maximal displacements for each time step by using the |min_max_fc| | ||
operator. | ||
|
||
.. jupyter-execute:: | ||
|
||
# Define the min_max operator and give the normed displacement results | ||
min_max_op = ops.min_max.min_max_fc(fields_container=ops.math.norm_fc(disp_results_2)) | ||
|
||
# Get the max displacement results | ||
max_disp = min_max_op.eval(pin=1) | ||
|
||
# Get the min displacement results | ||
min_disp = min_max_op.eval(pin=0) | ||
|
||
Define the time data | ||
^^^^^^^^^^^^^^^^^^^^ | ||
|
||
The results time steps in DPF are given by the |TimeFreqSupport| object. You can extract it | ||
from the displacement results |Field|. | ||
|
||
.. jupyter-execute:: | ||
|
||
# Define the time steps | ||
time_steps_1 = disp_results_2.time_freq_support.time_frequencies | ||
|
||
# Print the time frequencies | ||
print(time_steps_1) | ||
|
||
The time steps are given in a |Field|. To plot the graph you need to extract the | ||
|Field| data. | ||
|
||
.. jupyter-execute:: | ||
|
||
# Get the time steps data | ||
time_data = time_steps_1.data | ||
|
||
|
||
Plot the graph | ||
^^^^^^^^^^^^^^ | ||
|
||
Plot a graph of the minimal and maximal displacements over time using the | ||
`matplotlib <matplotlib_github_>`_ library. | ||
|
||
.. jupyter-execute:: | ||
|
||
# Define the plot figure | ||
plt.plot(time_data, max_disp.data, "r", label="Max") | ||
plt.plot(time_data, min_disp.data, "b", label="Min") | ||
|
||
# Graph formating | ||
plt.xlabel("Time (s)"); plt.ylabel("Displacement (m)"); plt.legend(); | ||
|
||
# Display the graph | ||
plt.show() |
Oops, something went wrong.
Oops, something went wrong.
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.