【问题标题】:Bollinger Band shading not shading in colour布林带阴影没有颜色阴影
【发布时间】:2020-07-02 06:23:35
【问题描述】:

我试图在代表“上带”和“下带”的上红线和下橙线之间加阴影

我得到了这张图片:

我试过的代码如下:

    fig = plt.figure()
    plt.plot(dataset[['Adj Close', 'ma21', 'upper_band', 'lower_band']])
    plt.fill_between(dataset.index, dataset['upper_band'], dataset['lower_band'], color='grey',  alpha = 0.3)
    plt.legend(loc='best')
    plt.savefig('bollinger.png')
    plt.show()

我相信它与dataset.index 有关,但我不确定如何正确获取 x 轴。

谢谢。

【问题讨论】:

  • 问题似乎是您使用plt.fill_between 的索引,但没有plt.plot 的索引。尝试使用plt.fill_between(range(len(dataset)), dataset['upper_band'], dataset['lower_band'], ...) 使用相同的 x 轴制作两个图。

标签: python matplotlib stock


【解决方案1】:

您可以使用找到的文档 here 并使用 matplotlib.pyplot.fill_between 方法

这是一个使用它的例子:

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(0.0, 2, 0.01)
y1 = np.sin(2 * np.pi * x)
y2 = 0.8 * np.sin(4 * np.pi * x)

fig, (ax1, ax2, ax3) = plt.subplots(3, 1, sharex=True, figsize=(6, 6))

ax3.fill_between(x, y1, y2)
ax3.set_title('fill between y1 and y2')
ax3.set_xlabel('x')
fig.tight_layout()

【讨论】:

  • 优秀。你试过使用 fill_between 命令吗?
猜你喜欢
  • 2012-09-18
  • 1970-01-01
  • 2011-12-19
  • 1970-01-01
  • 2015-07-14
  • 2014-02-11
  • 2018-12-02
  • 2018-10-31
相关资源
最近更新 更多