2
$\begingroup$

I'm using the CustomTicks package, which is part of the LevelScheme package, for styling the FrameTicks in my plots. On some sides of the plot frame, I want to have FrameTicks without tick labels. The problem is that I get extra white space in the place where the tick labels used to be. (Here, I have produced the plots with LightGray background in order to highlight the cropping problem.)

Needs["CustomTicks`"]

plot = Plot[x, {x, 0, 1},
  Frame -> True, 
  FrameTicks -> {LinTicks[0, 1, ShowTickLabels -> False],LinTicks[0, 1]},
  Background -> LightGray
]

Plot with unwanted white space

I think that the problem arises because the nonlabeled ticks are created as "", as can be seen in FullForm[plot]. How can I achieve a result similar to Plot[x,{x,0,1},Frame->True], i.e., a tight cropping of the plot around its frame (see below) with customized FrameTicks?

I do not want to rasterize my plot; thus, ImageCrop is not an option.

Plot[x, {x, 0, 1},
  Frame -> True,
  Background -> LightGray
]

Correct cropping but with default <code>FrameTicks</code>

$\endgroup$
1
  • $\begingroup$ I didn't try it with this package, but does ImagePadding work? $\endgroup$
    – Ian
    Jan 23, 2014 at 4:03

1 Answer 1

5
$\begingroup$

You can achieve the desired effect with the ImagePadding option. The syntax is ImagePadding-> {{left,right},{bottom,top}}.

This does not require you to rasterize the graphic. It might mean that you have to work out the appropriate whitespace on the sides that DO have tick labels, though.

I do not have CustomTicks installed but here is a simple function that tries to work out how much space you need on the bottom and left sides, instead of having to guess.

maxLabelSpace[labels_?VectorQ, type_String, 
  opts : OptionsPattern[{Style}]] := 
 With[{dim = Switch[type, "Horizontal", 1, "Vertical", 2, _, 1]}, 
  Max[ImageDimensions[#][[dim]] & /@ (Rasterize /@ (Style[ToString@#, 
          Sequence @@ {opts}] & /@ labels))]]

You can then do:

Plot[Sin[x], {x, 0, 4}, Frame -> True, 
 FrameStyle -> 12, 
 ImagePadding -> {{maxLabelSpace[{-0.5, 0.5, 1}, "Horizontal", 
  FontFamily -> "Times", FontSize -> 12], 1}, {maxLabelSpace[Range[4], "Vertical",    
  FontFamily -> "Times", FontSize -> 12], 1}}]

enter image description here

Notice that I've set the ImagePadding on the non-labeled sides to be 1, not 0. This avoids the frame being cut off.

Exercises for the reader include extending the maxLabelSpace function to add a pixel or two of whitespace to the ImagePadding, if that's what you want, via another parameter. You could also write a custom plotting function that automatically captures ticks and OptionValue[FrameStyle] to pass to maxLabelSpace.

$\endgroup$

Your Answer

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

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