-1

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.

mechanical_meat
  • 163,903
  • 24
  • 228
  • 223

1 Answers1

2

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

Peter
  • 567
  • 3
  • 10
  • i want only the values above the tolerance to be red not the whole line as in the output code. – user514080 Feb 13 '16 at 07:32
  • I have edited my answer by adding a third call to the `ax.stem` method to draw green lines over the red lines up to the threshold. This is a bit of a hack, but I hope it gives the result you are looking for! – Peter Feb 13 '16 at 18:05
  • This worked pefect if the tolerance value is lower than the Y values, if you try with a tolerance value exceed or equal the maximum value it will give error message. – user514080 Feb 14 '16 at 22:10
  • I used plot.vlines instead with the hack and worked faster – user514080 Feb 14 '16 at 22:12
  • Thanks @user514080! I've updated the answer to to include your improvements. – Peter Feb 14 '16 at 23:01