Skip to content

Commit ebfe69f

Browse files
committed
update: fix rdf plot on pyodide
1 parent 2dc2025 commit ebfe69f

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

utils/plot.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
import io
2+
import sys
13
from typing import Dict, List, Union
24

5+
import matplotlib
6+
import matplotlib.pyplot as plt
37
from mat3ra.made.material import Material
48
from mat3ra.made.tools.analyze.interface import ZSLMatchHolder
59
from mat3ra.made.tools.analyze.rdf import RadialDistributionFunction
@@ -60,8 +64,8 @@ def plot_twisted_interface_solutions(interfaces: List["Material"]) -> None:
6064

6165
x_values.append(angle)
6266
y_values.append(size)
63-
hover_texts.append(f"Interface {i+1}<br>Angle: {angle:.2f}°<br>Atoms: {size}<br>")
64-
trace_names.append(f"Interface {i+1}")
67+
hover_texts.append(f"Interface {i + 1}<br>Angle: {angle:.2f}°<br>Atoms: {size}<br>")
68+
trace_names.append(f"Interface {i + 1}")
6569

6670
plot_settings = {"x_title": "Twist Angle (°)", "y_title": "Number of Atoms", "title": "Twisted Interface Solutions"}
6771

@@ -73,7 +77,20 @@ def plot_rdf(material: "Material", cutoff: float = 10.0, bin_size: float = 0.1)
7377
"""
7478
Plot RDF for a material.
7579
"""
80+
is_pyodide = sys.platform == "emscripten"
81+
if is_pyodide:
82+
matplotlib.use("Agg")
83+
7684
rdf = RadialDistributionFunction.from_material(material, cutoff=cutoff, bin_size=bin_size)
7785
plot_distribution_function(
7886
rdf.bin_centers, rdf.rdf, xlabel="Distance (Å)", ylabel="g(r)", title="Radial Distribution Function (RDF)"
7987
)
88+
89+
if is_pyodide:
90+
from IPython.display import Image, display
91+
92+
buf = io.BytesIO()
93+
plt.savefig(buf, format="png")
94+
buf.seek(0)
95+
display(Image(buf.read()))
96+
plt.close()

0 commit comments

Comments
 (0)