12
$\begingroup$

Is it possible to have the text generated by PlotLabel (or any other function) aligned to the left side of the plot instead of in the center?

$\endgroup$
16
$\begingroup$
 Labeled[Plot[Sinc[x], {x, 0, 9}], Style["plot label", "Section"], {{Top, Left}}]

enter image description here

Also

Panel[Plot[Sinc[x], {x, 0, 9}], Style["plot label", "Section"], {{Top, Left}}, 
 Appearance -> "Frameless"]

enter image description here

$\endgroup$
  • $\begingroup$ This is exactly what I was looking for. $\endgroup$ – Matthew Apr 16 '13 at 19:01
  • $\begingroup$ How can I change the color? $\endgroup$ – Dinesh Shankar Mar 20 at 12:54
  • $\begingroup$ @Dinesh, you use Style["plot label", "Section", Blue] instead of Style["plot label", "Section"] $\endgroup$ – kglr Mar 20 at 15:41
14
$\begingroup$

You can specify your PlotLabel to be a construct that takes a TextAlignment or Alignment option, such as Pane. For example:

Plot[Sin[x], {x, 0, 5}, 
 PlotLabel -> Framed@Pane["This is the title", Alignment -> Left, 
    ImageSize -> 270], ImageSize -> 300]

enter image description here

The Framed isn't necessary; I just included it so you could see what was going on.

The main trick is that you need to specify the size of both the total graphic and the Pane, so that the text will align to the left edge of the graphic or the axis, as shown here.

If you don't do it this way, the Pane will indeed be left-aligned text, but it will "shrink" to fit the text and still be aligned center, like this:

Plot[Sin[x], {x, 0, 5}, 
 PlotLabel -> Framed@Pane["This is the title", Alignment -> Left]]

enter image description here

As an alternative, you can bend AxesLabel to fit, by working out how wide the label will be in pixels, and then adding that amount of spacing to its left in a Row construct to make the argument to AxesLabel. This is similar in spirit to Mr.Wizard's answer but has the advantage of automation of the spacing.

With[{space = 
   First@ImageDimensions[Rasterize[TraditionalForm[Sinc[x]]]]}, 
 Plot[Sinc[x], {x, 0, 10}, AxesLabel -> Row[{Spacer[space], Sinc[x]}] ]]

enter image description here

Obviously if you have styled the label for the plot label in some other way, you will need to adjust the argument to Rasterize accordingly.

$\endgroup$
  • $\begingroup$ Nice. For some reason I didn't think Pane worked. +1! $\endgroup$ – Mr.Wizard Apr 7 '13 at 5:06
10
$\begingroup$

You can use Epilog to insert the plot label wherever you wish within the PlotRange.

Edit: There is a drawback to this approach. Because the title will be within the graph region, there is a possibility that the title will be appear over part of the graph of the function.

Plot[Sinc[x], {x, 0, 9}, 
 Epilog -> Text[Style["This is where my plot label goes", {Blue, 16}], 
 Offset[{30, -10}, Scaled[{0, 1}]], {-1, 0}]]

plot

In the example, the text, the title is offset 30 printer's points (30/72 in) to the right and 10 printers points (10/72 in) under the upper left corner (Scaled[{0,1}]) of the Graphic. It is left-justified, {-1,0}.

$\endgroup$
4
$\begingroup$

Not directly that I am aware of. You could of course fake it with spacing:

Plot[Sinc[x], {x, 0, 9}, PlotLabel -> Row@{"Text", Spacer[300]}]

Simpler may be use Column but it is not part of the Graphics object itself:

Column@{"Text", Plot[Sinc[x], {x, 0, 9}]}
$\endgroup$
  • $\begingroup$ I need the label to be part of the Graphics object. The spacing option isn't very dynamic. I guess there's no "easy" way to do it. $\endgroup$ – Matthew Apr 7 '13 at 0:10
  • $\begingroup$ @Matthew Fortunately there is, and I was merely ignorant. See Verbeia's answer. :-) $\endgroup$ – Mr.Wizard Apr 7 '13 at 5:07
1
$\begingroup$

Here is a refinement of @DavidC's approach.

We can use PlotRangeClipping->False and then stick the label outside of the plot range and still have it show up. In order to do this we need to know the ImagePadding of the plot, and the height of the plot label. Here is the plot:

plot = Plot[Sinc[x], {x, 0, 9}]

enter image description here

We can find out the image padding using my GraphicsInformation function. Install with:

PacletInstall[
    "GraphicsInformation",
    "Site" -> "http://raw.githubusercontent.com/carlwoll/GraphicsInformation/master"
];

Then, load it:

<<GraphicsInformation`

GraphicsInformation returns a list of rules:

pad = "ImagePadding" /. GraphicsInformation[plot]

{{16.1531, 1.5}, {1.5, 0.5}}

To get the size of the label, we can use Rasterize:

label = Style["Plot label", "Section"];

Rasterize[label, "BoundingBox"]

{105, 33, 27}

The second element of the list is the height in points. Now, we are ready to add the label to the plot:

Show[
    plot,
    Graphics @ Text[label, Offset[{-16, 10}, Scaled[{0, 1}]], {-1, -1}],
    PlotRangeClipping->False,
    ImagePadding -> pad + {{0, 0}, {0, 43}}
]

enter image description here

I used an offset of 10 points in the vertical direction (to create space between the label and the y-axis), so that the total padding needed is actually 33 + 10, and not just 33.

$\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.