3

Following this answer, I tried to remove the grid lines at xmin and xmax, but it fails. What is missing here?

Also, how can I input the variables xmin and xmax in extra x ticks without the need to write the numbers manually?

\documentclass{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[
    xtick={0,0.5,...,5},
    xmin=0,xmax=5,
    xmajorgrids,
    major x grid style = {line width =2pt,dashed,black},
    extra x ticks={0,5},
    extra x tick labels={},
    extra tick style={
        grid=none,
    },]

        \addplot[mark=none,blue] {x^2};

    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

Diaa
  • 9,599

2 Answers2

3

You are almost there: just replace

xtick={0,0.5,...,5},

by

xtick={0.5,1,...,4.5},

and then remove

extra x tick labels={},

Edit

OP asks

why my solution fails in this way

The idea of this kind of workaround is based on a fact: that uses the xtick's position to draw the grids. Therefore as long as 0 and 5 are in xtick, it will draw the line as well as it will draw the line for 0.5, 1, 1.5 and so on.

how can I call the values 'xmin' and 'xmax' anywhere as numeric values?

This is ambiguous. If you mean the values that the user assigned as options, (instead of the nature min/max calculated by ), then I believe that it can be access by \pgfkeysvalueof{/pgfplots/xmin}.

Symbol 1
  • 36,855
3

Basically this is the same answer as Symbol 1 ones, but is a bit more "automated", which means, that you now only have one place to take care of for setting the xmin, xmax and xtick distance values.

% used PGFPlots v1.14
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
        % =====================================================================
        % state here the `xmin', `xmax' and `xtick distance' values
        \pgfmathsetmacro{\xmin}{0}
        \pgfmathsetmacro{\xmax}{5}
        \pgfmathsetmacro{\DeltaX}{0.5}
        % =====================================================================
        % calculate the `xtick' positions without drawing the `xmajorgrid' at
        % `xmin' and `xmax'
        \pgfmathsetmacro{\xmintick}{\xmin + \DeltaX}
        \pgfmathsetmacro{\nexttick}{\xmin + 2*\DeltaX}
        \pgfmathsetmacro{\lasttick}{\xmax - \DeltaX}
    \begin{axis}[
        % set `xmin' and `xmax'
        xmin=\xmin,
        xmax=\xmax,
        % set the `xtick' values using the defined variables
        xtick={\xmintick,\nexttick,...,\lasttick},
        xmajorgrids,
        major x grid style={
            dashed,
            red,
        },
        % add the extra ticks without using the grid lines at `xmin' and `xmax'
        % (for simplicity we can use the defined commands here ...
        extra x ticks={\xmin,\xmax},
%        %  ... but since we have set these values explicitly we can also call
%        %  the values)
%        extra x ticks={
%            \pgfkeysvalueof{/pgfplots/xmin},
%            \pgfkeysvalueof{/pgfplots/xmax}
%        },
        % set the style of the extra ticks
        extra tick style={
            % remove the grid
            % by default the `extra tick style' is the same as for the "normal" ticks
            grid=none,
        },
    ]
        \addplot [mark=none,blue] {x^2};
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

Stefan Pinnow
  • 29,535