小 xtick 标记错误吗?

小 xtick 标记错误吗?

我正在使用以下代码绘制相同的实验数据,出于某种原因,小 xtick 似乎与其预期位置错位。随着 x 值的增加,小标记和主标记之间的空间越来越大,尽管每隔一个小标记应该与一个主标记完美对齐。为什么会发生这种情况?这可能与小 xtick 步长(1e-3)的值较小有关吗?

\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{tikz}

\begin{document}
\centering
\begin{tikzpicture}
  \begin{axis}[
    width = 11cm, height = 7cm,
    xmin = 0.000, xmax = 0.018, ymin = 2, ymax = 9,
    xtick = {0.000,0.002,...,0.016}, minor xtick = {0.000,0.001,...,0.018},
    ytick = {2,3,...,8}, minor ytick = {2.0,2.2,...,9},
    xlabel = $I\mbox{ }{[A]}$, ylabel = $U\mbox{ }{[V]}$,
    axis line style = thick, axis lines = middle,
    hide obscured x ticks = false, hide obscured y ticks = false,
    x tick label style = {/pgf/number format/.cd, fixed, fixed zerofill, precision = 3, use comma, /tikz/.cd}, scaled x ticks = false,
    clip = false, grid = both,
    grid style = {line width=.1pt, draw=gray!20},
    major grid style = {line width = .2pt, draw = gray!50},
    enlargelimits = false,
    scale = 1,
  ]
  \addplot[color = black, thick, domain = 0.006:0.016]{-307.11*x+7.9911};
\end{axis}
\end{tikzpicture}
\end{document}

结果如下图所示。仔细观察 x 轴。

在此处输入图片描述

答案1

在此处输入图片描述

你以不寻常的方式定义次要网格。请参见中的更改(通过<--- added注释代码行指示)姆韦以下:

\documentclass[margin=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
%\usepackage{pgfplotstable} % not needed in this example
%\usepackage{tikz}          % loaded by pgfplots

\begin{document}
\centering
\begin{tikzpicture}
  \begin{axis}[
    width = 11cm, height = 7cm,
    xmin = 0, xmax = 0.018,
    ymin = 2, ymax = 9,
    xtick = {0.000,0.002,...,0.016},
%    minor xtick, = {0.000,0.001,...,0.018},
%    ytick = {2,3,...,8},
    extra y ticks = {3,5,7}, % <--- added
%    minor ytick, = {2.0,2.2,...,9},
    xlabel = $I\mbox{ }{[A]}$, ylabel = $U\mbox{ }{[V]}$,
    axis line style = thick, axis lines = middle,
    x tick label style = {/pgf/number format/.cd,
                          fixed, fixed zerofill, precision = 3, use comma,
                          /tikz/.cd},
%    hide obscured x ticks = false,
%    hide obscured y ticks = false,
    scaled x ticks = false,
    minor x tick num=1, % <--- added
    minor y tick num=4, % <--- added
    grid = both,
    minor grid style = {densely dashed,very thin},
%    enlargelimits = false,
%    scale = 1,
  ]
  \addplot[color = black, thick, domain = 0.006:0.016]{-307.11*x+7.9911};
\end{axis}
\end{tikzpicture}
\end{document}

相关内容