3

I'm trying a plot the left y-axis from 0.5 to 0.88 with 0.88 at the top border of the box (like the 160 at the 2nd y-axis).

But even to get 0.88 I have to set ymax and (the last ytick) to 0.9 and it's still not at the top!

Why is this?

Why do I have set ymax and ytick 0.02 points further then the number I would like to have? And why isn't the number at the top?

I really don't know what's the problem!

Thanks in advance!

\documentclass[fontsize=12pt,
            paper=a4,
            twoside=false,
                    ]{scrreprt}


\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{siunitx}

\usepackage{pgfplots}
\pgfplotsset{compat=1.9,                        
        /pgf/number format/1000 sep={},     
        /pgf/number format/use comma,       
        }
\usepgfplotslibrary{units}          
\usepgfplotslibrary{groupplots}                     

\begin{document}
%
\begin{tikzpicture}
%
\pgfplotsset{set layers,            
        ticklabel shift={0.05cm}}       
%
\begin{axis}[width=0.8\textwidth,
            grid=major,
            ymin = 0.5,
            ymax = 0.9,
            ytick={0.5,0.52,...,0.9},
            y tick label style={/pgf/number format/fixed,/pgf/number format/fixed zerofill=true,/pgf/number format/precision=2},    
            axis y line*=left,
            ylabel={voltage},
            y unit=\si{\volt},
            xlabel={time},
            x unit = \si{\minute},
            xtick={0,5,...,75},
            xmin = 0,
            xmax = 75,
            ]
%
\end{axis}
%
\begin{axis}[width=0.8\textwidth,
            %grid=major,
            %/pgf/number format/precision=3,
            legend style={legend columns=-1,anchor=north},
            legend style={at={(0.5,-0.15)}},
            %legend cell align=left,
            ymin = 0,
            ymax = 160, 
            %ytick={0,10,...,160},
            axis y line*=right,
            xlabel={time},
            x unit = \si{\minute},
            xtick={0,5,...,75},
            xmin = 0,
            xmax = 75,
            axis x line=none,
            ylabel={current},
            y unit= \si{\ampere},
            ]
%               
%
\end{axis}
%
\end{tikzpicture}
%
\end{document}

After further investigation I was able to figure out the problem.

It seems that setting ymin and ymax and additionally ytick={...} causes the problem.

ytick={...} seems (sometimes) to work differently dependent on the definition.

Here's a MWE with the behaviour and an explanation:

ymax and ymin are 0.5 and 1 and I would like to have 0.05 steps at the y-axis.

If I only define ymin=0.5 and ymax=1 the ticks at the y-axis are 0.5, 0.6, 0.7, ..., 1.0. But that's not what I want.

So instead of using ymin and ymax I define ytick={0.5, 0.55, ..., 1}. But then the ticks at the y-axis are only from 0.55 to 0.95. So the first and the last one are being ignored.

If I only define ytick={0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95, 1} then only the first tick is being ignored. So the ticks at the y-axis are from 0.55 to 1.

Therefore, defining ymin=0.5 and ymax=1 and additionally ytick={0.5, 0.55, ..., 1} prevents the last y-tick 1.0 from being printed!

And that's my problem. I want to have 0.5 to 1 with 0.05 steps. But specifying these additionally with ytick={0.5, 0.55, ..., 1} it only prints until 0.95.

What I'm doing now is defining ymin=0.5, ymax=1 and ytick={0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95, 1} to get the right divisions and all the ticks.

I don't know if it's really a problem or if I simply don't understand the behaviour of setting up ymax and ymin and additionally ytick={...}?!?!

Any ideas to avoid defining all the y-ticks or an explanation how the set up the y-ticks properly are very welcome!

MWE:

\documentclass[fontsize=12pt,
        paper=a4,
        twoside=false,
                 ]{scrreprt}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{pgfplots}
\pgfplotsset{compat=1.9,
     /pgf/number format/1000 sep={},
     /pgf/number format/use comma,
     enlargelimits=false,
     }

\begin{document}
%
\begin{tikzpicture}
%
\begin{axis}[grid=major,
        xlabel ={time /min},
        xtick={0,10,...,60},
        xmin = 0,
        xmax = 60,
        ylabel = {voltage /V},
        ymin = 0.5,
        ymax = 1,
        %ytick={0.5,0.55,...,1},   %<-- first and last ytick is being ignored
        %ytick={0.5,0.55,0.6,0.65,0.7,0.75,0.8,0.85,0.9,0.95,1}  %<-- first ytick is being ignored
        ]
        \addplot[color=red,no marks] coordinates{(0,0.52)(10,0.75)(20,0.85)(30,1)(40,0.85)(50,0.75)(60,0.55)};
\end{axis}
%
\end{tikzpicture}
%
\end{document} 
  • Welcome to TeX.SX! – user31729 Aug 9 '14 at 16:14
  • Add enlargelimits=false to the \pgfplotsset options. There is a default enlarging action in place. – percusse Aug 9 '14 at 16:23
  • Thanks for your answer, but it doesn't work! – prankenandi Aug 9 '14 at 16:43
  • 1
    Works for me though. – percusse Aug 9 '14 at 17:07
  • @AdamLiter Because it was so much additional information, I wasn't sure if it's ok to edit question. So I did add the answer as additional information to the question and tried to delete my own answer. – prankenandi Aug 10 '14 at 15:16
3

Apparently, before I commented I changed a few things at once. It is still possible to have a solution but here the main problem is how \foreach produces arrays.

First of all, pgfplots checks the min and max data so in your example there is nothing drawn inside the axis so takes the interval [0,1]. Without data, it will still be this interval no matter what you provide. I don't know if this a feature or something overrides a setting.


When \foreach starts reading the increment in the dotted syntax, it starts adding the increment to the initial point and if it goes beyond last point it discards it, say,

\foreach\x in{1,2,...,4.9}{\x}

will give 1234. So it has to hit something below the last number or exactly the last number.

Here is a compactified version of your problem. See how the numbers get contaminated by TeX arithmetic.

\documentclass[]{standalone}
\usepackage{siunitx,pgfplots}
\pgfplotsset{compat=1.10}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
            ymin = 0.5,
            ymax = 0.88,
            y tick label style={/pgf/number format/.cd,fixed,fixed zerofill=true,precision=6},
            ytick={0.5,0.52,...,0.88},
            ]\addplot {0.7 + (rand)/10};
\end{axis}
\end{tikzpicture}
\end{document}

The easiest solution is to shift the end point ever so slightly, say, 0.8801 and then you'll hit that point.

A possible solution is to implement the \foreach within axis environment with fpu enabled but it looks like quite some work.

enter image description here

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.