Version 3.1.2
matplotlib

Table of Contents

Related Topics

Color by y-value

Use masked arrays to plot a line with different colors by y-value.

================
Color by y-value
================

Use masked arrays to plot a line with different colors by y-value.
"""
import numpy as np
import matplotlib.pyplot as plt

t = np.arange(0.0, 2.0, 0.01)
s = np.sin(2 * np.pi * t)

upper = 0.77
lower = -0.77

supper = np.ma.masked_where(s < upper, s)
slower = np.ma.masked_where(s > lower, s)
smiddle = np.ma.masked_where((s < lower) | (s > upper), s)

fig, ax = plt.subplots()
ax.plot(t, smiddle, t, slower, t, supper)
plt.show()
Traceback (most recent call last):
  File "/build/matplotlib-tq5J6U/matplotlib-3.1.2/examples/color/color_by_yvalue.py", line 1
    ================
    ^
SyntaxError: invalid syntax

References

The use of the following functions, methods, classes and modules is shown in this example:

import matplotlib
matplotlib.axes.Axes.plot
matplotlib.pyplot.plot

Keywords: matplotlib code example, codex, python plot, pyplot Gallery generated by Sphinx-Gallery