Pourquoi PlotRange ne fonctionne pas correctement avec ListLinePlot?
Aug 29 2020
J'ai ce code et quand j'utilise PlotRange(M.12.0.0.0) cela me donne une erreur
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.
Bien que j'obtienne le résultat correctement, mais pourquoi cette erreur apparaît-elle? Si je supprime PlotRangeça marche bien !!
Réponses
3 BobHanlon Aug 29 2020 at 06:04
Lors du tracé de la ligne, il y a des valeurs de xqui ne sont égales à aucune des valeurs x dans postE. Par conséquent, utilisez Nearest[postE[[All, 1]], x][[1]]plutôt que xdans leColorFunction
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]