I have a piecewise function that I would like to plot but I was wondering if it is possible that each part of the function that is plotted when its corresponding condition is true be plotted with a different color from the other parts. That is, if I have a Piecewise function Piecewise[{{val1, cond1},{val2,cond2},{val3,cond3}}] then I want val1, val2, and val3 to be plotted with different colors so that I can differentiate each case in the plot.
|
|
||||
|
|
|
Here is one method, now more robust:
Testing:
I like Heike's method so much I have to do my own version of it.
|
|||||||||||
|
|
Here's an alternative approach than Spartacus' answer. What he did is splitting up the piecewise function into many different functions valid in only a small domain; what I am doing here is directly plotting the piecewise function as given, while the coloring is done using I'll use the same function as Spartacus,
Step by step to the resultNow let's create a First, create a copy of
Now we need to find out how many pieces there are in this function; for this we have to extract those into a list we can allpy
That's the full function body. By applying another
From this matrix-shaped list, we'd like to get the length, leaving us with
Alright! Now make some colors out of that. The default plot colors are stored in
Now we need to take these color directives and inject them into the original function (that is, the
Done!
Now it's time to plot, see the completed code below. The completed code
(The option |
||||
|
It looks like
|
|||
|





