1
$\begingroup$

I am trying to add a title with a background color to my plot. Currently, using Framed inside PlotLabel only creates a box around the text itself.

I want the background box of the title to:

  1. Span the full width of the plot frame (match the width of the top axis).
  2. Sit perfectly on top of the frame without gaps (flush with the top axis).

Is there a way to achieve this using PlotLabel, or should I use Epilog with Scaled coordinates?

Thanks!

RegionPlot[y^3 < x^2 + x, {x, -2, 2}, {y, -2, 2}, 
 PlotLabel -> 
  Style[Framed[y^3 < x^2 + x], 16, Blue, Background -> Lighter[Yellow]]]

enter image description here

$\endgroup$
1
  • $\begingroup$ Leading and trailing spaces? $\endgroup$ Commented Nov 18 at 3:51

1 Answer 1

2
$\begingroup$

I do no think there is an easy solution so I replaced default frame with AxisObject-s.

(Maybe there is some repository function that can do that but I haven't searched for one.)

Height h of the label should be adjusted manually.

pl = RegionPlot[y^3 < x^2 + x, {x, -2, 2}, {y, -2, 2}, Frame -> False];

pad = OptionValue[AbsoluteOptions[pl], PlotRangePadding];
pr = OptionValue[AbsoluteOptions[pl], PlotRange];
ax = pr + pad {{-1, 1}, {-1, 1}};

label = Style[y^3 < x^2 + x, 16, Blue];
h = 0.4;
bcolor = Lighter[Yellow];

as = DefaultBaseStyle -> {Antialiasing -> False};

pl /. Graphics[g1_, g2___] :> 
   Graphics[
    Append[g1, {AxisObject[{"Horizontal", ax[[2, 1]], ax[[1]]}, as], 
      AxisObject[{"Horizontal", ax[[2, 2]], ax[[1]]}, 
       TickDirection -> "Inward", TickLabels -> None, as], 
      AxisObject[{"Vertical", ax[[1, 1]], ax[[2]]}, as], 
      AxisObject[{"Vertical", ax[[1, 2]], ax[[2]]}, 
       TickDirection -> "Inward", TickLabels -> None, 
       as], {FaceForm[bcolor], EdgeForm[Directive[Black, Thin]], 
       Rectangle[{ax[[1, 1]], ax[[2, 2]]}, {ax[[1, 2]], 
         ax[[2, 2]] + h}]}, 
      Inset[label, {Mean@ax[[1]], ax[[2, 2]] + h/2}]}], 
    g2] /. (PlotRangePadding -> _) -> (PlotRangePadding -> 
    pad + {{0, 0}, {0, h}} + 0.1)

enter image description here

$\endgroup$
2
  • $\begingroup$ as = DefaultBaseStyle -> {Antialiasing -> False}; can be omitted if you like the default "blurry" style of AxisObject. mathematica.stackexchange.com/q/317066/53172 $\endgroup$ Commented Nov 22 at 13:29
  • $\begingroup$ Thanks a lot, it works. Cheers $\endgroup$ Commented Nov 24 at 4:28

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.