betweenx填充示例

使用fill_betweenx在两条水平曲线之间着色。

betweenx填充示例

betweenx填充示例2

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. y = np.arange(0.0, 2, 0.01)
  4. x1 = np.sin(2 * np.pi * y)
  5. x2 = 1.2 * np.sin(4 * np.pi * y)
  6. fig, [ax1, ax2, ax3] = plt.subplots(3, 1, sharex=True)
  7. ax1.fill_betweenx(y, 0, x1)
  8. ax1.set_ylabel('(x1, 0)')
  9. ax2.fill_betweenx(y, x1, 1)
  10. ax2.set_ylabel('(x1, 1)')
  11. ax3.fill_betweenx(y, x1, x2)
  12. ax3.set_ylabel('(x1, x2)')
  13. ax3.set_xlabel('x')
  14. # now fill between x1 and x2 where a logical condition is met. Note
  15. # this is different than calling
  16. # fill_between(y[where], x1[where], x2[where])
  17. # because of edge effects over multiple contiguous regions.
  18. fig, [ax, ax1] = plt.subplots(2, 1, sharex=True)
  19. ax.plot(x1, y, x2, y, color='black')
  20. ax.fill_betweenx(y, x1, x2, where=x2 >= x1, facecolor='green')
  21. ax.fill_betweenx(y, x1, x2, where=x2 <= x1, facecolor='red')
  22. ax.set_title('fill between where')
  23. # Test support for masked arrays.
  24. x2 = np.ma.masked_greater(x2, 1.0)
  25. ax1.plot(x1, y, x2, y, color='black')
  26. ax1.fill_betweenx(y, x1, x2, where=x2 >= x1, facecolor='green')
  27. ax1.fill_betweenx(y, x1, x2, where=x2 <= x1, facecolor='red')
  28. ax1.set_title('Now regions with x2 > 1 are masked')
  29. # This example illustrates a problem; because of the data
  30. # gridding, there are undesired unfilled triangles at the crossover
  31. # points. A brute-force solution would be to interpolate all
  32. # arrays to a very fine grid before plotting.
  33. plt.show()

下载这个示例