Matplotlib Stem Plot

1.3k Views Asked by At

Using stem, I want display (x,y) values with all the y values above a tolerance using red color and the all the rest using green. Exactly I want to display from the tolerance to maximum value using red and from the tolerance to zero using green. On the final display I want to see all the graph bellow the tolerance green, and above the tolerance red.

1

There are 1 best solutions below

5
On

This is a bit of a hack that does the job. I have created arbitrary data for x and y, and a tolerance of 2.0:

import numpy as np
import matplotlib.pyplot as plt
plt.ion()

x = np.arange(1., 10.)
y = x**.5
tolerance = 2.

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)

ax.stem(x[y<=tolerance], y[y<=tolerance], 'g', zorder=10)
if len(x[y>tolerance]) > 0:
    ax.stem(x[y>tolerance], y[y>tolerance], 'r', zorder=10)
    ax.vlines(x[y>tolerance], 0, tolerance, 'g', zorder=20)

ax.axhline(tolerance)

The ax.axhline is not required, but I added it to show the value of the tolerance. The result of this code looks like this:

output of code