python - matplotlib 图表中感兴趣的阴影区域
问题描述
给定一个像
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2 * np.pi * t)
fig, ax = plt.subplots()
ax.plot(s)
ax.set(xlabel='time (s)', ylabel='voltage (mV)', title='sine')
ax.grid()
plt.show()
如何自动对图表的 y 值介于(例如)1.25 和 0.75 之间的图表的垂直切片(从底部到顶部)进行着色?
正弦在这里只是一个样本,绘图的实际值不太规则。
我在 matplotlib 的两条垂直线之间看到了 FIll,它看起来类似于这个问题,但那里的答案在固定 x 值之间隐藏了一个区域。我希望阴影区域由 y 值确定。
解决方案
您可能正在寻找ax.fill_between,它非常灵活(请参阅链接文档)。
对于您的具体情况,如果我理解正确,这应该足够了:
fig, ax = plt.subplots()
ax.plot(s)
ax.set(xlabel='time (s)', ylabel='voltage (mV)', title='sine')
ax.fill_between(range(len(s)), min(s), max(s), where=(s < 1.25) & (s > 0.75), alpha=0.5)
ax.grid()
推荐阅读
- python-3.x - 使用 Django 信号捕获更改后更改实例值
- reporting-services - SSRS:根据特定值计算行数
- snakemake - Snakemake 帮助功能
- spss - Kaplan Meier,审查数据
- flutter - 是否有任何有用的颤振包来处理用户权限?
- json - Beautifulsoup AJAX 表转 JSON
- sas - 在 SAS proc SQL 中重新创建 Tableau 函数已修复
- powershell - 如何在 powershell 中列出 onedrive 文件夹?
- vb.net - VS 提出这个建议。为什么 distinct 出现两次?
- python - 熊猫如何比较相似的行然后按条件删除