Tell me more ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I use the CustomTicks package, because the built-in tick labeling leaves a lot to be desired. Even just putting the tick marks on the right instead of the left is a real pain without CustomTicks, from what I remember. I think this would be an issue even without this package, though.

Here is what I type in:

<< "CustomTicks`"
Plot[Sin[x], {x, 0, 2 π}, 
  Frame -> True, Axes -> False, 
  FrameTicks -> {Automatic, StripTickLabels[LinTicks], Automatic, LinTicks},
  BaseStyle -> 18]

and here is what I get:

enter image description here

I think the tick marks on the right look awful. They would be better if aligned on the decimal rather than on the left. But how do I fix this? My current workaround is to make another version of the plot with tick labels on the left, which usually are aligned properly because they align to the right. Then I cut the labels from that image and paste them onto the image I want.

Any other solution?

share|improve this question
1  
A simple, but crude way would be to prepend a transparent - to the positive ticks... – rm -rf Feb 12 at 1:06

4 Answers

up vote 9 down vote accepted

One way is to build your own ticks and format the numerical values for the ticks yourself. Here is a quick hack, just for illustration. I left the left side ticks as Automatic, to compare, and adjusted the right side and the lower side ticks as you wanted. You can play with this and adjust as needed.

enter image description here

padIt1[v_, f_List] := AccountingForm[Chop[v],
   f, NumberSigns -> {"-", "+"}, NumberPadding -> {"0", "0"},SignPadding -> True];
padIt2[v_, f_Integer] := AccountingForm[Round[v],
   f, NumberSigns -> {"", ""}, NumberPadding -> {"0", "0"}, SignPadding -> True];

fticks1[min_, max_] := 
  Table[If[Mod[i, 0.5] == 0, {i, padIt1[i, {3, 2}], {.03, 0.0}, 
     Red}, {i, Null, {0.01, 0.0}}], {i, Ceiling[min], Floor[max], .1}];

fticks2[min_, max_] := 
  Table[If[Mod[i, 1] == 0, {i, padIt2[i, 1], {.03, 0.0}, Red}, {i, 
     Null, {0.01, 0.0}}], {i, Ceiling[min], Floor[max], .2}];

Plot[Sin[x], {x, 0, 2 \[Pi]}, Frame -> True, Axes -> False, 
 FrameTicks -> {{Automatic, fticks1}, {fticks2, Automatic}}, BaseStyle -> 18]

This diagram explains the arguments in the above tick functions

enter image description here

share|improve this answer
Would NumberSigns -> {"-", " "} be even closer to what the OP wanted? – Verbeia Feb 12 at 2:39
@Verbeia, I assume you mean in the function padIt1. You can try and you'll see the number do not align over the decimal point any more. The size of the 'sign' itself is important. So using '-' in one number, and not using '+' for the positive one, will end up shifting things. A space is not the same exact size as "-", so had to use "+" there. Any way, the idea is to use a formatting function. I used AccountingForm since I am used to it. But one can use others if they do not like this form. – Nasser Feb 12 at 2:48

Update 2: a function to customize frame ticks:

 ClearAll[ticksF];
