Simulating Wave Interference with Plotly Contour Plots Using Java‑Generated Data
Learn how to create wave interference simulations by generating data with Java, processing it in Python, and visualizing the results using Plotly's contour plot and heatmap features, including step‑by‑step code snippets and configuration details for producing clear interference patterns.
Overview
This example demonstrates how to generate a synthetic two‑dimensional wave‑interference dataset in Java, store it as a plain‑text matrix, and visualise the result with Python's Plotly library as a heat‑map style contour plot.
Java data generation
The Java program creates a 100 × 200 matrix where each element is the superposition of two sinusoidal waves originating from two fixed points. The steps are:
Define two source points point1(50,50) and point2(150,50).
Iterate over i = 0..99 (y‑axis) and j = 0..199 (x‑axis).
For each grid point compute the normalised distance to each source:
double x = point.distance(point1) % lambda / lambda;
double y = point.distance(point2) % lambda / lambda;where lambda = 6 is the wavelength in grid units.
Calculate the wave contribution of each source using a sine function:
double xx = Math.sin(x * 2 * Math.PI);
double yy = Math.sin(y * 2 * Math.PI);Store the sum xx + yy in a temporary List<Double> for the current row, then add the row to data.
After the loops, flatten the matrix into a single comma‑separated line per row and write the whole content to intervene.log using the helper method logLong.
The resulting file contains 100 lines; each line holds 200 floating‑point values separated by commas.
Python Plotly visualisation
The Python script reads the CSV‑style log file, builds a numeric matrix z, and creates an offline Plotly figure.
#!/usr/bin/python
# coding=utf-8
import plotly.offline as offline
import plotly.graph_objs as go
z = []
with open('intervene.log') as f:
for line in f:
# split the line by commas and convert each entry to float
row = [float(v) for v in line.strip().split(',')]
z.append(row)
trace = go.Heatmap(
z=z,
colorscale='Viridis', # optional colour map
showscale=True,
zsmooth='best',
connectgaps=True
)
fig = go.Figure(data=[trace])
offline.plot(fig, filename='wave_interference.html')Key parameters:
z : 2‑D list of float values representing the interference amplitude.
colorscale : defines the colour mapping; any Plotly colour scale can be used.
zsmooth='best' : enables smoothing of the heat‑map.
connectgaps=True : fills missing data points (not needed here but kept for robustness).
The script uses Plotly’s offline mode, so no API key or internet connection is required. The generated HTML file ( wave_interference.html) can be opened in any modern browser to view the contour/heat‑map representation of the interference pattern.
Running the workflow
Compile and run the Java class Intervene. Ensure the SourceCode base class provides the logLong(String filename, String content) method that writes the string to the given file.
Verify that intervene.log appears in the working directory and contains 100 lines of 200 comma‑separated numbers.
Execute the Python script (requires plotly library, installable via pip install plotly).
Open the produced wave_interference.html in a browser to inspect the interference pattern.
Result
The heat‑map visualises constructive and destructive interference zones created by the two point sources. Bright regions correspond to constructive interference (high amplitude), while darker regions indicate destructive interference.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
