-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3D-matPlotLib.py
More file actions
84 lines (78 loc) · 5.2 KB
/
3D-matPlotLib.py
File metadata and controls
84 lines (78 loc) · 5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# 3D graph of a 2D array of data. Data came from a CSV file from Excel.
# Next step, use a bitmap for the source of data.
# Derived somewhat from https://stackoverflow.com/a/9170879/101252
import numpy as np
from mpl_toolkits.mplot3d import Axes3D # matplotlib
# Axes3D import has side effects, it enables using projection='3d' in add_subplot
import matplotlib.pyplot
import random
data = [
[0.0, 2.0, 4.0, 6.0, 8.0, 10.0, 12.0, 14.0, 15.9, 17.9, 19.9, 21.9, 23.9, 25.9, 27.9, 29.9, 31.9, 33.9, 36.0, 38.0,
40.0, 42.0, 44.0, 46.0, 48.0, 50.0],
[1.1, 6.8, 12.5, 18.0, 22.9, 26.8, 28.9, 29.5, 29.2, 29.2, 29.4, 29.7, 30.3, 31.1, 32.2, 33.4, 34.8, 36.4, 38.0,
39.7, 41.6, 43.4, 45.4, 47.4, 49.4, 51.4],
[2.3, 11.7, 21.1, 30.5, 39.1, 45.4, 47.5, 45.8, 42.1, 40.4, 38.6, 37.2, 36.4, 36.1, 36.3, 36.8, 37.6, 38.7, 40.0,
41.5, 43.1, 44.9, 46.8, 48.8, 50.8, 52.9],
[3.5, 16.5, 29.9, 43.8, 57.4, 68.2, 70.0, 64.2, 57.2, 51.7, 47.4, 44.2, 42.1, 40.7, 40.0, 39.9, 40.2, 40.8, 41.8,
43.1, 44.6, 46.3, 48.1, 50.1, 52.2, 54.3],
[4.8, 21.1, 38.2, 57.3, 78.7, 100.0, 100.0, 83.8, 71.0, 61.7, 55.0, 50.2, 46.9, 44.7, 43.2, 42.5, 42.4, 42.7, 43.4,
44.5, 45.9, 47.5, 49.4, 51.4, 53.6, 55.8],
[6.1, 24.7, 44.8, 68.5, 100.0, 100.0, 100.0, 100.0, 81.1, 69.1, 60.8, 54.9, 50.7, 47.8, 45.8, 44.7, 44.2, 44.2,
44.7, 45.7, 47.0, 48.6, 50.6, 52.7, 55.0, 57.4],
[7.5, 26.9, 47.9, 71.8, 100.0, 100.0, 100.0, 100.0, 84.5, 72.8, 64.1, 57.8, 53.2, 50.0, 47.7, 46.2, 45.5, 45.3,
45.7, 46.6, 47.9, 49.6, 51.6, 53.9, 56.4, 59.0],
[9.0, 27.8, 47.9, 70.9, 100.0, 100.0, 100.0, 100.0, 84.3, 73.3, 65.2, 59.1, 54.6, 51.2, 48.7, 47.1, 46.2, 45.9,
46.2, 47.1, 48.4, 50.2, 52.4, 55.0, 57.7, 60.6],
[10.5, 27.6, 45.2, 63.9, 83.4, 100.0, 100.0, 89.4, 79.3, 71.0, 64.3, 58.9, 54.7, 51.5, 49.1, 47.4, 46.4, 46.0, 46.3,
47.2, 48.6, 50.6, 53.1, 55.9, 59.0, 62.3],
[12.2, 26.8, 41.6, 56.2, 69.6, 79.5, 81.8, 78.2, 72.7, 67.1, 62.0, 57.6, 54.0, 51.0, 48.7, 47.0, 46.0, 45.6, 45.9,
46.8, 48.4, 50.7, 53.4, 56.7, 60.3, 64.1],
[13.9, 26.1, 38.2, 49.6, 59.6, 66.7, 69.6, 68.9, 66.2, 62.7, 59.1, 55.7, 52.6, 49.9, 47.7, 46.1, 45.0, 44.6, 44.9,
45.9, 47.7, 50.2, 53.5, 57.3, 61.5, 66.0],
[15.7, 25.8, 35.6, 44.7, 52.4, 58.1, 61.1, 61.7, 60.6, 58.6, 56.0, 53.4, 50.8, 48.4, 46.3, 44.7, 43.5, 43.0, 43.2,
44.2, 46.3, 49.2, 53.0, 57.5, 62.6, 67.9],
[17.7, 25.9, 33.9, 41.3, 47.5, 52.2, 55.1, 56.3, 56.1, 54.9, 53.1, 51.1, 48.8, 46.6, 44.6, 42.8, 41.4, 40.6, 40.7,
41.7, 44.0, 47.5, 52.0, 57.4, 63.5, 69.8],
[19.7, 26.5, 33.0, 39.0, 44.2, 48.2, 50.8, 52.2, 52.5, 51.9, 50.7, 48.9, 47.0, 44.8, 42.6, 40.6, 38.8, 37.6, 37.3,
38.2, 40.6, 44.7, 50.3, 56.9, 64.2, 71.9],
[21.8, 27.4, 32.8, 37.8, 42.1, 45.5, 48.0, 49.4, 49.9, 49.6, 48.7, 47.2, 45.3, 43.0, 40.6, 38.1, 35.7, 33.7, 32.6,
33.1, 35.7, 40.6, 47.5, 55.7, 64.6, 73.9],
[24.1, 28.7, 33.2, 37.4, 41.1, 44.0, 46.2, 47.6, 48.2, 48.1, 47.3, 45.9, 43.9, 41.5, 38.7, 35.6, 32.2, 29.0, 26.6,
25.9, 28.5, 34.7, 43.6, 53.9, 64.8, 76.1],
[26.4, 30.3, 34.1, 37.7, 40.8, 43.4, 45.4, 46.7, 47.3, 47.2, 46.5, 45.1, 43.1, 40.5, 37.2, 33.3, 28.7, 23.6, 18.7,
15.5, 17.6, 26.3, 38.3, 51.5, 64.9, 78.3],
[28.8, 32.1, 35.4, 38.5, 41.2, 43.5, 45.3, 46.5, 47.1, 47.1, 46.4, 45.1, 43.0, 40.2, 36.4, 31.6, 25.6, 18.1, 9.2,
0.0, 0.0, 14.6, 32.0, 48.9, 65.0, 80.6],
[31.3, 34.2, 37.0, 39.7, 42.1, 44.2, 45.9, 47.0, 47.6, 47.7, 47.1, 45.8, 43.8, 40.8, 36.7, 31.3, 24.0, 14.1, 0.0,
0.0, 0.0, 0.0, 26.3, 47.2, 65.6, 82.9],
[33.8, 36.4, 38.9, 41.3, 43.5, 45.4, 47.0, 48.1, 48.8, 48.9, 48.5, 47.4, 45.5, 42.6, 38.5, 32.8, 24.9, 14.2, 0.0,
0.0, 0.0, 0.0, 26.0, 48.0, 67.3, 85.3],
[36.4, 38.8, 41.1, 43.3, 45.3, 47.1, 48.6, 49.8, 50.5, 50.9, 50.6, 49.8, 48.2, 45.6, 41.9, 36.5, 28.8, 17.6, 0.0,
0.0, 0.0, 0.0, 29.6, 51.7, 70.4, 87.7],
[39.1, 41.2, 43.4, 45.4, 47.4, 49.1, 50.7, 51.9, 52.8, 53.3, 53.4, 53.0, 51.8, 49.9, 46.9, 42.5, 36.3, 27.6, 15.5,
0.0, 0.0, 20.6, 40.9, 58.8, 74.9, 90.1],
[41.8, 43.8, 45.9, 47.8, 49.7, 51.5, 53.0, 54.4, 55.5, 56.3, 56.8, 56.8, 56.4, 55.2, 53.3, 50.4, 46.2, 40.8, 34.6,
29.3, 31.1, 41.6, 54.6, 67.7, 80.3, 92.6],
[44.5, 46.5, 48.4, 50.4, 52.2, 54.0, 55.7, 57.2, 58.6, 59.7, 60.7, 61.3, 61.6, 61.4, 60.7, 59.4, 57.5, 55.0, 52.6,
51.4, 53.7, 59.9, 68.2, 77.1, 86.1, 95.0],
[47.3, 49.2, 51.1, 53.0, 54.9, 56.7, 58.5, 60.2, 61.8, 63.4, 64.8, 66.1, 67.2, 68.1, 68.8, 69.2, 69.3, 69.2, 69.3,
70.2, 72.4, 76.3, 81.2, 86.5, 92.0, 97.5],
[50.0, 51.9, 53.8, 55.7, 57.6, 59.5, 61.4, 63.3, 65.3, 67.2, 69.2, 71.1, 73.1, 75.1, 77.2, 79.2, 81.2, 83.3, 85.4,
87.4, 89.5, 91.6, 93.7, 95.8, 97.9, 100.0],
]
def fun(x, y):
zs = []
for i in range(len(x)):
zs.append(data[y[i]][x[i]])
return zs
fig = matplotlib.pyplot.figure()
ax = fig.add_subplot(111, projection='3d')
x_range = range(len(data[0]))
y_range = range(len(data))
X, Y = np.meshgrid(x_range, y_range)
zs = np.array(fun(np.ravel(X), np.ravel(Y)))
Z = zs.reshape(X.shape)
# Plot
red_blue_colormap = matplotlib.colors.LinearSegmentedColormap.from_list("", ["blue", "gray", "red"]) # https://stackoverflow.com/a/46778420/101252
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=red_blue_colormap)
matplotlib.pyplot.show()