N = len(RV_5min) - 1 RV_real_pred = np.zeros(N+1) RV_real_uub = np.zeros(N+1) RV_real_llb = np.zeros(N+1) RV_real_pred[0] = RV_5min[0] RV_real_uub[0] = RV_5min[0] RV_real_llb[0] = RV_5min[0] alpha=0.05 z_alpha = norm.ppf(1-alpha/2) for i in range(N): RV_real_pred[i+1] = c + beta*RV_5min[i] RV_real_uub[i+1] = RV_real_pred[i+1] + z_alpha*np.sqrt(np.var(RV_diff))*np.sqrt(1+beta**2) RV_real_llb[i+1] = RV_real_pred[i+1] - z_alpha*np.sqrt(np.var(RV_diff))*np.sqrt(1+beta**2) plt.plot(RV_5.index, np.maximum(0, RV_real_pred), 'k:') plt.plot(RV_5.index, np.maximum(0, RV_real_uub), 'r--', RV_5.index, np.maximum(0, RV_real_llb), 'r--') plt.xlabel('Time') plt.ylabel('RV') plt.legend(['Predicted', 'Upper Bound', 'Lower Bound']) plt.show() 画图x和y的长度不一样怎么改
时间: 2024-04-17 14:24:28 浏览: 144
asm.rar_The Don
为了绘制图形,确保x和y的长度一致,可以使用相同的时间轴作为x轴。假设RV_5min.index是时间轴,可以将其作为x轴,而RV_5min作为y轴。请注意,RV_5min和RV_diff的长度可能不同,因此需要相应调整。
下面是修正后的代码:
```python
N = len(RV_5min) - 1
RV_real_pred = np.zeros(N+1)
RV_real_uub = np.zeros(N+1)
RV_real_llb = np.zeros(N+1)
RV_real_pred[0] = RV_5min[0]
RV_real_uub[0] = RV_5min[0]
RV_real_llb[0] = RV_5min[0]
alpha=0.05
z_alpha = norm.ppf(1-alpha/2)
for i in range(N):
RV_real_pred[i+1] = c + beta*RV_5min[i]
RV_real_uub[i+1] = RV_real_pred[i+1] + z_alpha*np.sqrt(np.var(RV_5min))*np.sqrt(1+beta**2)
RV_real_llb[i+1] = RV_real_pred[i+1] - z_alpha*np.sqrt(np.var(RV_5min))*np.sqrt(1+beta**2)
plt.plot(RV_5min.index, np.maximum(0, RV_real_pred), 'k:')
plt.plot(RV_5min.index, np.maximum(0, RV_real_uub), 'r--', RV_5min.index, np.maximum(0, RV_real_llb), 'r--')
plt.xlabel('Time')
plt.ylabel('RV')
plt.legend(['Predicted', 'Upper Bound', 'Lower Bound'])
plt.show()
```
这样修改后,x轴和y轴的长度应该一致,可以正确地绘制图形。
阅读全文