I’m having a curve (parabol) from 0 to 1 on both axes as follows:
I generate another curve by moving the original curve along the x-axis and combine both to get the following graph:
How can I remove the intersected section to have only the double bottoms pattern like this:
The code I use for the graph:
JavaScript
x
38
38
1
import numpy as np
2
import matplotlib.pyplot as plt
3
4
def get_parabol(start=-1, end=1, steps=100, normalized=True):
5
x = np.linspace(start, end, steps)
6
y = x**2
7
if normalized:
8
x = np.array(x)
9
x = (x - x.min())/(x.max() - x.min())
10
y = np.array(y)
11
y = (y - y.min())/(y.max() - y.min())
12
return x, y
13
def curve_after(x, y, x_ratio=1/3, y_ratio=1/2, normalized=False):
14
x = x*x_ratio + x.max() - x[0]*x_ratio
15
y = y*y_ratio + y.max() - y.max()*y_ratio
16
if normalized:
17
x = np.array(x)
18
x = (x - x.min())/(x.max() - x.min())
19
y = np.array(y)
20
y = (y - y.min())/(y.max() - y.min())
21
return x, y
22
def concat_arrays(*arr, axis=0, normalized=True):
23
arr = np.concatenate([*arr], axis=axis).tolist()
24
if normalized:
25
arr = np.array(arr)
26
arr = (arr - arr.min())/(arr.max() - arr.min())
27
return arr
28
29
x, y = get_parabol()
30
new_x, new_y = curve_after(x, y, x_ratio=1, y_ratio=1, normalized=False)
31
new_x = np.add(x, 0.5)
32
# new_y = np.add(y, 0.2)
33
xx = concat_arrays(x, new_x, normalized=True)
34
yy = concat_arrays(y, new_y, normalized=True)
35
36
# plt.plot(x, y, '-')
37
plt.plot(xx, yy, '--')
38
I’m doing a research on pattern analysis that requires me to generate patterns with mathematical functions.
Could you show me a way to achieve this? Thank you!
Advertisement
Answer
First off, I would have two different parabola functions such that:
JavaScript
1
7
1
import numpy as np
2
import matplotlib.pyplot as plt
3
4
x = np.linspace(-1, 1, 100)
5
y1 = np.add(x, 0.3)**2 # Parabola centered at -0.3
6
y2 = np.add(x, -0.3)**2 # Parabola centered at 0.3
7
You can choose your own offsets for y1 and y2 depending on your needs.
And then it’s simply take the min of the two arrays
JavaScript
1
3
1
y_final = np.minimum(y1, y2)
2
plt.plot(x, y_final, '--')
3