python - 有没有办法使用 Matplotlib 中的时间序列数据生成类方波?
问题描述
我对 Python 和 Matplotlib 比较陌生。有没有办法使用熊猫系列(即时间序列)生成“方形”波?
例如,以下值在系列中:
12、34、97、-4、-100、-9、31、87、-5、-2、33、13、1
显然,如果我绘制这个系列,它不会显示为方波。
有没有办法告诉 Python 如果值大于零,则在零上方绘制一致的水平线(例如,假设将线绘制在 1 处),如果值低于零,则在零下方绘制一条水平线(例如,在-1)?
由于这是一个时间序列,我不希望它是一个完美的正方形。
解决方案
用作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: int64
推荐阅读
- c# - 在 C# 中删除单个数据集中相对于另一个数据集中的重复项
- android - 无法解析符号“@style/Widget.Design.CoordinatorLayout”
- c# - 如何用另一个代码调用 Input.GetButtonDown?
- haskell - 我们可以用多少种方式来表示 Haskell 中的 if else 语句?
- google-chrome - 谷歌浏览器是否支持通知表情符号?
- ruby-on-rails - 如何处理 Carrierwave 文件名:无
- unit-testing - React Native 单元测试 (Jest)
- c# - 列表框项目内的 ComboBox 的龚 WPF 问题
- c# - 检查 DBContext 是否有变化并在锅上生成更新脚本而不是使用包管理器控制台
- ios - 带有按钮和 TableView 的 GoogleMap swift