2
$\begingroup$

How can I pass plot options when I use the PlotPoints command? I thought that I could find an answer online but I was unable to do so.

The task at hand is to plot the function with thin green and the points with thick black. I naively used the following code

stpfnctn[r_] := Piecewise[{{5 - Abs[2]*r, r > 0}, {5 + Abs[2]*r, r < 0}}]

Plot[stpfnctn[r], {r, -3, 3}, 
  PlotRange -> {{-3.5, 3.5}, {-1.1, 5}}, 
  Exclusions -> {r == 0}, 
  Mesh -> All, 
  MaxRecursion -> 0, 
  PlotPoints -> {25, PlotStyle -> {Thick, Black}}, 
  PlotStyle -> {Thin, Green}, 
  BaseStyle -> {13, FontFamily -> "Times New Roman"}, 
  Ticks -> {None, Automatic}, 
  AxesStyle -> Arrowheads[0.04]]

which does not work. The whole plot is preented in thin green colour. Any suggestions?

$\endgroup$

2 Answers 2

3
$\begingroup$

We can also change the Mesh settings to control the mesh color.

positiveColor = 
  Thread[{Subdivide[0, 4, 10], Directive[Red, PointSize[Large]]}];
negativeColor = 
  Thread[{Subdivide[-4, 0, 20], Directive[Blue, PointSize[Medium]]}];
meshcolor = Catenate[{positiveColor, negativeColor}];

stpfnctn[r_] := 
 Piecewise[{{5 - Abs[2]*r, r > 0}, {5 + Abs[2]*r, r < 0}}]
Plot[stpfnctn[r], {r, -3, 3}, PlotRange -> {{-3.5, 3.5}, {-1.1, 5}}, 
 Exclusions -> {r == 0}, Mesh -> {meshcolor}, MaxRecursion -> 0, 
 PlotPoints -> {25, PlotStyle -> {Thick, Black}}, 
 PlotStyle -> {Thin, Green}, 
 BaseStyle -> {13, FontFamily -> "Times New Roman"}, 
 Ticks -> {None, Automatic}, AxesStyle -> Arrowheads[0.04]]

enter image description here

$\endgroup$
2
  • $\begingroup$ That's very cool. Much closer to what I was looking for. Thanks! $\endgroup$
    – user49048
    Oct 3, 2020 at 9:54
  • $\begingroup$ stpfnctn[r_] := Piecewise[{{5 - Abs[2]*r, r > 0}, {5 + Abs[2]*r, r < 0}}] Plot[stpfnctn[r], {r, -3, 3}, PlotRange -> {{-3.5, 3.5}, {-1.1, 5}}, Exclusions -> {r == 0}, MeshFunctions -> {#1 &, #1 &}, Mesh -> {Subdivide[0, 3, 10], Subdivide[-3, 0, 10]}, MeshStyle -> {Directive[PointSize[Medium], Red], Blue}, MaxRecursion -> 0, PlotPoints -> {25, PlotStyle -> {Thick, Black}}, PlotStyle -> {Thin, Green}, BaseStyle -> {13, FontFamily -> "Times New Roman"}, Ticks -> {Automatic, Automatic}, AxesStyle -> Arrowheads[0.04]] $\endgroup$
    – cvgmt
    Oct 28, 2020 at 8:25
6
$\begingroup$

Use MeshStyle

Plot[stpfnctn[r], {r, -3, 3},
 PlotRange -> {{-3.5, 3.5}, {-1.1, 5}},
 Exclusions -> {r == 0},
 MaxRecursion -> 0,
 PlotPoints -> 25,
 Mesh -> All,
 MeshStyle -> Black,
 PlotStyle -> {Thin, Green},
 BaseStyle -> {13, FontFamily -> "Times New Roman"},
 Ticks -> {None, Automatic}, AxesStyle -> Arrowheads[0.04]]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.