3

I want to edit this code from the wolfram Documentation Center so that, the red curve comes as dashed lines and the black comes in solid. How to do that using ColorFunction?

Colour a curve red when its absolute coordinate is above 0:

Plot[
 Sinc[x], {x, 0, 10}, 
 ColorFunction -> Function[{x, y}, If[y > 0, Red, Black]], 
 ColorFunctionScaling -> False, PlotStyle -> Thick]

enter image description here

halmir
  • 15,082
  • 37
  • 53
user444
  • 2,414
  • 1
  • 7
  • 28

2 Answers2

10
Plot[Sinc[x], {x, 0, 10}, MeshFunctions -> {Function[{x, y}, y]}, 
 Mesh -> {{0}}, MeshShading -> {Automatic, Directive[Dashed, Red]}, 
 PlotStyle -> Thick]

enter image description here

cvgmt
  • 72,231
  • 4
  • 75
  • 133
  • Isn't there any way to add colours? I tried this, but didn't get the desired result, Plot[Sinc[x], {x, 0, 10}, MeshFunctions -> {Function[{x, y}, y]}, Mesh -> {{0}}, MeshShading -> {Red, Dashed}, PlotStyle -> Thick] – user444 Mar 18 '22 at 04:58
  • @user84456 Directive[Dashed, Red] – cvgmt Mar 18 '22 at 05:06
4
Clear["Global`*"]

SolveValues[{Sinc[x] == 0, 0 <= x <= 10}, x]

(* {π, 2 π, 3 π} *)

Plot[Evaluate[
  ConditionalExpression[Sinc[x], #] & /@
   {x < Pi || 2 Pi < x < 3 Pi, Pi < x < 2 Pi || x > 3 Pi}],
 {x, 0, 10},
 PlotStyle -> {{Red, Dashed, Thick}, {Black, Thick}}]

enter image description here

Bob Hanlon
  • 157,611
  • 7
  • 77
  • 198