我对Python和Matplotlib比较陌生。有没有一种使用熊猫序列(即时间序列)生成“方形”波的方法?
例如,本系列中有以下值:
12、34、97、-4、-100、-9、31、87、-5、-2、33、13、1
显然,如果我画这个系列,它不会出现在方波上。
有什么方法可以告诉Python,如果值大于零,则在零以上绘制一致的水平线(例如,假设在1处绘制直线),如果值低于零,则在零下绘制一条水平线(例如,在- 1)?
因为这是一个时间序列,我不期望它是一个完美的正方形。
发布于 2019-05-14 05:05:47
将np.clip用作:
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])或者Series.clip:
s = pd.Series(x)
s = s.clip(lower=-1, upper=1)如果它的值介于>=-1到<=1之间,那么使用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: int64https://stackoverflow.com/questions/56123305
复制相似问题