14

I'm trying to replicate a graph from my math book for my notes as well as to learn Mathematica. I was hoping someone would be able to help me figure out how to add the open points to the graph, and possibly add a slight curve to the lines as shown in the real graph. My lines are straight.

The Graph from the book:

What I've been able to come up with:

   g[x_] := \[Piecewise] {
   {x + 1, x < 1},
   {-x + 3, x < 2},
   {(x + 2) - 2, x < 3},
   {-x + 6, x < 4}
  }

Plot[g[x], {x, 0, 5}, 
 PlotStyle -> {Thick}, PlotRange -> {0, 5},
 Ticks -> {{0, 1, 2, 3, 4, 5}, {0, 1, 2, 3, 4, 5}, Automatic},
 GridLines -> {{0, 1, 2, 3, 4, 5}, {0, 1, 2, 3, 4, 5}},
 GridLinesStyle -> Directive[LightGray, Dashed],
 Epilog -> {Red, Dashed, PointSize[Large],
   Point[{{0, 1}, {1, 3}, {2, 1}, {4, 2}}]
   }
 ]
  • Try Epilog -> {Red, Dashed, PointSize[Large], Point[{{0, 1}, {1, 3}, {2, 1}, {4, 2}}], White, PointSize[Medium], Point[{{0, 1}, {1, 3}, {2, 1}, {4, 2}}]} – Dr. belisarius Oct 01 '12 at 13:02

2 Answers2

20

Looks good. You just need to add quadratics and open points.

r = 0.05;
g[x_] := Piecewise[{
  {x + 1, x < 1},
  {-x + 3, 1 <= x < 2},
  {(x - 2)^2 + 2, 2 <= x < 3},
  {3 - (x - 3)^2, 3 <= x < 4}
}];
Plot[g[x], {x, 0, 4}, AspectRatio -> Automatic,
 PlotStyle -> Directive[Thick, Black],
 PlotRange -> {{-0.1, 5}, {-0.1, 4}},
 Epilog -> {
   {Disk[{0, 1}, r], Disk[{2, 1}, r], Disk[{4, 2}, r], 
    Disk[{1, 3}, r]},
   {White, EdgeForm[Black], Disk[{1, 2}, r], Disk[{2, 2}, r], 
    Disk[{3, 3}, r]},
   Text[Style["y=f(x)", FontSize -> 18, Italic, 
    Background -> White], {4,3.5}]
   }, GridLines -> Automatic, 
 GridLinesStyle -> Directive[Gray, Dashed]]

enter image description here

Rojo
  • 42,601
  • 7
  • 96
  • 188
Mark McClure
  • 32,469
  • 3
  • 103
  • 161
  • Tiny issue (which I've also run into) is that your horizontal grid lines on the y-axis run into the tick labels. – s0rce Oct 01 '12 at 17:14
  • @s0rce Well, that's really a completely different issue. Perhaps, you should post a question to the main site. – Mark McClure Oct 01 '12 at 19:40
  • I was just about to and I realized someone beat me to it: http://mathematica.stackexchange.com/questions/3618/how-do-i-get-rid-of-the-overlap-of-gridlines-and-tick-mark-labels – s0rce Oct 02 '12 at 20:24
6

If you want to use specific markers in your plots, you can specify your markers to use them with PlotMarkers:

emptyCircle[size_, thickness_] := 
     Graphics[{{EdgeForm[Thickness[thickness]], Black, Circle[]}, {White,
     Disk[]}}, ImageSize -> size];
filledCircle[size_] := Graphics[{Black, Disk[]}, ImageSize -> size];

ListPlot[{RandomReal[{-1, 1}, {20, 2}], RandomReal[{-1, 1}, {20, 2}]},
    PlotMarkers -> {emptyCircle[10, .01], filledCircle[10]}, 
    PlotRange -> {{-1.2, 1.2}, {-1.2, 1.2}}]

enter image description here

VLC
  • 9,818
  • 1
  • 31
  • 60