From 00d49f6acbff9958258cc20d7e9eb2a95dd1dd54 Mon Sep 17 00:00:00 2001 From: German <28149841+germa89@users.noreply.github.com> Date: Mon, 11 Dec 2023 11:19:59 +0100 Subject: [PATCH] Fixing some plot bugs (#2537) * Adding labelling automatic color * checking line color * fixing vplot show_volume_numbering * fixing function arguments * fixing pymapdl cmap * more tests * Adding more tests * Fix labels in vplot * disabling rerunning * fix repeated function --- .github/workflows/ci.yml | 2 +- examples/01-geometry/01-lines.py | 2 +- src/ansys/mapdl/core/mapdl.py | 19 +++++++++++++-- src/ansys/mapdl/core/plotting.py | 14 +++++++++++ src/ansys/mapdl/core/theme.py | 2 +- tests/test_plotting.py | 42 ++++++++++++++++++++++++++++++++ tests/test_post.py | 6 ++++- 7 files changed, 81 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e8e9c18773..b6a0773ae5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -32,7 +32,7 @@ env: DOCKER_PACKAGE: ghcr.io/ansys/mapdl DOCKER_IMAGE_VERSION_DOCS_BUILD: v23.1.0 ON_CI: True - PYTEST_ARGUMENTS: '-vv --durations=10 --maxfail=10 --reruns 7 --reruns-delay 5 --cov=ansys.mapdl.core --cov-report=html' + PYTEST_ARGUMENTS: '-vv --durations=10 --maxfail=1 --reruns 1 --reruns-delay 5 --cov=ansys.mapdl.core --cov-report=html' # Following env vars when changed will "reset" the mentioned cache, # by changing the cache file name. It is rendered as ...-v%RESET_XXX%-... diff --git a/examples/01-geometry/01-lines.py b/examples/01-geometry/01-lines.py index 0ba015bcab..7ec25e6ea4 100644 --- a/examples/01-geometry/01-lines.py +++ b/examples/01-geometry/01-lines.py @@ -44,7 +44,7 @@ k1 = mapdl.k("", 1, 1, 0) k2 = mapdl.k("", 0, 1, 0) lnum = mapdl.larc(k0, k1, k2, 2) -mapdl.lplot(background="w", color="k", line_width=5, cpos="xy") +mapdl.lplot(background="w", color="y", line_width=5, cpos="xy") ############################################################################### diff --git a/src/ansys/mapdl/core/mapdl.py b/src/ansys/mapdl/core/mapdl.py index 00fba5eec1..5123ebe340 100644 --- a/src/ansys/mapdl/core/mapdl.py +++ b/src/ansys/mapdl/core/mapdl.py @@ -1755,8 +1755,14 @@ def vplot( quality of the mesh to display. Varies between 1 (worst) to 10 (best). Applicable when ``vtk=True``. - show_numbering : bool, optional - Display line and keypoint numbers when ``vtk=True``. + show_volume_numbering : bool, optional + Display volume numbers when ``vtk=True``. + + show_area_numbering : bool, optional + Display area numbers when ``vtk=True``. + + show_line_numbering : bool, optional + Display line numbers when ``vtk=True``. **kwargs See :meth:`ansys.mapdl.core.plotting.general_plotter` for @@ -1812,10 +1818,19 @@ def vplot( ) meshes_ = get_meshes_from_plotter(pl) + center_volumes = pl.center for each_mesh in meshes_: each_mesh.cell_data["entity_num"] = int(each_volu) + if show_volume_numbering: + labels.append( + { + "points": np.array(center_volumes), + "labels": [int(each_volu)], + } + ) + meshes.extend(meshes_) meshes = [{"mesh": meshes}] diff --git a/src/ansys/mapdl/core/plotting.py b/src/ansys/mapdl/core/plotting.py index 490ecb9bbb..fd50e6117d 100644 --- a/src/ansys/mapdl/core/plotting.py +++ b/src/ansys/mapdl/core/plotting.py @@ -406,6 +406,20 @@ def _general_plotter( if background: plotter.set_background(background) + # Making sure that labels are visible in dark backgrounds + if not text_color and background: + bg = plotter.background_color.float_rgb + # from: https://graphicdesign.stackexchange.com/a/77747/113009 + gamma = 2.2 + threshold = ( + 0.2126 * bg[0] ** gamma + 0.7152 * bg[1] ** gamma + 0.0722 * bg[2] ** gamma + > 0.5 * gamma + ) + if threshold: + text_color = "black" + else: + text_color = "white" + for point in points: plotter.add_points( point["points"], diff --git a/src/ansys/mapdl/core/theme.py b/src/ansys/mapdl/core/theme.py index e9c019b1ff..d103a5ac11 100644 --- a/src/ansys/mapdl/core/theme.py +++ b/src/ansys/mapdl/core/theme.py @@ -55,7 +55,7 @@ class myEmptyClass: ) if _HAS_MATPLOTLIB: - PyMAPDL_cmap: ListedColormap = ListedColormap(MAPDL_colorbar, name="PyMAPDL", N=255) + PyMAPDL_cmap: ListedColormap = ListedColormap(MAPDL_colorbar, name="PyMAPDL") def get_ansys_colors(N=9): diff --git a/tests/test_plotting.py b/tests/test_plotting.py index d0710e4679..c9e5acab69 100644 --- a/tests/test_plotting.py +++ b/tests/test_plotting.py @@ -885,3 +885,45 @@ def test_cuadratic_beam(mapdl, cuadratic_beam_problem): ) is None ) + + +@pytest.mark.parametrize("background", ["white", "black", "green", "red"]) +def test_labels_colors_background(mapdl, make_block, background): + # Test if the labels change color according background + mapdl.nplot(background=background, nnum=True) + + +def test_vplot_show_volume_numbering(mapdl, make_block): + mapdl.vplot(show_volume_numbering=True) + + +def test_vplot_area_numbering(mapdl, make_block): + mapdl.vplot(show_area_numbering=True) + + +def test_vplot_line_numbering(mapdl, make_block): + mapdl.vplot(show_line_numbering=True) + + +def test_vplot_multi_numbering(mapdl, make_block): + mapdl.vplot( + show_area_numbering=True, show_line_numbering=True, show_volume_numbering=True + ) + + +def test_vplot_color(mapdl, make_block): + mapdl.vplot(color="gray") + + +def test_vplot_cpos(mapdl, make_block): + mapdl.vplot(cpos="xy") + + +def test_vplot_multiargs(mapdl, make_block): + mapdl.vplot( + color="gray", + cpos="xy", + show_volume_numbering=True, + show_line_numbering=False, + show_area_numbering=True, + ) diff --git a/tests/test_post.py b/tests/test_post.py index 387f662fa6..2789abbaf1 100644 --- a/tests/test_post.py +++ b/tests/test_post.py @@ -10,6 +10,7 @@ if has_dependency("pyvista"): from pyvista import Plotter from pyvista.plotting.renderer import CameraPosition + from ansys.mapdl.core.theme import PyMAPDL_cmap from ansys.mapdl.core import examples from ansys.mapdl.core.post import ( @@ -158,7 +159,10 @@ def test_disp_norm_all(mapdl, static_solve): @requires("pyvista") def test_disp_plot(mapdl, static_solve, comp): assert ( - mapdl.post_processing.plot_nodal_displacement(comp, smooth_shading=True) is None + mapdl.post_processing.plot_nodal_displacement( + comp, smooth_shading=True, cmap=PyMAPDL_cmap + ) + is None )