3

I am using the following code to plot same experimental data, and for some reason, minor xticks appear to be misaligned from their intended position. As the x value incrases, there is more and more space between minor and major marks even though every second minor mark should perfectly align with a major one. Why is this happening? Could it have something to do with the small value of minor xtick step (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}

The result is the following graph. Look at the x-axis closely.

enter image description here

2
  • 1
    yes this is expected. foreach lists use TeX arithmetic and that goes wrong in limited precision. Try giving the entries manually and it will go away. There is a duplicate about this somewhere on this site.
    – percusse
    Sep 6, 2017 at 23:46
  • Adding manual entries, despite being a bit tedious, also works, thanks! I tried to find this error as not to post a duplicate question, but I was unsuccessful. Sep 11, 2017 at 1:54

1 Answer 1

3

enter image description here

you define minor grid on unusual way. see changes (indicated by <--- added and with commented lines of code) in mwe below:

\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}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.