使用Span_Where

举例说明一些着色区域的辅助函数,其中逻辑掩码为真。

matplotlib.collections.BrokenBarHCollection.span_where .

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.collections as collections


t = np.arange(0.0, 2, 0.01)
s1 = np.sin(2*np.pi*t)
s2 = 1.2*np.sin(4*np.pi*t)


fig, ax = plt.subplots()
ax.set_title('using span_where')
ax.plot(t, s1, color='black')
ax.axhline(0, color='black', lw=2)

collection = collections.BrokenBarHCollection.span_where(
    t, ymin=0, ymax=1, where=s1 > 0, facecolor='green', alpha=0.5)
ax.add_collection(collection)

collection = collections.BrokenBarHCollection.span_where(
    t, ymin=-1, ymax=0, where=s1 < 0, facecolor='red', alpha=0.5)
ax.add_collection(collection)


plt.show()
using span_where

工具书类

以下函数、方法、类和模块的使用如本例所示:

出:

<function Axes.axhline at 0x7faa00db28c8>

关键词:matplotlib代码示例,codex,python plot,pyplot Gallery generated by Sphinx-Gallery