Given the following image, where plt.Polygon
was used to create the triangle and plt.contourf
the contour, i would like to ‘cut’ off the regions marked in X so that i only get the contour inside the triangle. How would i do this?
I’ve tried placing NaN
values in the mentioned regions but the edges are square and therefore requires a large meshgrid to generate a ‘clean’ cut between the unwanted and wanted regions. Rather is it more appropiate to use plt.tricontourf
? If so how should one approach it?
Advertisement
Answer
Thanks @Zephyr for the tricontourf
solution, Heres how i solved it without the use of tricontourf
and simply a meshgrid
JavaScript
x
69
69
1
# =============== Define Points =============== #
2
left_point = np.array([8, 1])
3
top_point = np.array([10.75, 3.75])
4
right_point = np.array([13.5, 1])
5
6
# =============== Define Left Line Eq: y = mx+c =============== #
7
left_m = (top_point[-1] - left_point[-1]) / (top_point[0] - left_point[0])
8
left_c = left_point[-1] - left_m*left_point[0]
9
# =============== Define Right Line Eq: y = mx+c =============== #
10
right_m = (right_point[-1] - top_point[-1]) / (right_point[0] - top_point[0])
11
right_c = right_point[-1] - right_m*right_point[0]
12
13
# =============== Generate Spaced Points on Both Lines =============== #
14
n_points = 100
15
16
# x-coordinates on left line
17
X_left = np.linspace(left_point[0], top_point[0], n_points)
18
# y-coordinates on left line
19
Y_left = left_m * X_left + left_c
20
21
# x-coordinates on right line
22
X_right = np.linspace(right_point[0], top_point[0], n_points)
23
# y-coordinates on right line
24
Y_right = right_m * X_right + right_c
25
26
# Concatenate Left line X and Right line X: [X_left, X_right]
27
LR_X = np.hstack([X_left[:, None], X_right[:, None]])
28
29
30
# =============== Generate Spaced Points IN BETWEEN points on both lines =============== #
31
32
"""
33
# We can use lists to generate points between each adjacent points on the left/right line
34
# Then turn them into arrays
35
# NOTE: Y_left and Y_right are essentially equal so we could just use one of them
36
37
# XX = []
38
# YY = []
39
# for ii in range(n_points):
40
# XX.append(np.linspace(LR_X[ii, 0], LR_X[ii, 1], n_points).reshape(1, -1))
41
# YY.append(Y_left[ii]*np.ones(n_points).reshape(1, -1))
42
# XX = np.vstack(XX)
43
# YY = np.vstack(YY)
44
45
"""
46
47
# Or We could do this (Same thing)
48
XX = np.meshgrid(np.linspace(LR_X[:, 0], LR_X[:, 1], n_points))[0].reshape(n_points, n_points).T
49
YY = np.meshgrid([Y_left*np.ones(n_points)]*n_points)[0].reshape(n_points, n_points).T
50
51
# Im using a model to predict each point, so i had to flatten it out first
52
# i.e. XX.shape = (100, 100); YY.shape = (100, 100), WW.shape = (100*100, 2)
53
WW = np.c_[XX.ravel(), YY.ravel()]
54
ZZ = model.predict(WW).reshape(XX.shape)
55
56
# =============== Contour/Surface Plots =============== #
57
# Contour plot
58
fig1 = plt.figure(1, figsize=(8, 6))
59
ax1 = fig1.add_subplot(111)
60
levels = np.arange(Y.min(), Y.max())
61
contour_map = ax1.contourf(XX, YY, ZZ, cmap='viridis')
62
contour = ax1.contour(XX, YY, ZZ)
63
cbar = fig1.colorbar(contour_map, )
64
65
# Surface Plot
66
fig2 = plt.figure(2, figsize=(10, 6))
67
ax2 = fig2.add_subplot(projection='3d')
68
ax2.plot_surface(XX, YY, ZZ, cmap='viridis')
69