2
$\begingroup$

I want to plot such a figure

enter image description here

I used command Plot[Piecewise[{{1,x<0},{x,x=0},{1,x>0}}],{x,-3,3}].

But it shows wrong results.

$\endgroup$
4
  • $\begingroup$ Lookup = vs ==. $\endgroup$
    – Kuba
    Commented Oct 25, 2018 at 11:38
  • $\begingroup$ @Kuba, Okay, but we can not not see the line to point (0,0) $\endgroup$
    – ABCDEMMM
    Commented Oct 25, 2018 at 11:42
  • $\begingroup$ Correct but that was solved by kglr now. Plot is not likely to exactly "hit" x==0 so that is why it looks like a straight line. $\endgroup$
    – Kuba
    Commented Oct 25, 2018 at 11:46
  • $\begingroup$ @Kuba, also, Thanks a lot, Before only do it in PPTX.... .:) $\endgroup$
    – ABCDEMMM
    Commented Oct 25, 2018 at 12:00

2 Answers 2

3
$\begingroup$

Update:

ParametricPlot[{ {x, 2}, {0, 2 Rescale[x, {-3, 3}]}}, {x, -3, 3}, 
     AxesLabel -> {Style["x", 16], Style["y", 16]}, 
     AxesStyle -> Arrowheads[0.04], 
     Ticks -> None, ExclusionsStyle -> Automatic, 
     PlotStyle -> Directive[Thick, Black], 
     AspectRatio -> Automatic, PlotRangePadding -> {{.2, .5}, {0., .5}}, 
     ImageSize -> Large ]

enter image description here

Original answer:

ParametricPlot[{{x, 0}, {x, 2}, {0, 2 Rescale[x, {-3, 3}]}}, {x, -3, 3}, 
   Axes -> False, PlotStyle -> Thick]

enter image description here

Alternatively, you can Plot two Piecewise functions with the option ExclusionsStyle:

Plot[{Piecewise[{{0, x < 0}, {2, x > 0}}], 
  Piecewise[{{2, x < 0}, {0, x > 0}}]}, {x, -3, 3}, Axes -> False, 
  ExclusionsStyle -> Automatic, PlotStyle -> Black, AspectRatio -> Automatic]

enter image description here

$\endgroup$
7
  • $\begingroup$ where is error in my code? $\endgroup$
    – ABCDEMMM
    Commented Oct 25, 2018 at 11:37
  • $\begingroup$ why we can not see line to (0,0) using my method?? $\endgroup$
    – ABCDEMMM
    Commented Oct 25, 2018 at 11:47
  • $\begingroup$ @ABCDEMMM, (1) you should use x ==0 in the middle piece as Kuba commented (2) even then, it gives a single horizontal line at y==1 with a gap at x==0 . $\endgroup$
    – kglr
    Commented Oct 25, 2018 at 11:51
  • $\begingroup$ how can we add -> x, ->y; symbols?? $\endgroup$
    – ABCDEMMM
    Commented Oct 25, 2018 at 12:26
  • $\begingroup$ @ABCDEMMM, you mean Plot[{Piecewise[{{0, x < 0}, {2, x > 0}}], Piecewise[{{2, x < 0}, {0, x > 0}}]}, {x, -3, 3}, AxesLabel -> {"x", "y"}, AxesStyle -> Arrowheads[0.04], Ticks -> None, ExclusionsStyle -> Automatic, PlotStyle -> Black, AspectRatio -> Automatic, PlotRangePadding -> {{.2, .5}, {0., .5}}]? $\endgroup$
    – kglr
    Commented Oct 25, 2018 at 12:33
2
$\begingroup$

Edit:

Graphics[{Black, AbsoluteThickness@3, Line[{{0, 0}, {0, 3}}], 
  Line[{{-3, 3}, {3, 3}}], AbsoluteThickness[1.5], 
  Arrow[{{-5, 0}, {5, 0}}], Arrow[{{0, 3}, {0, 4}}], 
  Text[Style["x", 16, FontFamily -> "Times New Roman"], {4.5, 0.5}]}]

enter image description here

Here is alternatives:

Show[Plot[UnitStep[x], {x, -3, 3}, 
  PlotStyle -> {Black, AbsoluteThickness@3}, PlotRange -> {0, 1}], 
 Plot[UnitStep[-x], {x, -3, 3}, 
  PlotStyle -> {Black, AbsoluteThickness@3}], 
 AxesStyle -> AbsoluteThickness@3, Ticks -> None, PlotRange -> {0, 1}]

Or

Show[Plot[0, {x, -3, 3}, PlotStyle -> {Black, AbsoluteThickness@3}, 
  PlotRange -> {0, 1}], 
 Plot[1, {x, -3, 3}, PlotStyle -> {Black, AbsoluteThickness@3}], 
 AxesStyle -> AbsoluteThickness@3, Ticks -> None, PlotRange -> {0, 1}]

enter image description here

Or using Graphics

Graphics[{Black, AbsoluteThickness@3, Line[{{0, 0}, {0, 2}}], 
  Line[{{-3, 0}, {3, 0}}], Line[{{-3, 2}, {3, 2}}]}]

enter image description here

$\endgroup$
1
  • $\begingroup$ you answer is also very helpful, thank you! $\endgroup$
    – ABCDEMMM
    Commented Oct 25, 2018 at 12:55

Your Answer

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

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