Por que PlotRange não está funcionando proporcionalmente com ListLinePlot?

Aug 29 2020

Eu tenho este código e quando estou usando PlotRange(M.12.0.0.0) ele me dá um erro

colorBar[arg_] := Blend[{Red, Green, Blue}, Rescale[arg, {-1, 1}]];
postE = Table[{x, 0.05 + Cos[x]^2, Cos[x]}, {x, 0, 30, 0.05}];
ListLinePlot[postE[[All, 1 ;; 2]], PlotRange -> {-1, 1}, 
 ColorFunction -> 
  Function[{x, y}, 
   colorBar[postE[[Position[postE[[All, 1]], x][[1, 1]], 3]]]], 
 ColorFunctionScaling -> False];  



Part::partw: Part 1 of {} does not exist.
Part::pkspec1: The expression {}[[1,1]] cannot be used as a part specification.
Part::pkspec1: The expression {}[[1,1]] cannot be used as a part specification.
Part::pkspec1: The expression {}[[1,1]] cannot be used as a part specification.
General::stop: Further output of Part::pkspec1 will be suppressed during this calculation.  

Embora eu tenha obtido o resultado corretamente, por que esse erro está aparecendo? Se eu removê- PlotRangelo funciona bem !!

Respostas

3 BobHanlon Aug 29 2020 at 06:04

Ao desenhar a linha, existem valores de xque não são iguais a nenhum dos valores de x em postE. Consequentemente, use em Nearest[postE[[All, 1]], x][[1]]vez de xnoColorFunction

Clear["Global`*"]

colorBar[arg_] := Blend[{Red, Green, Blue}, Rescale[arg, {-1, 1}]];
postE = Table[{x, 0.05 + Cos[x]^2, Cos[x]}, {x, 0, 30, 0.05}];

ListLinePlot[Most /@ postE,
 PlotRange -> {-1, 1},
 ColorFunction :> 
  Function[{x, y}, 
   colorBar[postE[[Position[postE[[All, 1]], 
        Nearest[postE[[All, 1]], x][[1]]][[1, 1]], 3]]]],
 ColorFunctionScaling -> False]