7
$\begingroup$

I am very sorry if this is trivial, but I can not for the life of me adjust the thickness of all tick marks in Plots. Depending on the data to plot, either the top, bottom, top and bottom, or top and right ticks stay unaffected. I could not yet find a pattern. Additionally, the ticks look different, depending if I export as PNG or PDF. I need PDF, so I have attached a screenshot of the PDF output.

So, how do I get all the ticks to look like the thicker ones?

Minimal example:

Plot[Sin[x],{x,-7,7},Frame->True,FrameStyle->Thickness[.003],FrameTicksStyle->Thickness[.003]]

enter image description here

My actual example:

ListLinePlot[CaP,
PlotRange->All,
Frame->True,
FrameLabel->{"\[Nu] / cm^-1","Absorption / a. u."},
FrameStyle->Thickness[.003],
LabelStyle->{12},
FrameTicksStyle->{Directive[FontOpacity->0,FontSize->0,Thickness[.003]],Directive[Thickness[.003],Black]},
AspectRatio->1/2,
ScalingFunctions->{"Reverse"},AxesOrigin->{850,0}
]

enter image description here

That is Mathematica 11. Mathematica 10 generates the same output, but the top ticks are as they should be, only the bottom ticks stay unaffected. Same code.

$\endgroup$
  • 1
    $\begingroup$ I think you're doing this correctly in your first example, all the frame ticks should be thick. This is likely a bug. I see the same behavior on OSX v.11.1.1.0 $\endgroup$ – N.J.Evans Sep 7 '17 at 18:56
  • $\begingroup$ Maybe a duplicate of mathematica.stackexchange.com/q/154092 $\endgroup$ – m_goldberg Sep 7 '17 at 23:59
  • $\begingroup$ Since the underlying bug is exactly the same as here, I'm voting to close this question as a dupe of that. $\endgroup$ – Alexey Popkov Sep 8 '17 at 8:53
7
$\begingroup$

It seems that the internal functions to create ticks have a hard-coded thickness that overrides the FrameTicksStyle option. You could work around this by modifying this internal function. The easiest way to do so is to use my Initial function, which I reproduce below:

Initial /: Verbatim[TagSetDelayed][Initial[sym_], lhs_, rhs_] := With[
    {
    new=Block[{sym},
        TagSetDelayed[sym,lhs,rhs];
        First @ Language`ExtendedDefinition[sym]
    ],
    protect=Unprotect[sym]
    },

    sym;
    Quiet@MakeBoxes[sym[],TraditionalForm];

    Unprotect[sym];
    Replace[
        new,
        Rule[values_,n:Except[{}]] :> (
            values[sym] = DeleteDuplicates @ Join[n, values[sym]]
        ),
        {2}
    ];
    Protect@protect;
]

(updated to modify Charting`ScaledTicks instead of Charting`ScaledFrameTicks so that both ticks with and without numbers are fixed)

The internal function that we need to modify is Charting`ScaledTicks:

Initial[Charting`ScaledTicks] /: 
    Charting`ScaledTicks[a__][b___] /; !TrueQ@$Fix := Block[{$Fix=True},
        Charting`ScaledTicks[a][b] /. AbsoluteThickness[.1]->Sequence[]
    ]

The above just removes the hard-coded AbsoluteThickness[.1]. Now, let's check:

GraphicsRow[{
    Plot[
        Sin[x],
        {x,-7,7},
        Frame->True
    ],
    Plot[
        Sin[x],
        {x,-7,7},
        Frame->True,
        FrameStyle->AbsoluteThickness[2]
    ]
}]

enter image description here

$\endgroup$
  • $\begingroup$ This works for the sine plot, but for my actual example it does not work. The tick marks on the bottom stay unaffected. $\endgroup$ – Seriouslynothing Sep 8 '17 at 7:16
  • $\begingroup$ I think doing the exact same thing for Charting`ScaledTicks instead should fix both of them. $\endgroup$ – Carl Woll Sep 8 '17 at 7:24
  • $\begingroup$ Yes, that fixed it. Thank you! $\endgroup$ – Seriouslynothing Sep 8 '17 at 7:50
4
$\begingroup$

It looks like an erroneous FrameTicks rule is being put in the full form. You can get around this by doing the following:

yourPlot=Plot[
 Sin[x], {x, -7, 7},
 FrameStyle -> Thickness[.003],
 Frame -> True,
 FrameTicksStyle ->Directive[Thickness[.005]]
 ];


ToExpression@ToBoxes[
    FullForm[yourplot]/.Rule[FrameTicks,_]:>Nothing
]

The one bit of trouble here is that this will also delete any FrameTicks you specify. If you need custom positioned ticks, you can try:

ToExpression@ToBoxes[
    FullForm[yourplot]/.Rule[FrameTicks,_]:>Rule[FrameTicks,yourFrameTicks]
]

Without fix:

enter image description here

With fix:

enter image description here

$\endgroup$
  • $\begingroup$ This works, too. Thank you! $\endgroup$ – Seriouslynothing Sep 8 '17 at 8:31
4
$\begingroup$

This issue is exactly the same as in the following question:

Starting from Mathematica 11.1 Plot uses Charting`ScaledFrameTicks in the default value for FrameTicks:

Options[Plot[x, {x, 0, 1}], FrameTicks]    
{FrameTicks -> {{Automatic, Charting`ScaledFrameTicks[{Identity, Identity}]}, 
                {Automatic, Charting`ScaledFrameTicks[{Identity, Identity}]}}}

As a result, the bug in Charting`ScaledFrameTicks now affects also Plot. The same type of fix is applicable here:

fixTickThickness[gr_] := gr /. f : (_Charting`ScaledTicks | _Charting`ScaledFrameTicks) :>
   (Part[#, ;; , ;; 3] &@*f)

Example:

$Version

pl = Plot[Sin[x], {x, -7, 7}, Frame -> True, FrameStyle -> AbsoluteThickness[3], 
   ImageSize -> 400];

Row@{pl, pl // fixTickThickness}
"11.1.1 for Microsoft Windows (64-bit) (April 18, 2017)"

output


Update

As it is noted in the comments, the above fix needs to be applied to every plot separately what is tedious. Actually one can make this fix permanent by modifying the default value of DisplayFunction of Plot and other functions affected by this bug:

SetOptions[
  Select[Symbol /@ Names["*Plot"], MemberQ[First /@ Options[#], DisplayFunction] &],
  DisplayFunction :>
   (ReplaceAll[#,
      t : (Ticks | FrameTicks -> _) :>
       (t /. f : (_Charting`ScaledTicks | _Charting`ScaledFrameTicks) :>
          (Part[#, ;; , ;; 3] &@*f))] &)];

One can add this piece of code to the Kernel's init.m file in order to have it loaded automatically when the Kernel starts. But while this demonstrates another approach to the problem, the solution by Carl Woll is more direct and should be preferable in this concrete case.

$\endgroup$
  • $\begingroup$ My answer is a one-time fix to Charting`ScaledTicks while yours is a post-process step that needs to be done to every plot. $\endgroup$ – Carl Woll Sep 8 '17 at 15:03
  • $\begingroup$ @CarlWoll Yes, I upvouted your answer before posing mine. Actually it is possible to convert my solution into a on-time fix too, please see the Update section in my answer. $\endgroup$ – Alexey Popkov Sep 9 '17 at 5:35

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