0

Suppose I have the following array:

array = np.random.rand(5,3).flatten()
array

array([0.19274605, 0.46876174, 0.26670674, 0.55353007, 0.47098491,
       0.41225302, 0.99921434, 0.43489851, 0.93929395, 0.85369836,
       0.31313385, 0.41672712, 0.762171  , 0.06592526, 0.23157575])

These are the plotted data (with pyplot.plot()):

click here

Now, I need the line to have a different color depending on the value, for example, I need that for values between 0.2 and 0.4 the line is red, orange for values between 0.4 and 0.6 and blue for values between 0.8 and 1.

How can I do it? I need a solution that does not involve separating the array itself into several subarrays according the ranges and graphing them with the different colors.

NOTE: I've seen a lot of examples of multicolored lines but along the x axis, I need it but for the y axis.

Thanks.

JVL
  • 63
  • 1
  • 7

1 Answers1

0

Don't have enough reputation to make a comment. But I think you are looking for this. The link is from the official Matplotlib documentation and is using derivative to represent the line color at different points.

Anshul Choudhary
  • 225
  • 2
  • 12
  • I've seen a lot of examples of multicolored lines but along the x axis, I need it but for the y axis. Thanks for your answer. – JVL Sep 26 '19 at 22:47
  • It could also work on y-axis. Refer to the documentation of [LineCollection](https://matplotlib.org/3.1.1/api/collections_api.html#matplotlib.collections.LineCollection) with an [example](https://stackoverflow.com/questions/35372993/python-matplotlib-multicolor-line). – Anshul Choudhary Sep 26 '19 at 22:54