2
$\begingroup$

I have the following 3D plot

RegionPlot3D[
 x > 1 && y > 0.5 && 
  z > 0.25 && (x + y > 1.7) && (y + z > 0.9) && (x + z > 1.5) && 
  x + y + z > 2, {x, 0, 3}, {y, 0, 3}, {z, 0, 3}, PlotPoints -> 100, 
 BoundaryStyle -> None, Mesh -> None, PlotStyle -> Green, 
 PlotLegends -> "Region", 
 AxesLabel -> {HoldForm[Subscript[C, 1]], HoldForm[Subscript[C, 2]], 
   HoldForm[Subscript[C, 3]]}, AxesStyle -> Thick, BoxStyle -> Thick, 
 LabelStyle -> {FontSize -> 22, FontFamily -> "Arial", Black, Bold}]

This generates a legend label "Region" that lies outside the 3D plot. If I try to use LegendPosition->{0,0}, I get an error. How do I get the legend to sit inside the 3D plot box, in the white region and not outside the box?

$\endgroup$

3 Answers 3

5
$\begingroup$

Solution using Placed[text, coords]

It helps to read the documentation for PlotLegends, under Details, you can read

Placed[lspec,…] specify placement of legend

So you can use Placed

enter image description here

Also notice that a well-indented code is easier to read.

RegionPlot3D[
  And[
      x > 1, y > 0.5, z > 0.25,
      x + y > 1.7, 
      y + z > 0.9,
      x + z > 1.5,
      x + y + z > 2
  ]
  , {x, 0, 3}
  , {y, 0, 3}
  , {z, 0, 3}
  , PlotPoints -> 100
  , BoundaryStyle -> None
  , Mesh -> None
  , PlotStyle -> Green
  , PlotLegends -> Placed["Region",Center] (* <-- See here *)
  , AxesLabel -> {
     HoldForm[Subscript[C, 1]]
   , HoldForm[Subscript[C, 2]]
   , HoldForm[Subscript[C, 3]]
  }
  , AxesStyle -> Thick
  , BoxStyle -> Thick
  , LabelStyle -> {FontSize -> 22, FontFamily -> "Arial", Black, Bold}
]

enter image description here

Or you can be more specific with the Style and position

 PlotLegends -> Placed[Text[Style["Region",Bold,Red,25]],{0.3,0.5}]

enter image description here

Other solutions

Other answers use Show and could have used also Overlay to combine various plots. From those I like the most the use of Epilog or alternatively Prolog, depending in which order you want things stacking.

$\endgroup$
6
$\begingroup$
plot = RegionPlot3D[
  x > 1 && y > 0.5 && 
   z > 0.25 && (x + y > 1.7) && (y + z > 0.9) && (x + z > 1.5) && 
   x + y + z > 2, {x, 0, 3}, {y, 0, 3}, {z, 0, 3}, PlotPoints -> 100, 
  BoundaryStyle -> None, Mesh -> None, PlotStyle -> Green, 
  PlotLegends -> "Region", 
  AxesLabel -> {HoldForm[Subscript[C, 1]], HoldForm[Subscript[C, 2]], 
    HoldForm[Subscript[C, 3]]}, AxesStyle -> Thick, BoxStyle -> Thick,
   LabelStyle -> {FontSize -> 22, FontFamily -> "Arial", Black, 
    Bold}];
  • We note that plot[[2,1]] is the PlotLegends and we Inset it into 3D.
Show[plot[[1]], 
 Graphics3D[{Inset[Style[plot[[2, 1]], Red], {.5, .5, 2}, Automatic, 
    Scaled[{.2, .1}]]}]]

enter image description here

  • We can test another example which use several legends.
plot = RegionPlot3D[{x^2 + y^2 <= 1, x^2 + z^2 <= 1, 
    y^2 + z^2 <= 1}, {x, -2, 2}, {y, -2, 2}, {z, -2, 2}, 
   PlotLegends -> {"aaa", "bbb", "ccc"}, PlotRange -> 3];
Show[plot[[1]], 
 Graphics3D[
  Inset[plot[[2, 1]], {-2, -2, 2}, Automatic, Scaled[{.2, .2}]]]]

enter image description here

$\endgroup$
5
$\begingroup$

You may either insert a text in your plot using Epilog. For this purpose 2D coordinates from 0 to 1 are used:

RegionPlot3D[
 x > 1 && y > 0.5 && 
  z > 0.25 && (x + y > 1.7) && (y + z > 0.9) && (x + z > 1.5) && 
  x + y + z > 2, {x, 0, 3}, {y, 0, 3}, {z, 0, 3}, PlotPoints -> 100, 
 BoundaryStyle -> None, Mesh -> None, PlotStyle -> Green, 
 AxesLabel -> {HoldForm[Subscript[C, 1]], HoldForm[Subscript[C, 2]], 
   HoldForm[Subscript[C, 3]]}, AxesStyle -> Thick, BoxStyle -> Thick, 
 LabelStyle -> {FontSize -> 22, FontFamily -> "Arial", Black, Bold}, 
 Epilog -> Text[Style["asas", FontSize -> 22], {0.15, 0.5}]]

enter image description here

Or you can insert some text as an independent 3D item using Show. In this case the text is placed at an 3D position:

pl = RegionPlot3D[
  x > 1 && y > 0.5 && 
   z > 0.25 && (x + y > 1.7) && (y + z > 0.9) && (x + z > 1.5) && 
   x + y + z > 2, {x, 0, 3}, {y, 0, 3}, {z, 0, 3}, PlotPoints -> 100, 
  BoundaryStyle -> None, Mesh -> None, PlotStyle -> Green, 
  AxesLabel -> {HoldForm[Subscript[C, 1]], HoldForm[Subscript[C, 2]], 
    HoldForm[Subscript[C, 3]]}, AxesStyle -> Thick, BoxStyle -> Thick,
   LabelStyle -> {FontSize -> 22, FontFamily -> "Arial", Black, 
    Bold}]; Show[pl, 
 Graphics3D[Text[Style["asas", FontSize -> 22], {0.5, 0.5, 2}]]]

enter image description here

$\endgroup$
2
  • $\begingroup$ How is your second solution different from the one offered by @cvgmt? (+1) $\endgroup$
    – rhermans
    Jul 4 at 14:45
  • $\begingroup$ I used "Text" not "Inset" $\endgroup$ Jul 4 at 14:49

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