python-3.x - 如何在 matplotlib 中绘制垂直条
问题描述
我想在具有其他时间序列的图上显示 0 或 1 数组的值。
我怎样才能实现像下面的灰线一样的东西 - 除了我的会摆动得更多。
例如,如何在此处添加 osc:
import numpy as np
import matplotlib.pyplot as plt
import datetime
import pandas as pd
n = 100
x = range(n)
y = np.random.rand(100)
osc = np.random.randint(2, size=n)
plt.plot(x,y)
plt.show(block=True)
解决方案
好吧,您可以遍历这些值并调用axvspan(x0,x1,color=...,alpha=...)
;
import numpy as np
import matplotlib.pyplot as plt
n = 100
x = range(n)
y = np.random.randn(100).cumsum()
osc = np.random.randint(2, size=n)
plt.plot(x, y, color='crimson')
for x0, x1, os in zip(x[:-1], x[1:], osc):
if os:
plt.axvspan(x0, x1, color='blue', alpha=0.2, lw=0)
plt.margins(x=0)
plt.show()
请注意,仅使用前 99 个值osc
,因为只有 99 个区间。
推荐阅读
- python - 如何设置笔记本电脑麦克风以用于语音识别
- java - 同步线程以从 arduino 发送和接收数据
- reactjs - 如何在 react-bootstrap 导航栏中正确设置路由
- python - 构建一个能够以特定顺序运行一些任务的简单应用程序
- javascript - 根据另一个对象数组的属性值对数组进行排序
- php - pdomodel:只有 fech 1 行
- java - 从其他活动向 Handler 发送消息
- c - 为什么生成的解析器会出现语法错误?
- wordpress - 从我的虚拟主机下载 zip 文件
- django - 使用 post_save 信号更新用户配置文件问题。错误消息是用户在更新时已经存在