Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I am plotting many regions with RegionPlot. I would like to label each region as I plot it so that I get something that looks like figure made with the following code:

Show[
  RegionPlot[x > y, {x, 0, 1}, {y, 0, 1}],
  RegionPlot[x < y,  {x, 0, 1}, {y, 0, 1}],
  Graphics[Text["B", {0.6, 0.2}]],
  Graphics[Text["A", {0.2, 0.6}]]
]

Example of labelled regions

However, as the sets of inequalities are quite complicated and there are many regions, I would like to automatically determine a reasonable place to put each label so that it is in the middle of each region.

Is there a simple way to do this? I could manually find a point within each region, but that would be a pain and wouldn't necessarily solve the problem of centering the text within the region.

share|improve this question
2  
Labels outside is not an option? Like: RegionPlot[{x > y, x < y}, {x, 0, 1}, {y, 0, 1}, PlotLegends -> "Expressions"] –  Pinguin Dirk Oct 11 '13 at 12:55
    
If you want to place it on the plot, here's a hint — find the centroid of the region (either analytically or via image processing) and place the text at the centroid. –  rm -rf Oct 11 '13 at 12:58
2  
This might be useful –  Simon Woods Oct 11 '13 at 13:22
add comment

2 Answers

I am certain there are more elegant ways but for example:

func[inq_, inside_, outside_, {x0_, x1_}, {y0_, y1_}] := Module[
  {rp1, rp2, in, out, ch1, ch2, ins, outs, cntr1, cntr2},
  rp1 = RegionPlot[inq, {x, x0, x1}, {y, y0, y1}];
  rp2 = RegionPlot[Not@inq, {x, x0, x1}, {y, y0, y1}];
  in = rp1[[1, 1]];
  out = rp2[[1, 1]];
  Needs["ComputationalGeometry`"];
  ch1 = ConvexHull[in];
  ch2 = ConvexHull[out];
  ins = in[[ch1]];
  outs = out[[ch2]];
  cntr1 = Plus @@ ins/Length[ins];
  cntr2 = Plus @@ outs/Length[outs];
  RegionPlot[{inq, Not@inq}, {x, x0, x1}, {y, y0, y1}, 
   Epilog -> {Text[inside, cntr1], Text[outside, cntr2]}]
  ]

This finds plots one inequality and its complement and places text at centroid of convex hull of the points of the graphics complex. Here is an example:

func[x > y && x > 1 && y < 2 && y > 0 && x < 2.5, "In", "Out", {-3, 
  3}, {-3, 3}]

I have not used OptionsPattern etc...just an example

There may be issues with some irregular shapes.

enter image description here

share|improve this answer
    
This is good, but I can't get it to work for multiple regions which would be nice. I'm trying Show[func[],func[]]. I'll keep on trying. –  timcdlucas Oct 14 '13 at 15:59
add comment

Supposed that the sets you are considering are convex it makes sense to just calculate the pivot by using Mean and take this point for text placement.

If you are dealing with arbitrary sets you might be interested in taking random samples from your set, then calculate an estimation of the pivot based on those random samples by simply taking their Mean. For taking random samples of your regions take a look at this thread: obtaining random element of a set given by multiple inequalities

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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