2

I want to plot the following cumulative distribution function

enter image description here

and to do this I thought I could use np.piecewise as follows

x = np.linspace(3, 9, 100)
np.piecewise(x, [x < 3, 3 <= x <= 9, x > 9], [0, float((x - 3)) / (9 - 3), 1])

but this gives the following error

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

How can I do this?

MackM
  • 2,717
  • 5
  • 31
  • 42
user5368737
  • 763
  • 3
  • 10
  • 19

1 Answers1

1

np.piecewise is a capricious beast.

Use :

x = np.linspace(3, 9, 100)
cond = [x < 3, (3 <= x) & (x <= 9), x > 9];
func = [0, lambda x : (x - 3) / (9 - 3), 1];
np.piecewise(x, cond, func)

Explanations here.

Ortomala Lokni
  • 48,718
  • 17
  • 164
  • 211