3

Following my previous question, I found that the minor grid is removed between 0 and 0.5. So, I tried to add grid = minor inside extra tick style

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[
    xtick={0.5,1,...,4.5},
    xmin=0,xmax=5,
    minor x tick num = {1},
    minor x tick style = {line width = 2pt},
    major x tick style = {line width = 2pt},
    xmajorgrids, xminorgrids,
    major x grid style = {dashed,red},
    minor x grid style = {dotted,black},
    extra x ticks={0,5},
    extra tick style={
        grid=minor,
    },]
        \addplot[mark=none,blue] {x^2};
    \end{axis}
\end{tikzpicture}
\end{document}

but it results in this

enter image description here

What I need is to

  1. remove the major ticks at 0 and 5 and
  2. place minor ticks at 0.25

Digressive Question

Why is there a minor tick placed at 4.75 and not at 0.25?

Diaa
  • 9,599
  • For the first, simply add tick style={draw=none} to extra tick style. And I think for the digressive question the answer is: I think the minor tick shouldn't be drawn for 4.75 neither, so this can be considered being a bug ... – Stefan Pinnow Apr 11 '17 at 17:49
  • @StefanPinnow Thanks, so how to place a minor tick at 0.25? – Diaa Apr 11 '17 at 17:52

1 Answers1

4

So you have to do the stuff "the other way round". So apply the style of the normal ticks to the extra ticks and vice versa.

The the comments of the code on how it works. (Not needed code I deleted.)

% used PGFPlots v1.14
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        xmin=0,xmax=5,
        % don't state the `xtick's here, but use just the default and state
        % the `xtick distance'
        xtick distance=0.5,
        minor x tick num={1},
        minor x tick style={line width=2pt},
        xminorgrids,
        minor x grid style={dotted,black},
        % but draw the `extra x ticks' with the values of the former `xtick' ...
        extra x ticks={0.5,1,...,4.5},
        % ... but don't draw any labels (because they are there already and
        % there is no need to draw them twice)
        extra x tick labels={},
        % finally apply the needed style for the extra ticks
        extra tick style={
            tick style={line width=2pt},
            major grid style={dashed,red},
            grid=major,
        },
    ]
        \addplot [mark=none,blue] {x^2};
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

Stefan Pinnow
  • 29,535
  • I was keen to understand the reasons behind failure in my way, but its seems that there is some bug, and your answer did the job. Many thanks for help. – Diaa Apr 11 '17 at 18:11