Skip to content

Commit 111348b

Browse files
AlejandroFernandezLucespyansys-ci-botjorgepiloto
authored
feat(plotly): Link names to hover info (#407)
Co-authored-by: pyansys-ci-bot <[email protected]> Co-authored-by: Jorge Martínez <[email protected]>
1 parent c165efe commit 111348b

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Feat(plotly): Link names to hover info

examples/01-basic-plotly-examples/plain-usage.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def name(self):
107107
color='lightblue',
108108
opacity=0.50
109109
)
110-
pl.plot(custom_mesh3d)
110+
pl.plot(custom_mesh3d, name="CustomMesh3d")
111111

112112
# Show other plotly objects like Scatter3d
113113
from plotly.graph_objects import Scatter3d
@@ -119,6 +119,6 @@ def name(self):
119119
mode='markers',
120120
marker=dict(size=5, color='red')
121121
)
122-
pl.plot(scatter)
122+
pl.plot(scatter, name="CustomScatter3d")
123123

124124
pl.show()

src/ansys/tools/visualization_interface/backends/plotly/plotly_interface.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ def plot_iter(self, plotting_list: Iterable[Any]) -> None:
144144
def plot(
145145
self,
146146
plottable_object: Union[PolyData, pv.MultiBlock, MeshObjectPlot, go.Mesh3d],
147+
name: str = None,
147148
**plotting_options
148149
) -> None:
149150
"""Plot a single object using Plotly.
@@ -154,27 +155,34 @@ def plot(
154155
The object to plot. Can be a PyVista PolyData, MultiBlock, a MeshObjectPlot, or a Plotly Mesh3d.
155156
plotting_options : dict
156157
Additional plotting options.
158+
name : str, optional
159+
Name of the mesh for labeling in Plotly. Overrides the name from MeshObjectPlot if provided.
157160
"""
158161
if isinstance(plottable_object, MeshObjectPlot):
159162
mesh = plottable_object.mesh
163+
name = name or plottable_object.name
160164
else:
161165
mesh = plottable_object
162166

163167
if isinstance(mesh, (PolyData, pv.MultiBlock)):
164168
mesh_result = self._pv_to_mesh3d(mesh)
165-
166169
# Handle both single mesh and list of meshes
167170
if isinstance(mesh_result, list):
168171
# MultiBlock case - add all meshes
169172
for mesh_3d in mesh_result:
173+
mesh_3d.name = name or mesh_3d.name
170174
self._fig.add_trace(mesh_3d)
171175
else:
172-
# Single PolyData case
176+
mesh_result.name = name if name is not None else mesh_result.name
173177
self._fig.add_trace(mesh_result)
174178
elif isinstance(plottable_object, go.Mesh3d):
179+
if name is not None:
180+
plottable_object.name = name
175181
self._fig.add_trace(plottable_object)
176182
else:
183+
# Try in case there is a compatible Plotly object
177184
try:
185+
plottable_object.name = name
178186
self._fig.add_trace(plottable_object)
179187
except Exception:
180188
raise TypeError("Unsupported plottable_object type for PlotlyInterface.")

0 commit comments

Comments
 (0)