Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 5 additions & 21 deletions utils/plot.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import io
import sys
from typing import Dict, List, Tuple, Union

from IPython.display import Image, display
from mat3ra.made.material import Material
from mat3ra.made.tools.analyze.interface import ZSLMatchHolder
from mat3ra.made.tools.analyze.rdf import RadialDistributionFunction
from mat3ra.utils.jupyterlite.plot import plot_distribution_function, scatter_plot_2d
from mat3ra.utils.jupyterlite.plot import plot_distribution_function, render_figure, scatter_plot_2d
from matplotlib import pyplot as plt


Expand Down Expand Up @@ -43,7 +40,7 @@ def plot_strain_vs_area(matches: List["ZSLMatchHolder"], settings: Dict[str, Uni
}

fig = scatter_plot_2d(x_values, y_values, hover_texts, plot_settings, trace_names)
fig.show()
render_figure(fig)


def plot_twisted_interface_solutions(interfaces: List["Material"]) -> None:
Expand All @@ -70,31 +67,18 @@ def plot_twisted_interface_solutions(interfaces: List["Material"]) -> None:
plot_settings = {"x_title": "Twist Angle (°)", "y_title": "Number of Atoms", "title": "Twisted Interface Solutions"}

fig = scatter_plot_2d(x_values, y_values, hover_texts, plot_settings, trace_names)
fig.show()
render_figure(fig)


def plot_rdf(material: "Material", cutoff: float = 10.0, bin_size: float = 0.1) -> None:
"""
Plot RDF for a material.
"""
is_pyodide = sys.platform == "emscripten"
if is_pyodide:
# This is needed so that plt is adjusted before import to work in Pyodide environment
plt.switch_backend("Agg")

rdf = RadialDistributionFunction.from_material(material, cutoff=cutoff, bin_size=bin_size)
plot_distribution_function(
rdf.bin_centers, rdf.rdf, xlabel="Distance (Å)", ylabel="g(r)", title="Radial Distribution Function (RDF)"
)

if is_pyodide:
# Necessary to display the plot in Pyodide environment
buf = io.BytesIO()
plt.savefig(buf, format="png")
buf.seek(0)
display(Image(buf.read()))
plt.close()


def plot_series(
series: List[Dict],
Expand Down Expand Up @@ -124,12 +108,12 @@ def plot_series(
x_labels = [str(item[x_key]) for item in series]
y_values = [item[y_key] for item in series]
x_indices = list(range(len(series)))
_, ax = plt.subplots(figsize=figsize)
figure, ax = plt.subplots(figsize=figsize)
ax.plot(x_indices, y_values, marker=marker)
ax.set_xticks(x_indices)
ax.set_xticklabels(x_labels, rotation=rotation, ha="right")
ax.set_xlabel(xlabel)
ax.set_ylabel(ylabel)
ax.set_title(title)
plt.tight_layout()
plt.show()
render_figure(figure)
Loading