-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplotit.py
More file actions
116 lines (98 loc) · 2.92 KB
/
Copy pathplotit.py
File metadata and controls
116 lines (98 loc) · 2.92 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# Import Bessel function.
from scipy.special import jn
# Import colormaps.
from matplotlib import cm
# Import lighting object for shading surface plots.
from matplotlib.colors import LightSource
# Define grid of points.
points = np.linspace(-10, 10, 101)
X, Y = np.meshgrid(points, points)
R = np.sqrt(X ** 2 + Y ** 2)
Z = jn(0, R)
# Create an rgb array for single-color surfaces.
white = np.ones((Z.shape[0], Z.shape[1], 3))
red = white * np.array([1, 0, 0])
green = white * np.array([0, 1, 0])
blue = white * np.array([0, 0, 1])
# Set view parameters for all subplots.
azimuth = 45
altitude = 60
# Create empty figure.
fig = plt.figure(figsize=(18, 12))
# -------------------------------------------------------------------------
# Generate first subplot.
# -------------------------------------------------------------------------
# Create a light source object for light from
# 0 degrees azimuth, 0 degrees elevation.
light = LightSource(0, 0)
# Generate face colors for a shaded surface using either
# a color map or the uniform rgb color specified above.
illuminated_surface = light.shade_rgb(red, Z)
# Create a subplot with 3d plotting capabilities.
# This command will fail if Axes3D was not imported.
ax = fig.add_subplot(2, 2, 1, projection="3d")
ax.view_init(altitude, azimuth)
ax.plot_surface(
X,
Y,
Z,
rstride=1,
cstride=1,
linewidth=0,
antialiased=False,
facecolors=illuminated_surface,
)
# -------------------------------------------------------------------------
# Repeat the commands above for the other three subplots, but use different
# illumination angles and colors.
# -------------------------------------------------------------------------
light = LightSource(90, 0)
illuminated_surface = light.shade_rgb(green, Z)
ax = fig.add_subplot(2, 2, 2, projection="3d")
ax.view_init(altitude, azimuth)
ax.plot_surface(
X,
Y,
Z,
rstride=1,
cstride=1,
linewidth=0,
antialiased=False,
facecolors=illuminated_surface,
)
# -------------------------------------------------------------------------
light = LightSource(90, 45)
illuminated_surface = light.shade_rgb(blue, Z)
ax = fig.add_subplot(2, 2, 3, projection="3d")
ax.view_init(altitude, azimuth)
ax.plot_surface(
X,
Y,
Z,
rstride=1,
cstride=1,
linewidth=0,
antialiased=False,
facecolors=illuminated_surface,
)
# -------------------------------------------------------------------------
light = LightSource(180, 45)
illuminated_surface = light.shade(Z, cmap=cm.coolwarm)
ax = fig.add_subplot(2, 2, 4, projection="3d")
ax.view_init(altitude, azimuth)
ax.plot_surface(
X,
Y,
Z,
rstride=1,
cstride=1,
linewidth=0,
antialiased=False,
facecolors=illuminated_surface,
)
# -------------------------------------------------------------------------
plt.tight_layout()
plt.savefig("shading.png")