Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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}}]
   }
 ]
share|improve this question
    
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}}]} –  belisarius Oct 1 '12 at 13:02
add comment

2 Answers 2

up vote 19 down vote accepted

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

share|improve this answer
    
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 1 '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 1 '12 at 19:40
    
I was just about to and I realized someone beat me to it: mathematica.stackexchange.com/questions/3618/… –  s0rce Oct 2 '12 at 20:24
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.