I want to plot such a figure
I used command Plot[Piecewise[{{1,x<0},{x,x=0},{1,x>0}}],{x,-3,3}]
.
But it shows wrong results.
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 ]
Original answer:
ParametricPlot[{{x, 0}, {x, 2}, {0, 2 Rescale[x, {-3, 3}]}}, {x, -3, 3},
Axes -> False, PlotStyle -> Thick]
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]
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$
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$
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}]}]
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}]
Or using Graphics
Graphics[{Black, AbsoluteThickness@3, Line[{{0, 0}, {0, 2}}],
Line[{{-3, 0}, {3, 0}}], Line[{{-3, 2}, {3, 2}}]}]
=
vs==
. $\endgroup$Plot
is not likely to exactly "hit"x==0
so that is why it looks like a straight line. $\endgroup$