Simplest
I don't know why Plot[]
can parse the array output of a function into individual lines for each scalar component but not style them independently. The best I can come up with is to reprocess the lines:
Plot[f[x], {x, 0, 2 Pi}] // ListLinePlot[ Cases[#, Line[p_] :> p, Infinity], PlotLegends -> {1, 2}, Options@# ] &

More robust
If you're going to add Exclusions
, then the graph of a function will consist of separate lines, and the above will give each piece its own color. It's a bit more complicated to unify the components. The solution below relies on the current structure of the output of exclusion lines and the individual pieces of the graph of a function. There's no guarantee that WRI won't change this in the future (and the structure has changed in the past).
myLine // ClearAll; asymptote // ClearAll; myLine[p_] /; With[{width = MinMax[p[[All, 1]]]}, Abs[Subtract @@ width] < 0.0001 Max[Abs[width] + Sqrt@$MinMachineNumber]] := asymptote[p]; myLine /: HoldPattern@{a___, myLine[p_], myLine[q_], b___} /; p[[-1, 1]] < q[[1, 1]] := {a, myLine[Join[p, {{(p[[-1, 1]] + q[[1, 1]])/2, Indeterminate}}, q]], b}; Plot[{Tan[x], f[x]}, {x, 0, 2 Pi}, ExclusionsStyle -> {Dashed, Gray}] // With[{plt = Normal@# /. Line -> myLine}, Show[ Graphics[{ Cases[plt, {___, _asymptote, ___}, Infinity] /. asymptote -> Line Cases[plt, {___, _Point, ___}, Infinity] }], ListLinePlot[Cases[plt, HoldPattern[myLine[p_]] :> p, Infinity], PlotLegends -> {1, 2, 3}], Options@# ] ] &

This looks identical to the output of the following:
Plot[{Tan[x], {Cos[x], Sin[x]}}, {x, 0, 2 Pi}, ExclusionsStyle -> {Dashed, Gray}, PlotLegends -> {1, 2, 3}]
One internal difference is the coordinate plot highlighting is missing in for the golden #2 plot in the straight Plot[]
code immediately above, but it is added by ListLinePlot[]
in the fix.
If you want more, complicated features like filling and mesh and so forth, you'll have to figure out how to dive in and adjust them. It's too bad Plot[]
doesn't do this for you.
f01[x_?NumericQ] := {Cos[x], Sin[x]}; vectorLength = 2; Plot[Evaluate @ Table[Indexed[f01[x], i], {i, vectorLength}], {x, 0, 2 Pi}, PlotLegends -> {1, 2}]
?. I'm not sure. I don't fullly understandIndexed[..]
$\endgroup$Sin
andCos
curves have different numbers of points, so I think it is evaluating the function independently to compute each curve. It would be nice to avoid this.$\endgroup$Evaluate
would do nothing here. So how do I get round it?$\endgroup$decl = FunctionDeclaration[sincos, Typed["Real32" -> {"Real32", "Real32"}]@Function[x, {Sin[x], Cos[x]}]]
andFunctionCompile[decl, Function[Typed[arg, "Real32"], sincos[arg]]]
and I couldn't get that to work either.$\endgroup$