I would like to apply a custom PlotStyle
(colors and dashing) to a plot of a bunch of expressions, which are in a nested list. I can do this fine when using Plot
, but with ParametricPlot
I have to manually flatten the list of expressions. Why do they behave differently?
I'm using Mathematica 10.2.
Here is an example: I want to plot two expressions, each under two substitution rules, and use the default colors for them. I also want to show a gray dashed 45° line. (In this example, ParametricPlot
doesn't actually improve on Plot
, but in my real application it does.)
expnList = {-.2 + x^n, 1 - x*n};
substList = {{n -> .1}, {n -> .2}};
myPlotStyle = PadRight[{Directive[Dashed, Gray]}, 5, Automatic];
Here's a Plot
that works just fine:
Plot[Evaluate @ Prepend[x][expnList /. substList], {x, 0, 1}, PlotStyle -> myPlotStyle]
(I chose to use Evaluate@
rather than Evaluated->True
as suggested here, since for some reason that fails with ParametricPlot
.)
But when I try to make a ParametricPlot
just like it, Mathematica thinks it's plotting just 2 lines plus the pre-pended 45° one, and so only uses 2+1 colors:
expnPairList = Map[{x, #} &, expnList]
ParametricPlot[
Evaluate @ Prepend[{x, x}][expnPairList /. substList], {x, 0, 1},
PlotStyle -> myPlotStyle]
I can fix this by flattening my expnPairList
into a list of length-2 lists using Partition
and Flatten
.
ParametricPlot[
Evaluate @
Prepend[{x, x}][
Partition[#, 2] &@Flatten@(expnPairList /. substList)],
{x, 0, 1}, PlotStyle -> myPlotStyle]
What I don't understand is: Why is this flattening necessary? I thought that using Evaluate
would allow Mathematica to figure out how many lines it will need to plot, so it could assign styles appropriately.
And, is there a better way to make the colors behave correctly, without having to flatten things in this way?
Evaluate
but the ways that the two functions handle lists of lists. To see this, executePrepend[x][expnList /. substList]
outsidePlot
and copy the result intoPlot
(withoutEvaluate
), and do the similar thing forParametricPlot
. You will see that the behavior is unchanged from what you describe in your question. As I understand it,Plot
is a special case ofParametricPlot
. Perhaps it flattens lists of functions before passing them toParametricPlot
. It is impossible to know for certain without knowing the internal behavior of these two functions. $\endgroup$Plot
orParametricPlot
, so I'm not sure that you can consider one behaviour correct or the other incorrect. They are different functions and they handle unsupported inputs differently. IMO flattening the lists is the correct approach. $\endgroup$