本人在学习使用 plotly 的contour plots 制作的时候,发现利用这个表格制作波的干涉模拟方面有很不错的效果,因为之前被各种波动方程和振动方程教育了很久,所以就用波函数来开动,下面分享代码,供大家参考。(我用 java 写的代码模拟的波函数的测试数据)
下面是 plotly 的全部代码,里面包含了两种生成 contour plots 图表的方式,一个生成的是单个图表,但是带着等高线,所以被我放弃了。另外一个生成是图表矩阵,我选的row=1,cols=1的模式。
1#!/usr/bin/python
2# coding=utf-8
3
4import plotly.plotly
5from plotly.graph_objs import *
6import plotly.graph_objs as go
7import plotly.tools as tool
8
9z = []
10with open("/Users/Vicky/Documents/workspace/fission/long/intervene.log") as apidata:
11 for i in apidata:
12 data = i.split("\n")[0].split(",")
13 z.append(data)
14'''
15#这是单独一个表格的情况,但没找到去掉等高线的方法
16data = Data([
17 Contour(
18 z=z,
19 contours = dict(
20 coloring="heatmap"
21 )
22 )
23])
24plotly.offline.plot(data,filename="3333.html")
25'''
26data = {
27 'z': z,
28 'connectgaps': True,
29 'type': 'heatmap',
30 'zsmooth': 'best',
31 'showscale': True
32}
33fig = tool.make_subplots(rows=1, cols=1)
34fig.append_trace(data, 1, 1)
35plotly.offline.plot(fig,filename= "3333.html")
下面是 java 生成数据的代码:
1package practise;
2
3import java.awt.Point;
4import java.util.ArrayList;
5import java.util.List;
6import source.SourceCode;
7
8public class Intervene extends SourceCode {
9 public List<List<Double>> data = new ArrayList<>();
10
11 public static void main(String[] args) {
12 Intervene intervene = new Intervene();
13 intervene.testDemo001();
14 }
15
16 public void testDemo001() {
17 Point point1 = new Point(50, 50);
18 Point point2 = new Point(150, 50);
19 int lamda = 6;
20 for (int i = 0; i < 100; i++) {// y 轴
21 List<Double> distance = new ArrayList<>();
22 for (int j = 0; j < 200; j++) {// x 轴
23 Point point = new Point(j, i);
24 double x = point.distance(point1) % lamda / lamda;
25 double y = point.distance(point2) % lamda / lamda;
26 double xx = Math.sin(x * 2 * Math.PI);
27 double yy = Math.sin(y * 2 * Math.PI);
28 distance.add(xx + yy);
29 }
30 data.add(distance);
31 }
32 StringBuffer content = new StringBuffer();
33 int size = data.size();
34 for (int i = 0; i < size; i++) {
35 String text = data.get(i).toString();
36 text = text.substring(1, text.length() - 1);
37 if (i == 0)
38 content.append(text);
39 content.append(LINE + text);
40 }
41 logLong("intervene.log", content.toString());
42 }
43}
下面是生成图表: