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:

  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.

  • 2
    $\begingroup$ Labels outside is not an option? Like: RegionPlot[{x > y, x < y}, {x, 0, 1}, {y, 0, 1}, PlotLegends -> "Expressions"] $\endgroup$ Oct 11, 2013 at 12:55
  • $\begingroup$ 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. $\endgroup$
    – rm -rf
    Oct 11, 2013 at 12:58
  • 2
    $\begingroup$ This might be useful $\endgroup$ Oct 11, 2013 at 13:22

3 Answers 3


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]];
  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

  • $\begingroup$ 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. $\endgroup$
    – timcdlucas
    Oct 14, 2013 at 15:59

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


Using Region* functionality:

r1 = ImplicitRegion[
   x > y && x > 1 && y < 2 && y > 0 && x < 2.5, {x, y}];
r2 = Rectangle[{-3, -3}, {3, 3}];

RegionPlot[{r1, r2}
 , Epilog -> {
   Text[Style["Out", 16, Brown], RegionCentroid[r2]]
   , Text[Style["In", 16, Blue], RegionCentroid[r1]]

enter image description here


Your Answer

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

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