Creating 3D Surface Plots of Double-Source Interference with Plotly (Python) and Data Generation in Java
This article demonstrates how to generate a 100×100 interference dataset in Java, compress it with a transparent layer, and visualize the double‑source interference pattern as a 3D surface plot using Plotly in Python, including full source code and rendered images.
After learning to draw 2D contour plots of double‑source interference, the author explores Plotly's 3D surface plotting capabilities to better illustrate the phenomenon, noting that the default Plotly surface plot has a square base, so a 100×100 interference matrix is flattened with a transparent overlay for aesthetic reasons.
The following Python code reads the generated data, constructs a matrix, and creates two overlapping surface traces—one with the original data and another flattened, semi‑transparent layer—to produce the final 3D visualization.
#!/usr/bin/python
# coding=utf-8
import plotly.plotly
z = []
with open("/Users/Vicky/Documents/workspace/fission/long/intervene.log") as apidata:
for i in apidata:
data = i.split("\n")[0].split(",")
z.append(data)
matrix = [[20 for zij in zi] for zi in z] # flatten the 3D plot
plotly.offline.plot([
dict(z=z, type="surface"),
dict(z=matrix, showscale=False, opacity=0.01, type="surface")
], filename="2222.html")The Java portion generates the interference data by calculating distances from two source points, applying sinusoidal functions, and writing the results to a log file.
package practise;
import java.awt.Point;
import java.util.ArrayList;
import java.util.List;
import source.SourceCode;
public class Intervene extends SourceCode {
public List
> data = new ArrayList<>();
public static void main(String[] args) {
Intervene intervene = new Intervene();
intervene.testDemo001();
}
public void testDemo001() {
Point point1 = new Point(25, 25);
Point point2 = new Point(75, 75);
int lamda = 6;
for (int i = 0; i < 100; i++) { // y axis
List
distance = new ArrayList<>();
for (int j = 0; j < 100; j++) { // x axis
Point point = new Point(j, i);
double x = point.distance(point1) % lamda / lamda;
double y = point.distance(point2) % lamda / lamda;
double xx = Math.sin(x * 2 * Math.PI);
double yy = Math.sin(y * 2 * Math.PI);
distance.add(xx + yy);
}
data.add(distance);
}
StringBuffer content = new StringBuffer();
int size = data.size();
for (int i = 0; i < size; i++) {
String text = data.get(i).toString();
text = text.substring(1, text.length() - 1);
if (i == 0)
content.append(text);
content.append(LINE + text);
}
logLong("intervene.log", content.toString());
}
}Two screenshots of the resulting 3D surface plots are displayed below, illustrating the flattened visual effect.
FunTester
10k followers, 1k articles | completely useless
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.