Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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>

share|improve this question
    
I didn't try it with this package, but does ImagePadding work? –  Ian Jan 23 at 4:03

1 Answer 1

up vote 4 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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