3
$\begingroup$

From the documentation, Labeled accepts the positions 'Top, Bottom, Right, Left'. But this is too vague in some situations. So my question is whether it's possible to give the explicit position of labeling object respect to the labeled one?

A minimal example will be

Labeled[DensityPlot[x^2 + y^2, {x, -1, 1}, {y, -1, 1}, 
  PlotLegends -> BarLegend[{Automatic, {0, 2}}]], {"a", "b"}, {Bottom,
   Top}]

enter image description here

Let's say I want to put the label "b" at 3/4 to the left. I hope I could assign the position and alignment like {Top,Scaled[.75]}. What should I do?(Please do not use FrameLabel because the labeled object doesn't have to be a 'plot'. I just use it as an example here.) A very dumb workaround that I can think of is to add extra space before but that's just too ugly.

What I want to achieve:

enter image description here

$\endgroup$
2
$\begingroup$

You can have finer control on positioning if you use Legended in stead of Labeled:

Legended[DensityPlot[x^2 + y^2, {x, -1, 1}, {y, -1, 1}, 
   PlotLegends -> BarLegend[{Automatic, {0, 2}}]], 
 {Placed["a", Bottom], Placed["b", {{.85, 1.02}}]}]

enter image description here

$\endgroup$
0
$\begingroup$

You could use Show to combined graphics, including text.

Positions can be specified with scaled coordinates (relative to the plot):

Show[
 DensityPlot[x^2 + y^2, {x, -1, 1}, {y, -1, 1}, 
  PlotLegends -> BarLegend[{Automatic, {0, 2}}]],
 Graphics[{
   Text[Style["b", Medium], Scaled[{.75, 1.05}]],
   Text[Style["a", Medium], Scaled[{.5, -.08}]]
   }], ImagePadding -> 30, PlotRangeClipping -> False, ImageSize -> 300]

enter image description here

Or with absolute coordinates:

Show[
 DensityPlot[x^2 + y^2, {x, -1, 1}, {y, -1, 1}, 
  PlotLegends -> BarLegend[{Automatic, {0, 2}}]],
 Graphics[{
   Text[Style["b", Medium], {0.5, 1.15}],
   Text[Style["a", Medium], {0, -1.2}]
   }], ImagePadding -> 30, PlotRangeClipping -> False, ImageSize -> 300]

ImagePadding may need to be adjusted to make room for the labels.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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