0

I have a set of tick that are too close to each other and I would like to customize the total length of individual ticks amongst this set. The customization would be applicable for variable legnth and more than two different ticks. See picture below.

enter image description here

The outcome that I would wish to is as following:

enter image description here

I have seen a couple of questions and answers regarding accessing individual ticks by using xshift={\ticknum == <the tick number to access> ? "<value to be applied>" : <else statement>} when it comes to label positions (Ref: Individual tick label style depending on position). Though, when it comes to tick customization, several posts tackled by using the different types of tick (extra, ...) (Ref: Positioning Even or Odd x axis tick labels in PGFPlots) but that sort of limits to two different types of customization.

This works pretty fine for xshift, yshift and I guess it works for even more in the tick label style level .. Though, when I try to use the same principle in the tick style level, I get error messages that it cannot be recognized, as in the example below:

enter image description here

For the error message, that is the given message: The compiler is having trouble understanding a command you have used. Check that the command is spelled correctly. If the command is part of a package, make sure you have included the package in your preamble using \usepackage{...}.

There was one post that work around (Ref: Individual Tick style for each Tick ) by creating different \begin{axis} ... \end{axis} . Though, it does not seem right :))

I wonder if there is anyway to access the individual ticks within the same \begin{axis} ... \end{axis} .

[EDIT] For reference, below a base code.

\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[%
                        ymajorgrids=true, xmajorgrids=true, grid style=dashed,
                        xmin=0,
                        xmax=3,
                        ymin=0, 
                        ymax=3.2,
                        ytick={0,1,2,3},
                        extra y ticks = {0.436, 1.251, 1.436, 2.436, 2.844},
                        extra y tick labels = {$\overline{G_1}$, $\overline{G_2}$, $\overline{G_3}$, $\overline{G_4}$, $\overline{G}$},
                       extra y tick style={
                            y tick label style={
                                %xshift={\ticknum == 2 ? "20pt" : (\ticknum == 4 ? "-5pt" : "0pt")},
                                %yshift={\ticknum == 2 ? "+7pt" : ("0pt")},                                
                            },
                            %major tick length = 0.45cm,
                            %xshift = -0.25cm,                            
                            %major tick length={\ticknum == 3 ? "0.45cm" : ("0.15cm")}
                        },
                        ylabel = {$\overline{v(t)}$}
]    
\addplot[no marks,samples=250, thick] {x};
\end{axis}
\end{tikzpicture}
\end{document}

Thank you,

2
  • Please add a Minimal Working Example to your question. A picture of a code fragment is not useable for people who want to help you. Dec 26, 2022 at 22:49
  • Hello Kristian, I have added a base code for the example. I did not place it before because I thought it could be a situation for any type of plot with individual access of tick style. Thanks for your help! Dec 28, 2022 at 17:37

1 Answer 1

1

\tick and \ticknum is only for usage in the tick labels - that is in x tick label style, xticklabel and the like. The best option would be to draw the ticks manually inside the axis. If you really want, you can make the label draw a small line like this:

\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=0, xmax=10,
ymin=0, ymax=10,
ytick distance=1,
y tick label style={execute at end node={\draw[red] (3.4,2) -- +(\ticknum,0);}},
]    
\end{axis}
\end{tikzpicture}
\end{document}

Empty graph with varying y tick lengths

This takes manual adjustments and it breaks as soon as the scale, font or any sizes changes.

Edit: The same trick with the ticks outside

\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=0, xmax=10,
ymin=0, ymax=10,
ytick distance=1,
y tick label style={xshift=-2*\ticknum, execute at end node={\draw[red] (3.4,2) +(2*\ticknum pt,0) -- +(0,0);}},
major tick length=0 pt,
]    
\end{axis}
\end{tikzpicture}
\end{document}

Variable length ticks outside axis

I still do not commend this method. -much better to use e.g ytick=\empty and add all the ticks and labels manually inside the axis.

1
  • Thanks for your comment, @hpekristiansen .. With the given suggestion, in case you align the ticks on the outside, would the ticklabels collapse to the manually added ticks? or would they follow along and add a minimum distance to the tick? Thanks Dec 28, 2022 at 17:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .