2
$\begingroup$

Consider the following test data:

reg1 = RegionPlot[(x^2 + y^2) <= 1 || 2 <= (x^3 + y^2) <= 5, {x, -3, 
    3}, {y, -5, 5}];
reg2 = RegionPlot[(x^2 + y^3) <= 1 || 2 <= (x^3 + y^2) <= 5, {x, -3, 
    3}, {y, -5, 5}];
sens1 = Cases[Normal@reg1, Line[x_] :> x, Infinity];
sens2 = Cases[Normal@reg2, Line[x_] :> x, Infinity][[1]];

sens1 includes two tables with data, while sens2 is just one table.

I want to plot the data from sens1 in one color and the data from sens2 in another. For this specific example, I may simply use

ListPlot[{sens1[[1]],sens1[[2]], sens2}, Joined -> True, AspectRatio -> Automatic,PlotStyle -> {{Thick, Blue}, {Thick, Blue}, {Thick, Darker@Red}, {Thick, Gray}}]

However, what should I do if the dimensions of sens1 and sens2 are a priori unknown?

Edit

What if additionally I need to show plot legends, one legend label per all belongings of sens1 and sens2? Again, if the dimensions are known a priori, I may just plot something like

ListPlot[{sens2,sens1[[1]],sens1[[2]]}, Joined -> True, AspectRatio -> Automatic,PlotStyle -> {{Thick, Darker@Red},{Thick, Blue}, {Thick, Blue}}, PlotLegends -> Placed[Style[#, 15] & /@ {"2","1"}, {0.2, 0.15}]]

But it is not clear to me what to do if the dimensions are unknown.

$\endgroup$

3 Answers 3

4
$\begingroup$
reg1 = RegionPlot[(x^2 + y^2) <= 1 || 2 <= (x^3 + y^2) <= 5, {x, -3, 
    3}, {y, -5, 5}];
reg2 = RegionPlot[(x^2 + y^3) <= 1 || 2 <= (x^3 + y^2) <= 5, {x, -3, 
    3}, {y, -5, 5}];
sens1 = Cases[Normal@reg1, Line[x_] :> x, Infinity];
sens2 = Cases[Normal@reg2, Line[x_] :> x, Infinity][[1]];

ListPlot[
 Cases[{sens1, sens2}, _?MatrixQ, All],
 Joined -> True,
 PlotStyle -> {Thick, Automatic, {Red, Dashed}},
 AspectRatio -> Automatic, 
 PlotLegends -> Placed[Style[#, 15] & /@ Automatic, {0.75, 0.85}]]

enter image description here

Alternatively, using ContourPlot

ContourPlot[
 {x^2 + y^2 == 1, 2 == x^3 + y^2, x^3 + y^2 == 5},
 {x, -3, 3}, {y, -5, 5},
 PlotLegends -> Placed["Expressions", {.8, .85}]]

enter image description here

EDIT: Per comment

ListPlot[Cases[{sens1, sens2}, _?MatrixQ, All], Joined -> True,
 PlotStyle -> Flatten[{
    ConstantArray[{Thick, Blue}, If[MatrixQ[sens1], 1, Length@sens1]],
     ConstantArray[{Thick, Red, Dashed}, 
     If[MatrixQ[sens2], 1, Length@sens2]]},
   1],
 AspectRatio -> Automatic,
 PlotLegends -> Placed[
   LineLegend[{Directive[Thick, Blue], 
     Directive[Thick, Red, Dashed]},
    Style[#, 15] & /@ {"sens1", "sens2"}],
   {.75, .85}]]

enter image description here

$\endgroup$
2
  • $\begingroup$ Thanks! May I please ask you how to show the lines from sens1 with the same color independently of the dimensionality of sens1? $\endgroup$ Jun 12 at 14:24
  • $\begingroup$ And also use only one legend label for all the belongings of sens1 (please see the edit to the question). $\endgroup$ Jun 12 at 14:25
3
$\begingroup$
plt1 = ListPlot[sens1, Joined -> True, AspectRatio -> Automatic, 
  PlotStyle -> Directive[Thick, Blue], PlotLegends -> {"sens1"}]
plt2 = ListPlot[sens2, Joined -> True, AspectRatio -> Automatic, 
  PlotStyle -> Directive[Thick, Red], PlotLegends -> {"sens2"}]
Show[plt1, plt2, PlotRange -> All]

Edited: add plot legend, in response to comment.

$\endgroup$
2
  • $\begingroup$ Thanks! Additional question: what if I want to show plot legends? $\endgroup$ Jun 12 at 13:04
  • $\begingroup$ @JohnTaylor See edit. $\endgroup$
    – Alan
    Jun 12 at 15:58
3
$\begingroup$
RegionPlot[{(x^2 + y^2) <= 1 || 2 <= (x^3 + y^2) <= 5, 
   (x^2 + y^3) <= 1 || 2 <= (x^3 + y^2) <= 5}, 
  {x, -3, 3}, {y, -5, 5}, 
 BoundaryStyle -> {1 -> AbsoluteThickness[3], 
   2 -> Directive[AbsoluteThickness[3], AbsoluteDashing[5]]}, 
 BaseStyle -> FaceForm[], 
 PlotLegends ->  Placed[
   LineLegend[{#, Directive[#2, Dashed]} & @@ (ColorData[97] /@ {1, 2}), 
    {"sens1", "sens2"}], {.7, .9}]]

enter image description here

Working with sens1 and sens2:

Graphics[{ColorData[97]@1, AbsoluteThickness[2], 
  Map[Line, sens1, {Depth@sens1 - 3}], ColorData[97]@2, 
  AbsoluteDashing[{6, 5}], AbsoluteThickness[2], 
  Map[Line, sens2, {Depth@sens2 - 3}]}]

enter image description here

$\endgroup$
2
  • $\begingroup$ Thanks! Is it possible however to do this if having sens1, sens2 pre-generated (i.e., not having the access to reg1, reg2)? $\endgroup$ Jun 12 at 14:49
  • 1
    $\begingroup$ @JohnTaylor, please see the update. $\endgroup$
    – kglr
    Jun 12 at 15:06

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.