|
| 1 | +""" |
| 2 | +.. _ref_plottype: |
| 3 | +
|
| 4 | +Plot types |
| 5 | +========== |
| 6 | +
|
| 7 | +This example shows how to start an Ansys Dynamic Reporting |
| 8 | +service via a Docker image and create different plot items. |
| 9 | +The example focuses on showing how to use the API to generate |
| 10 | +different plot types. |
| 11 | +
|
| 12 | +.. note:: |
| 13 | + This example assumes that you do not have a local Ansys installation but |
| 14 | + are starting an Ansys Dynamic Reporting Service via a Docker image on |
| 15 | + a new database. |
| 16 | +
|
| 17 | +""" |
| 18 | + |
| 19 | +############################################################################### |
| 20 | +# Start an Ansys Dynamic Reporting service |
| 21 | +# ---------------------------------------- |
| 22 | +# |
| 23 | +# Start an Ansys Dynamic Reporting service via a Docker image on a new |
| 24 | +# database. The path for the database directory must be to an empty directory. |
| 25 | + |
| 26 | +import numpy as np |
| 27 | + |
| 28 | +import ansys.dynamicreporting.core as adr |
| 29 | + |
| 30 | +db_dir = r"C:\tmp\new_database" |
| 31 | +adr_service = adr.Service(ansys_installation="docker", db_directory=db_dir) |
| 32 | +session_guid = adr_service.start(create_db=True) |
| 33 | + |
| 34 | +############################################################################### |
| 35 | +# Create a simple table |
| 36 | +# --------------------- |
| 37 | +# |
| 38 | +# Let us start by creating a simple table and visualizing it. Create a table |
| 39 | +# with 5 columns and 3 rows. |
| 40 | + |
| 41 | +simple_table = adr_service.create_item(obj_name="Simple Table", source="Documentation") |
| 42 | +simple_table.item_table = np.array( |
| 43 | + [[0, 1, 2, 3, 4], [0, 3, 6, 9, 12], [0, 1, 4, 9, 16]], dtype="|S20" |
| 44 | +) |
| 45 | +simple_table.labels_row = ["X", "line", "square"] |
| 46 | + |
| 47 | +############################################################################### |
| 48 | +# You can use the labels_row attribute to set the row labels. Use the visualize |
| 49 | +# method on the object to see its representation. By default, it will be displayed |
| 50 | +# as a table |
| 51 | + |
| 52 | +simple_table.visualize() |
| 53 | + |
| 54 | + |
| 55 | +############################################################################### |
| 56 | +# |
| 57 | +# .. image:: /_static/simpletable.png |
| 58 | +# |
| 59 | +# Visualize as a line plot |
| 60 | +# ------------------------ |
| 61 | +# |
| 62 | +# Let us know create a new item that is the same as the previous simple table, |
| 63 | +# but this time we will set the plot attribute to line to visualize the values |
| 64 | +# as two line plots, and we will use the xaxis attribute to set which row should |
| 65 | +# be used as the X axis. We can also control the formatting and the title of the |
| 66 | +# axis separately with the *axis_format and *title attributes, as done below. |
| 67 | +# The result can be seen in the following image. |
| 68 | + |
| 69 | +line_plot = adr_service.create_item(obj_name="Line Plot", source="Documentation") |
| 70 | +line_plot.item_table = np.array([[0, 1, 2, 3, 4], [0, 3, 6, 9, 12], [0, 1, 4, 9, 16]], dtype="|S20") |
| 71 | +line_plot.labels_row = ["X", "line", "square"] |
| 72 | +line_plot.plot = "line" |
| 73 | +line_plot.xaxis = "X" |
| 74 | +line_plot.yaxis_format = "floatdot0" |
| 75 | +line_plot.xaxis_format = "floatdot1" |
| 76 | +line_plot.xtitle = "x" |
| 77 | +line_plot.ytitle = "f(x)" |
| 78 | +line_plot.visualize() |
| 79 | + |
| 80 | + |
| 81 | +############################################################################### |
| 82 | +# |
| 83 | +# .. image:: /_static/line_plot.png |
| 84 | +# |
| 85 | +# Visualize as a bar plot |
| 86 | +# ----------------------- |
| 87 | +# |
| 88 | +# Next, we will see how to create a bar plot, and decorate it with the same |
| 89 | +# attributes used in the previous code snippet. See the following image for |
| 90 | +# the resulting visualization. |
| 91 | + |
| 92 | +bar_plot = adr_service.create_item(obj_name="Bar Plot", source="Documentation") |
| 93 | +bar_plot.item_table = np.array([[0, 1, 2, 3, 4], [0.3, 0.5, 0.7, 0.6, 0.3]], dtype="|S20") |
| 94 | +bar_plot.plot = "bar" |
| 95 | +bar_plot.labels_row = ["ics", "my variable"] |
| 96 | +bar_plot.xaxis_format = "floatdot0" |
| 97 | +bar_plot.yaxis_format = "floatdot2" |
| 98 | +bar_plot.xaxis = "ics" |
| 99 | +bar_plot.yaxis = "my variable" |
| 100 | +bar_plot.visualize() |
| 101 | + |
| 102 | + |
| 103 | +############################################################################### |
| 104 | +# |
| 105 | +# .. image:: /_static/bar_plot.png |
| 106 | +# |
| 107 | +# Visualize a pie chart |
| 108 | +# --------------------- |
| 109 | +# |
| 110 | +# Next supported plot type is the pie chart. Please see the following code snippet |
| 111 | +# to generate the pie chart as in the following image. |
| 112 | + |
| 113 | + |
| 114 | +pie_plot = adr_service.create_item(obj_name="Pie Plot", source="Documentation") |
| 115 | +pie_plot.item_table = np.array([[10, 20, 50, 20]], dtype="|S20") |
| 116 | +pie_plot.plot = "pie" |
| 117 | +pie_plot.labels_column = ["Bar", "Triangle", "Quad", "Penta"] |
| 118 | +pie_plot.visualize() |
| 119 | + |
| 120 | + |
| 121 | +############################################################################### |
| 122 | +# |
| 123 | +# .. image:: /_static/pie_plot.png |
| 124 | +# |
| 125 | +# Visualize a heatmap |
| 126 | +# ------------------- |
| 127 | +# |
| 128 | +# Heatmaps are plots where at each (X,Y) position is associated the value of a |
| 129 | +# variable, colored according to a legend. Here the snippet on how to create |
| 130 | +# a heatmap representation - please note how nan values are also supported, |
| 131 | +# resulting in empty cells. |
| 132 | + |
| 133 | +heatmap = adr_service.create_item(obj_name="Heatmap", source="Documentation") |
| 134 | +heatmap.item_table = np.array( |
| 135 | + [ |
| 136 | + [0.00291, 0.01306, 0.02153, 0.01306, 0.00291], |
| 137 | + [0.01306, 0.05854, 0.09653, 0.05854, 0.01306], |
| 138 | + [0.02153, 0.09653, np.nan, 0.09653, 0.02153], |
| 139 | + [0.01306, 0.05854, 0.09653, 0.05854, 0.01306], |
| 140 | + [0.00291, 0.01306, 0.02153, 0.01306, 0.00291], |
| 141 | + ], |
| 142 | + dtype="|S20", |
| 143 | +) |
| 144 | +heatmap.plot = "heatmap" |
| 145 | +heatmap.format = "floatdot0" |
| 146 | +heatmap.visualize() |
| 147 | + |
| 148 | + |
| 149 | +############################################################################### |
| 150 | +# |
| 151 | +# .. image:: /_static/heatmap.png |
| 152 | +# |
| 153 | +# Visualize a parallel coordinate plot |
| 154 | +# ------------------------------------ |
| 155 | +# |
| 156 | +# Parallel coordinate plots are especially useful when analyzing data coming |
| 157 | +# from multiple runs. Place in each raw the values of variables for a given |
| 158 | +# simulation. Each column is a different variable. The parallel coordinate |
| 159 | +# plot allows you to visualize all this data in a way that stresses |
| 160 | +# correlations between variables and runs. |
| 161 | + |
| 162 | +parallel = adr_service.create_item() |
| 163 | +parallel.item_table = np.array( |
| 164 | + [ |
| 165 | + [54.2, 12.3, 1.45e5], |
| 166 | + [72.3, 9.3, 4.34e5], |
| 167 | + [45.4, 10.8, 8.45e4], |
| 168 | + [67.4, 12.2, 2.56e5], |
| 169 | + [44.8, 13.5, 9.87e4], |
| 170 | + ], |
| 171 | + dtype="|S20", |
| 172 | +) |
| 173 | +parallel.labels_column = ["Temperature", "Max. Pressure", "Max. Work"] |
| 174 | +parallel.plot = "parallel" |
| 175 | +parallel.visualize() |
| 176 | + |
| 177 | + |
| 178 | +############################################################################### |
| 179 | +# |
| 180 | +# .. image:: /_static/parallel_coord.png |
| 181 | +# |
| 182 | +# Visualize a Sankey diagram |
| 183 | +# -------------------------- |
| 184 | +# |
| 185 | +# A Sankey diagram allows you to visualize the relationship between |
| 186 | +# different elements. For this reprenstation, place the information |
| 187 | +# inside a squared table. |
| 188 | + |
| 189 | +sankey_plot = adr_service.create_item() |
| 190 | +sankey_plot.item_table = np.array( |
| 191 | + [ |
| 192 | + [0, 0, 8, 2, 0, 0], |
| 193 | + [0, 0, 0, 4, 0, 0], |
| 194 | + [0, 0, 0, 0, 8, 0], |
| 195 | + [0, 0, 0, 0, 5, 1], |
| 196 | + [0, 0, 0, 0, 0, 0], |
| 197 | + [0, 0, 0, 0, 0, 0], |
| 198 | + ], |
| 199 | + dtype="|S20", |
| 200 | +) |
| 201 | +sankey_plot.labels_row = ["A", "B", "C", "D", "E", "F"] |
| 202 | +sankey_plot.labels_column = ["A", "B", "C", "D", "E", "F"] |
| 203 | +sankey_plot.plot = "sankey" |
| 204 | +sankey_plot.visualize() |
| 205 | + |
| 206 | + |
| 207 | +############################################################################### |
| 208 | +# |
| 209 | +# .. image:: /_static/sankey.png |
| 210 | +# |
| 211 | +# Close the service |
| 212 | +# ----------------- |
| 213 | +# |
| 214 | +# Close the Ansys Dynamic Reporting service. The database with the items that |
| 215 | +# were created remains on disk. |
| 216 | + |
| 217 | +# sphinx_gallery_thumbnail_path = '_static/01_connect_3.png' |
| 218 | +adr_service.stop() |
0 commit comments