Reputation: 144
I want to find maxima and minima from a list, but after running my program, terminal shows error like "ValueError: x and y must have same first dimension, but have shapes (1, 2) and (2,),". How to fix this problem?
Code:
from scipy.signal import argrelextrema
data = np.array([-1, 0, 1, 2, 1, 0, -1, -2, -1, 0, 1, 2, 1 ])
plt.plot(data)
maximums = argrelextrema(data, np.greater)
plt.plot(maximums, data[maximums], "x")
minimums = np.array(argrelextrema(data, np.less))
plt.plot(minimums, data[minimums], ".")
plt.show()
Upvotes: 1
Views: 555
Reputation: 2249
As you can see in the documentation, maxima
and minima
are not 1 dimensional arrays but tuples of ndarrays
import numpy as np
from scipy.signal import argrelextrema
import matplotlib.pyplot as plt
data = np.array([-1, 0, 1, 2, 1, 0, -1, -2, -1, 0, 1, 2, 1 ])
maxima = argrelextrema(data, np.greater)
minima = argrelextrema(data, np.less)
let's check
print(maxima)
gives
(array([ 3, 11]),)
and
print(minima)
gives
(array([7]),)
Since you need the first element only of the tuple, you can do
plt.plot(data)
plt.plot(maxima[0], data[maxima[0]], "x", ms=10)
plt.plot(minima[0], data[minima[0]], ".", ms=10)
plt.show()
Upvotes: 1