5
$\begingroup$

I have two lists below and each element in rangeX with the corresponding element in rangeY form a region/regions (or a line/lines in some case).

rangeX = {(x >= 3 || x <= -4) && -5 <= x <= 5, -1 <= x <= 0, -5 <=
    x <= 0};
rangeY = {1 <= y <= 5, y == 1, 1 <= y <= 5};

For example, the first element of each list (x >= 3 || x <= -4) && -5 <= x <= 5 and 1 <= y <= 5 would form a region as follows:
The marking is just to illustrate (it could be cross like that or colors or anything else).
The plot range is limited in a rectangle -5 < x < 5 and 0 < y < 5 as in the image.

This is just my draft code to generate the region.

Plot[6, {x, -5, 5}, PlotRange -> {{-5, 5}, {0, 5}},
 GridLines -> {Range[-5, 5, 0.5], Range[-5, 5, 0.5] },
 Ticks -> {Range[-5, 5, 0.5], Range[-5, 5, 0.5]},
 TicksStyle -> Directive[Red, 10, Bold]]

Enter image description here

How can I fill the regions like this?
I would like to do something like Manipulate with button1, button2, button3 for regions formed by first, second, third elements of each list and button4 for showing all three in one plot.
I'm still stuck at how to fill the regions from the ranges of x, y.

$\endgroup$

3 Answers 3

6
$\begingroup$
impregs = ImplicitRegion[#, {{x, -5, 5}, {y, 0, 6}}] & /@ Thread[{rangeX, rangeY}];

plots = MapThread[
   RegionPlot[#, PlotStyle -> None, BaseStyle -> Thin, 
     MeshStyle -> #2, MeshFunctions -> #3, Mesh -> #4, BoundaryStyle -> #5] &, 
   {impregs, {Red, Green, Blue}, {# + 2 #2 &, #2 &, # - 2 #2 &}, {70, 50, 50}, {None, 
     Directive[CapForm["Round"], AbsoluteThickness[5], Opacity[1],  Green], None}}];

Show[plots, 
 GridLines -> {Range[-5, 5, 0.5], Range[-5, 5, 0.5]}, 
 PlotRange -> {{-5, 5}, {0, 5}}, 
 Ticks -> {Range[-5, 5, 0.5], Range[-5, 5, 0.5]}]

enter image description here

Manipulate[Show[If[indices === {}, Graphics[{}], plots[[indices]]], 
  GridLines -> {Range[-5, 5, 0.5], Range[-5, 5, 0.5]}, 
  AspectRatio -> 1, PlotRange -> {{-5, 5}, {0, 5}}, 
  Ticks -> {Range[-5, 5, 0.5], Range[-5, 5, 0.5]}], 
 { {indices, {1}, "regions"}, 
  Thread[Range[3] -> 
   (Style[("region "<>ToString[#]), {Red, Green, Blue}[[#]], 16, Bold]& /@ Range[3])],
  TogglerBar, Background -> GrayLevel[.6]}]

enter image description here

Show[MapThread[RegionPlot[#, PlotStyle -> #2, BaseStyle -> Thin, BoundaryStyle -> #3, 
    PlotLegends -> SwatchLegend[{#[[1]]}, LegendMarkerSize -> {40, 40}]] &,
   {impregs, 
    {Directive[Red, HatchFilling[]], Directive[Green, HatchFilling[0]], 
       Directive[Blue, HatchFilling[-Pi/4]]}, 
    {None,  Directive[CapForm["Round"], AbsoluteThickness[5], 
       Opacity[1], Green], None}}], 
  GridLines -> {Range[-5, 5, 0.5], Range[-5, 5, 0.5]}, 
  PlotRange -> {{-5, 5}, {0, 5}}, 
  Ticks -> {Range[-5, 5, 0.5], Range[-5, 5, 0.5]}] 

enter image description here

$\endgroup$
1
  • $\begingroup$ very nice images! $\endgroup$
    – hana
    Mar 29, 2022 at 20:11
4
$\begingroup$

You can get started with something like this:

RegionPlot[
  ImplicitRegion[(x >= 3 || x <= -4) && -5 <= x <= 5 && 1 <= y <= 5, {x, y}]]
$\endgroup$
3
  • $\begingroup$ You can get the combinations of your conditions with something like this conditions = Flatten@Outer[And, rangeX, rangeY]. You can follow that up by mapping ImplicitRegion over the conditions and finally RegionPlot-ting those. $\endgroup$
    – lericr
    Mar 29, 2022 at 18:02
  • $\begingroup$ Having said all of this, Regions can be a bit finicky (and slow). So, depending on what level of accuracy you want and what kind of presentation you want, there may be better approaches. $\endgroup$
    – lericr
    Mar 29, 2022 at 18:04
  • $\begingroup$ Yeah, knowing this function is helpful. $\endgroup$
    – hana
    Mar 29, 2022 at 20:12
3
$\begingroup$

Edit: more faithful image

lines[xx_, yy_] := Graphics[
   {xx, Table[
     InfiniteLine[{0, i}, AngleVector[yy]], {i, -2, 2, .05}]}, 
   PlotRange -> 1/2];
RegionPlot[
 ImplicitRegion[rangeX[[1]], {x, y}],
 PlotRange -> {{-5, 5}, {0, 5}},
 GridLines -> {Range[-5, 5, 0.5], Range[-5, 5, 0.5]},
 PlotStyle -> {Texture[lines[Black, -\[Pi]/4]]},
 BoundaryStyle -> None,
 Axes -> True,
 AxesStyle -> Thick,
 Ticks -> {Range[-5, 5, 0.5], Range[-5, 5, 0.5]},
 TicksStyle -> Directive[Red, 10, Bold],
 Frame -> False,
 ImageSize -> Large]

pp



lines[xx_, yy_] := Graphics[
   {xx, Table[
     InfiniteLine[{0, i}, AngleVector[yy]], {i, -2, 2, .05}]}, 
   PlotRange -> 1/2];
RegionPlot[
 ImplicitRegion[rangeX[[1]], {x, y}],
 PlotRange -> {{-5, 5}, {0, 5}},
 GridLines -> {Range[-5, 5, 0.5], Range[-5, 5, 0.5]},
 PlotStyle -> {Texture[lines[Black, -\[Pi]/4]]},
 BoundaryStyle -> None,
 FrameTicks -> {{None, None}, {Range[-5, 5, 0.5], None}},
 FrameTicksStyle -> {{None, None}, {Directive[Red, 10, Bold], None}}]

plot

$\endgroup$
2
  • 1
    $\begingroup$ Thanks, indeed similar but better drawing! $\endgroup$
    – hana
    Mar 29, 2022 at 20:14
  • 1
    $\begingroup$ Glad I was able to help. Check also what @kglr did. Pretty impressive!!! $\endgroup$
    – bmf
    Mar 29, 2022 at 20:14

Your Answer

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

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