I would like to plot a simple interval on the number line in Mathematica. How do I do this?
The previous ugly solution has helped me to develop the InequalityPlot function to solve and plotting inequalities in two variables.
InequalityPlot[ineq_, {x_Symbol, xmin_, xmax_},{y_Symbol, ymin_, ymax_},
opts : OptionsPattern[Join[Options[ContourPlot],
Options[RegionPlot], {CurvesColor -> RGBColor[1, .4, .2]}]]] :=
Module[{le = LogicalExpand[ineq], opencurves, closedcurves, curves},
opencurves = Cases[Flatten[{le /. And | Or -> List}],
lexp_ < rexp_ | lexp_ > rexp_ | lexp_ < rexp_ | lexpr_ > rexp_ :>
{lexp == rexp, Dashing[Medium]}];
closedcurves = Cases[Flatten[{le /. And | Or -> List}],
lexp_ <= rexp_ | lexp_ >= rexp_ | lexp_ <= rexp_ | lexp_ >= rexp_ :>
{lexp == rexp, Dashing[None]}];
curves = Join[opencurves, closedcurves];
Show[ RegionPlot[ineq, {x, xmin, xmax}, {y, ymin, ymax},
BoundaryStyle -> None,
Evaluate[Sequence @@ FilterRules[{opts}, Options[RegionPlot]]]],
ContourPlot[First[#] // Evaluate, {x, xmin, xmax}, {y, ymin, ymax},
ContourStyle -> Directive[OptionValue[CurvesColor], Last[#]],
Evaluate[Sequence @@ FilterRules[{opts},
Options[ContourPlot]]]] & /@ curves ]
]
Here are two examples:
InequalityPlot[0.5 <= x^2 + y^2 < 1, {x, -1, 1}, {y, -1, 1}]
InequalityPlot[x^2 + y^2 < 0.5 && x + y <= 0.5,{x, -1, 1}, {y, -1, 1}]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With