Perché PlotRange non funziona proporzionalmente con ListLinePlot?

Aug 29 2020

Ho questo codice e quando uso PlotRange(M.12.0.0.0) mi dà un errore

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.  

Anche se ottengo il risultato correttamente, ma perché viene visualizzato un errore? Se lo rimuovo PlotRangefunziona bene !!

Risposte

3 BobHanlon Aug 29 2020 at 06:04

Nel disegnare la linea, ci sono valori xche non sono uguali a nessuno dei valori x in postE. Di conseguenza, usa Nearest[postE[[All, 1]], x][[1]]piuttosto che xinColorFunction

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]