pgfplots:刪除主要網格後恢復次要網格/刻度

pgfplots:刪除主要網格後恢復次要網格/刻度

下列的我之前的問題,我發現0和之間的小網格被刪除了0.5。所以,我嘗試grid = minor在裡面添加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}

但結果是這樣的

在此輸入影像描述

我需要的是

  1. 0刪除和5處的主要勾號
  2. 放置小刻度0.25

離題問題

為什麼在 處有一個小勾號4.75,而不是在 處0.25

答案1

所以你必須「反過來」做這些事情。因此,將正常刻度線的樣式套用於額外刻度線,反之亦然。

程式碼關於其工作原理的註解。 (不需要的程式碼我刪除了。)

% 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}

顯示上述程式碼結果的圖像

相關內容