在和Alpha之间填充

fill_between()函数在最小和最大边界之间生成阴影区域,这对于说明范围很有用。 它具有非常方便的用于将填充与逻辑范围组合的参数,例如,仅在某个阈值上填充曲线。

在最基本的层面上,fill_between 可用于增强图形的视觉外观。让我们将两个财务时间图与左边的简单线图和右边的实线进行比较。

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. import matplotlib.cbook as cbook
  4. # load up some sample financial data
  5. with cbook.get_sample_data('goog.npz') as datafile:
  6. r = np.load(datafile)['price_data'].view(np.recarray)
  7. # Matplotlib prefers datetime instead of np.datetime64.
  8. date = r.date.astype('O')
  9. # create two subplots with the shared x and y axes
  10. fig, (ax1, ax2) = plt.subplots(1, 2, sharex=True, sharey=True)
  11. pricemin = r.close.min()
  12. ax1.plot(date, r.close, lw=2)
  13. ax2.fill_between(date, pricemin, r.close, facecolor='blue', alpha=0.5)
  14. for ax in ax1, ax2:
  15. ax.grid(True)
  16. ax1.set_ylabel('price')
  17. for label in ax2.get_yticklabels():
  18. label.set_visible(False)
  19. fig.suptitle('Google (GOOG) daily closing price')
  20. fig.autofmt_xdate()

在和Alpha之间填充示例

此处不需要Alpha通道,但它可以用于软化颜色以获得更具视觉吸引力的图形。在其他示例中,正如我们将在下面看到的,alpha通道在功能上非常有用,因为阴影区域可以重叠,alpha允许您查看两者。请注意,postscript格式不支持alpha(这是postscript限制,而不是matplotlib限制),因此在使用alpha时保存PNG,PDF或SVG中的数字。

我们的下一个例子计算两个随机游走者群体,它们具有不同的正态分布的均值和标准差,从中得出步骤。我们使用共享区域绘制人口平均位置的+/-一个标准偏差。 这里的alpha通道非常有用,而不仅仅是审美。

  1. Nsteps, Nwalkers = 100, 250
  2. t = np.arange(Nsteps)
  3. # an (Nsteps x Nwalkers) array of random walk steps
  4. S1 = 0.002 + 0.01*np.random.randn(Nsteps, Nwalkers)
  5. S2 = 0.004 + 0.02*np.random.randn(Nsteps, Nwalkers)
  6. # an (Nsteps x Nwalkers) array of random walker positions
  7. X1 = S1.cumsum(axis=0)
  8. X2 = S2.cumsum(axis=0)
  9. # Nsteps length arrays empirical means and standard deviations of both
  10. # populations over time
  11. mu1 = X1.mean(axis=1)
  12. sigma1 = X1.std(axis=1)
  13. mu2 = X2.mean(axis=1)
  14. sigma2 = X2.std(axis=1)
  15. # plot it!
  16. fig, ax = plt.subplots(1)
  17. ax.plot(t, mu1, lw=2, label='mean population 1', color='blue')
  18. ax.plot(t, mu2, lw=2, label='mean population 2', color='yellow')
  19. ax.fill_between(t, mu1+sigma1, mu1-sigma1, facecolor='blue', alpha=0.5)
  20. ax.fill_between(t, mu2+sigma2, mu2-sigma2, facecolor='yellow', alpha=0.5)
  21. ax.set_title(r'random walkers empirical $\mu$ and $\pm \sigma$ interval')
  22. ax.legend(loc='upper left')
  23. ax.set_xlabel('num steps')
  24. ax.set_ylabel('position')
  25. ax.grid()

在和Alpha之间填充示例2

where关键字参数非常便于突出显示图形的某些区域。其中布尔掩码的长度与x,ymin和ymax参数的长度相同,并且仅填充布尔掩码为True的区域。在下面的示例中,我们模拟单个随机游走者并计算人口位置的分析平均值和标准差。总体平均值显示为黑色虚线,并且与平均值的正/负一西格玛偏差显示为黄色填充区域。我们使用where掩码X> upper_bound来找到walker在一个sigma边界之上的区域,并将该区域遮蔽为蓝色。

  1. Nsteps = 500
  2. t = np.arange(Nsteps)
  3. mu = 0.002
  4. sigma = 0.01
  5. # the steps and position
  6. S = mu + sigma*np.random.randn(Nsteps)
  7. X = S.cumsum()
  8. # the 1 sigma upper and lower analytic population bounds
  9. lower_bound = mu*t - sigma*np.sqrt(t)
  10. upper_bound = mu*t + sigma*np.sqrt(t)
  11. fig, ax = plt.subplots(1)
  12. ax.plot(t, X, lw=2, label='walker position', color='blue')
  13. ax.plot(t, mu*t, lw=1, label='population mean', color='black', ls='--')
  14. ax.fill_between(t, lower_bound, upper_bound, facecolor='yellow', alpha=0.5,
  15. label='1 sigma range')
  16. ax.legend(loc='upper left')
  17. # here we use the where argument to only fill the region where the
  18. # walker is above the population 1 sigma boundary
  19. ax.fill_between(t, upper_bound, X, where=X > upper_bound, facecolor='blue',
  20. alpha=0.5)
  21. ax.set_xlabel('num steps')
  22. ax.set_ylabel('position')
  23. ax.grid()

在和Alpha之间填充示例3

填充区域的另一个方便用途是突出显示轴的水平或垂直跨度 - 因为matplotlib具有一些辅助函数 axhspan()axvspan() 以及示例axhspan Demo

  1. plt.show()

下载这个示例