I need to find the maximum x
value associated to the maximum y
value of the following function that I plot using Python matplotlib
module:
JavaScript
x
15
15
1
# Import modules
2
import numpy as np
3
from matplotlib import pyplot as plt
4
5
# Define the function
6
x = np.arange(0., 1000, 0.2)
7
y = np.abs(-np.exp(-x/1068)+np.exp(-x/46))
8
9
# Plot
10
plt.title("")
11
plt.xlabel("x")
12
plt.ylabel("y")
13
plt.plot(x,y)
14
plt.grid()
15
Hence, the trend is this one:
If I use the max()
function in such way:
JavaScript
1
2
1
print(max(y))
2
It prints only the maximum y value (that in this case is equal to 0.8306243772229114
). How can I do to have also the maximum x value ?
Advertisement
Answer
If x
is an np.array and you want to find the x
associated with the maximum y
, you can use np.argmax.
JavaScript
1
2
1
x_max = x[np.argmax(y)]
2