首页 > 解决方案 > 布林带阴影没有颜色阴影

问题描述

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

我得到了这张图片:

在此处输入图像描述

我试过的代码如下:

    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 轴。

谢谢。

标签: pythonmatplotlibstock

解决方案


您可以使用此处找到的文档并使用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()

推荐阅读