ticksF[majorminor_List: {5, 5}, mjrtcklngth_: {.01, 0.}, exmpl_: "-.500", 
 nmbrfrm_: {3, 2}][min_, max_] := Module[{majordivs, minordivs},
 {majordivs, minordivs} = Switch[Head[majorminor[[1]]],
 List, majorminor, 
 _, FindDivisions[{min, max}, majorminor]];
 Join[{#, "", mjrtcklngth/2.} & /@ DeleteDuplicates[Flatten[minordivs]],
 {#, Overlay[{Invisible[exmpl],
    Switch[Sign[nmbrfrm[[2]]],
     0, IntegerPart[N@#],
     -1, "",
     _, NumberForm[N@#, nmbrfrm]]},
   Alignment -> Right], mjrtcklngth} & /@ majordivs]]

Usage examples:

 Plot[Sin[x], {x, 0, 2 \[Pi]}, Frame -> True, Axes -> False, 
 BaseStyle -> 18, ImageSize -> 500,
 FrameTicks -> {
 {ticksF[{Range[-1, 1, .2], Range[-1, 1, .05]}, {0.02,  0.}, "", {0, -1}],
 ticksF[]},
 {ticksF[{7, 5}, {0.01, 0.}, "5", {3, 0}],
 ticksF[{7, 1}, {0.01, 0.}, "500", {3, 2}]}}]

enter image description here

 Plot[Sin[x], {x, 0, 2 \[Pi]},
 Frame -> True, Axes -> False, BaseStyle -> 18, ImageSize -> 500,
 FrameTicks -> {
 {ticksF[{Range[-1, 1, .2], Range[-1, 1, .05]}, {0.02, 0.}, "", {0, -1}],
 ticksF[{Range[-1, 1, .2], Range[-1, 1, .1]}]},
 {ticksF[{7, 5}, {0.01, 0.}, "5", {3, 0}],
  ticksF[{7, 1}, {0.01, 0.}, "500", {3, 2}]}},
 FrameTicksStyle -> {Directive[Green, Thick, FontWeight -> Bold, FontColor -> Purple],
   Directive[Red, Thickness[Large]], Automatic, 
   Directive[Brown, Bold]}]

enter image description here


 rightTicks = {#, Pane[NumberForm[N@#, {3, 2}], 45, Alignment -> Right]} & /@ 
  Range[-1, 1, 2/8];
 Plot[Sin[x], {x, 0, 2 \[Pi]}, Frame -> True, Axes -> False, BaseStyle -> 18,
 ImageSize -> 500, FrameTicks -> {{None, rightTicks}, {Automatic, None}}]

enter image description here

or, replace rightTicks by rightTicks2, where

rightTicks2 = {#,   Framed[NumberForm[N@#, {3, 2}], Alignment -> Right, 
  ImageSize -> 50, FrameMargins -> 0, FrameStyle -> None]} & /@ Range[-1, 1, 2/8];

In both cases, one needs to manually adjust the image size of the container.

Update: another trick using Overlay and Invisible:

 rightTicks3 = {#, Overlay[{Invisible["-0.75"], NumberForm[N@#, {3, 2}]}, 
 Alignment -> Right]} & /@ Range[-1, 1, 2/8]
share|improve this answer

Just a quick hack (without using CustomTicks)

Show[#, BaseStyle -> 14, FrameTicks -> (FrameTicks /. AbsoluteOptions[#, FrameTicks] /. 
{x_, y_, w_, z_}:> {x, z, w, y /. ({a_, b_, c_, d_} /; b >= 0 :> {a, "+"<>ToString@b, c, d})})]&@ 
     Plot[Sin[x], {x, 0, 2 Pi}, Frame -> True, Axes -> False]

Mathematica graphics

share|improve this answer
Great quick workaround, but it brings up the one of the big headaches I encountered trying to make my own tick marks. Now they do align on the decimal, but some have one digit, some two, and some three. Is one of the main reasons I use CustomTicks. – user5789 Feb 12 at 19:23
@Jimbo But you can fix it with NumberForm if you like. Show[#, BaseStyle -> 14, FrameTicks -> (FrameTicks /. AbsoluteOptions[#, FrameTicks] /. {x_, y_, w_, z_} :> {x, z, w, y /. ({a_, b_, c_, d_} /; b != "" :> {a, ToString@ NumberForm[ToExpression@b, {5, 2}, NumberSigns -> {"+", "-"}, NumberPadding -> {"", "0"}], c, d})})] &@ Plot[Sin[x], {x, 0, 2 Pi}, Frame -> True, Axes -> False] – belisarius Feb 12 at 20:51

Thanks for all the answers given, especially those from Nasser and kguler. It turns out there is an easy way to do this using the LinTicks function. I love using the CustomTicks package, where they've already done a lot of the work for me.

<< "CustomTicks`"
Plot[Sin[x], {x, 0, 2 π}, 
  Frame -> True, Axes -> False, 
  FrameTicks -> {Automatic, StripTickLabels[LinTicks], Automatic, 
    LinTicks[-1, 1, .5, 5, TickLabelFunction -> (If[#1 >= 0.0, "+" <> #2, #2] &)]},
  BaseStyle -> 18]

gives this result:

enter image description here

share|improve this answer

Your Answer

 
discard

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