เหตุใด PlotRange จึงไม่ทำงานร่วมกับ ListLinePlot

Aug 29 2020

ฉันมีรหัสนี้และเมื่อฉันใช้PlotRange(M.12.0.0.0) มันทำให้ฉันมีข้อผิดพลาด

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.  

แม้ว่าฉันจะได้ผลลัพธ์ที่ถูกต้อง แต่เหตุใดจึงเกิดข้อผิดพลาดขึ้น ถ้าเอาออกPlotRangeก็ใช้ได้ !!

คำตอบ

3 BobHanlon Aug 29 2020 at 06:04

ในการวาดเส้นที่มีค่าของxที่ไม่ได้เท่ากับค่าใด ๆ x postEใน ดังนั้นให้ใช้Nearest[postE[[All, 1]], x][[1]]แทนที่จะใช้xในไฟล์ColorFunction

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]