使用span_where

说明一些用于逻辑掩码为True的阴影区域的辅助函数。

请参考 matplotlib.collections.BrokenBarHCollection.span_where()

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import matplotlib.collections as collections
  4. t = np.arange(0.0, 2, 0.01)
  5. s1 = np.sin(2*np.pi*t)
  6. s2 = 1.2*np.sin(4*np.pi*t)
  7. fig, ax = plt.subplots()
  8. ax.set_title('using span_where')
  9. ax.plot(t, s1, color='black')
  10. ax.axhline(0, color='black', lw=2)
  11. collection = collections.BrokenBarHCollection.span_where(
  12. t, ymin=0, ymax=1, where=s1 > 0, facecolor='green', alpha=0.5)
  13. ax.add_collection(collection)
  14. collection = collections.BrokenBarHCollection.span_where(
  15. t, ymin=-1, ymax=0, where=s1 < 0, facecolor='red', alpha=0.5)
  16. ax.add_collection(collection)
  17. plt.show()

使用span_where示例

参考

本例中显示了下列函数、方法、类和模块的使用:

  1. import matplotlib
  2. matplotlib.collections.BrokenBarHCollection
  3. matplotlib.collections.BrokenBarHCollection.span_where
  4. matplotlib.axes.Axes.add_collection
  5. matplotlib.axes.Axes.axhline

下载这个示例