Reputation: 31
I am relatively new to Python and Matplotlib. Is there a way generate a "square-like" wave using a Panda series (i.e., a Time Series)?
For example, the following values are in the series:
12, 34, 97, -4, -100, -9, 31, 87, -5, -2, 33, 13, 1
Obviously, if I plot this series, it's not going to appear as a square wave.
Is there a way to tell Python that if the value is greater than zero, then plot consistent horizontal line above zero (e.g., let's say plot the line at 1), and if the value is below zero, plot a horizontal line below zero (e.g., at -1)?
Since this is a time series, I don't expect for it to be a perfect square.
Upvotes: 1
Views: 361
Reputation: 13255
Use np.clip
as:
x=[12, 34, 97, -4, -100, -9, 31, 87, -5, -2, 33, 13, 1]
np.clip(x, a_min=-1, a_max=1)
array([ 1, 1, 1, -1, -1, -1, 1, 1, -1, -1, 1, 1, 1])
Or Series.clip
:
s = pd.Series(x)
s = s.clip(lower=-1, upper=1)
If it has values between >=-1 to <=1 then use np.where
:
x = np.where(np.array(x)>0, 1, -1) # for series s = np.where(s>0, 1, -1)
print(s)
0 1
1 1
2 1
3 -1
4 -1
5 -1
6 1
7 1
8 -1
9 -1
10 1
11 1
12 1
dtype: int64
Upvotes: 2