From dd801781e5d4f168a96a00f6962956785aac2132 Mon Sep 17 00:00:00 2001 From: "Anastasia K. Lavrinenko" <75916717+AstyLavrinenko@users.noreply.github.com> Date: Fri, 20 Oct 2023 13:50:33 +0200 Subject: [PATCH 1/3] Update _density.py Fix the lattice mismatch between Volume and Structure, change Structure to optional argument, create structure coords using Volume --- src/gemdat/plots/_density.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/gemdat/plots/_density.py b/src/gemdat/plots/_density.py index def8404b..249acd61 100644 --- a/src/gemdat/plots/_density.py +++ b/src/gemdat/plots/_density.py @@ -71,7 +71,8 @@ def plot_points(points: np.ndarray, point_size : int, optional Size of the points """ - colors = px.colors.qualitative.G10 + colors_by_site = {site: px.colors.qualitative.G10[i] for i, site in enumerate(np.unique(np.array(labels)))} + colors = list(map(colors_by_site.get, labels)) assert len(points) == len(labels) @@ -158,7 +159,7 @@ def plot_volume( showlegend=False)) -def density(vol: Volume, structure: Structure) -> go.Figure: +def density(vol: Volume, structure = None) -> go.Figure: """Create density plot from volume and structure. Uses plotly as plotting backend. @@ -167,7 +168,7 @@ def density(vol: Volume, structure: Structure) -> go.Figure: --------- vol : Volume Input volume - structure : Structure + structure : Structure, optional Input structure Returns @@ -175,12 +176,13 @@ def density(vol: Volume, structure: Structure) -> go.Figure: fig : go.Figure Output as plotly figure """ - lattice = structure.lattice + lattice = vol.lattice fig = go.Figure() plot_lattice_vectors(lattice, fig=fig) - plot_points(structure.cart_coords, structure.labels, fig=fig) + if structure: + plot_points(lattice.get_cartesian_coords(structure.frac_coords), structure.labels, fig=fig) plot_volume(vol, fig=fig) fig.update_layout(title='Density', From afa5e72cecd54284f84eb264f302a85f59b99edd Mon Sep 17 00:00:00 2001 From: Stef Smeets Date: Tue, 24 Oct 2023 09:47:54 +0200 Subject: [PATCH 2/3] Plot structure cartesian coords --- src/gemdat/plots/_density.py | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/gemdat/plots/_density.py b/src/gemdat/plots/_density.py index 249acd61..755a5c41 100644 --- a/src/gemdat/plots/_density.py +++ b/src/gemdat/plots/_density.py @@ -1,6 +1,6 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Sequence +from typing import TYPE_CHECKING, Optional, Sequence import numpy as np import plotly.express as px @@ -71,7 +71,10 @@ def plot_points(points: np.ndarray, point_size : int, optional Size of the points """ - colors_by_site = {site: px.colors.qualitative.G10[i] for i, site in enumerate(np.unique(np.array(labels)))} + colors_by_site = { + site: px.colors.qualitative.G10[i] + for i, site in enumerate(np.unique(np.array(labels))) + } colors = list(map(colors_by_site.get, labels)) assert len(points) == len(labels) @@ -159,7 +162,7 @@ def plot_volume( showlegend=False)) -def density(vol: Volume, structure = None) -> go.Figure: +def density(vol: Volume, structure: Optional[Structure] = None) -> go.Figure: """Create density plot from volume and structure. Uses plotly as plotting backend. @@ -176,15 +179,17 @@ def density(vol: Volume, structure = None) -> go.Figure: fig : go.Figure Output as plotly figure """ - lattice = vol.lattice - fig = go.Figure() - plot_lattice_vectors(lattice, fig=fig) - if structure: - plot_points(lattice.get_cartesian_coords(structure.frac_coords), structure.labels, fig=fig) + plot_lattice_vectors(vol.lattice, fig=fig) plot_volume(vol, fig=fig) + if structure: + if structure.lattice == vol.lattice: + plot_points(structure.cart_coords, structure.labels, fig=fig) + else: + plot_structure(structure, fig=fig) + fig.update_layout(title='Density', scene={ 'aspectmode': 'manual', @@ -193,9 +198,9 @@ def density(vol: Volume, structure = None) -> go.Figure: 'y': 1, 'z': 1 }, - 'xaxis_title': 'X (Angstrom)', - 'yaxis_title': 'Y (Angstrom)', - 'zaxis_title': 'Z (Angstrom)' + 'xaxis_title': 'X (Ångstrom)', + 'yaxis_title': 'Y (Ångstrom)', + 'zaxis_title': 'Z (Ångstrom)' }, legend={ 'orientation': 'h', From ab89780c84d0105f8199579e8af657e50d5c9940 Mon Sep 17 00:00:00 2001 From: Stef Smeets Date: Tue, 24 Oct 2023 10:00:49 +0200 Subject: [PATCH 3/3] Simplify color spec --- src/gemdat/plots/_density.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/gemdat/plots/_density.py b/src/gemdat/plots/_density.py index 755a5c41..89088199 100644 --- a/src/gemdat/plots/_density.py +++ b/src/gemdat/plots/_density.py @@ -71,17 +71,13 @@ def plot_points(points: np.ndarray, point_size : int, optional Size of the points """ - colors_by_site = { - site: px.colors.qualitative.G10[i] - for i, site in enumerate(np.unique(np.array(labels))) - } - colors = list(map(colors_by_site.get, labels)) - assert len(points) == len(labels) + colors = dict(zip(labels, px.colors.qualitative.G10)) + for i, (x, y, z) in enumerate(points): - color = colors[i % len(colors)] label = labels[i] + color = colors[label] fig.add_trace( go.Scatter3d(x=[x],