12
$\begingroup$

I would like to have rounded ticks in my Mathematica plots. They look much better for thick frames

Plot[ {Sin[x], Cos[x]}, {x, 0, 3 Pi}, Frame -> True, FrameStyle -> Directive[Black, Thick]]

gives plot with square tick ends

but I would like to round them, to get something like

plot with round tick ends

There is a function CapForm["Round"] but it is not clear how to use it with plots.

$\endgroup$
1

2 Answers 2

16
$\begingroup$

Here is another approach which is based on converting the plot to PDF format first. It makes the tick marks accessible as regular Graphics objects. Specifically, they (and the frame) show up as open JoinedCurve that can be identified by pattern matching. That leads to the following:

p = Plot[{Sin[x], Cos[x]}, {x, 0, 3 Pi}, Frame -> True, 
   FrameStyle -> Directive[Black, Thick]];

First@ImportString[ExportString[p, "PDF"], "PDF"] /. 
 JoinedCurve[{{{0, 2, 0}}}, x_, 
   CurveClosed -> {0}] :> {CapForm["Round"], 
   JoinedCurve[{{{0, 2, 0}}}, x, CurveClosed -> {0}]}

plot

What I did here is to replace the identified curves by wrapping them in {CapForm["Round"], ...}. This will affect all straight lines, including the frame itself (but the frame doesn't have end points because the corners overlap anyway). The plot lines themselves are not affected by the rounding because they aren't straight lines.

The advantage of this approach is that you don't have to manually calculate and specify the tick positions: the ticks will be modified in post-processing without changing their positions. The disadvantage is that the ticks in the resulting plot can no longer be modified by changing the options with Show, because they are "baked in".

$\endgroup$
3
  • $\begingroup$ Just wonderful. I'll spend the next hours playing with your answer :) $\endgroup$
    – eldo
    Sep 4, 2014 at 18:21
  • $\begingroup$ (+1) Nice! There is built-in FullGraphics function designed exactly for the purpose you achieve (in a hackish way) with Import[Export[...]] but unfortunately its support seems to be dropped since at least version 5. Export to PDF rounds all the coordinates to 16-bit precision and the Imported plot will have lesser accurate positioning of the graphical primitives than the original plot (though it is still sufficient for the most purposes). $\endgroup$ Sep 5, 2014 at 14:40
  • $\begingroup$ @AlexeyPopkov Yes, that's right. But FullGraphics was never reliable. And when you're talking about plots with ticks etc., I think we can assume that a reduction to 16-bit precision will be visually imperceptible. This precision issue is indeed important when your goal is to use the vector graphics in some other high-res operation later. Then I don't think one would want a frame and ticks in the first place, though. $\endgroup$
    – Jens
    Sep 5, 2014 at 16:51
1
$\begingroup$

I'm afraid you will have to build your own graphic primitives. Something like this:

Show[
 Plot[Sin[x], {x, 0, 5}],
 Graphics[{GrayLevel[0.3], Table[Disk[{n, 0}, {0.025, 0.04}, {0, Pi}], {n, 0, 5}]}],
 ImageSize -> 600]

enter image description here

$\endgroup$

Your Answer

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

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