Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'm generating pgf plots on the fly and I would like to avoid adding non integer y ticks on my plots, while keeping all the x ticks. Here is a MWE:

\documentclass[10pt]{article}
\usepackage{tikz}
\usepackage{amsmath}
\usepackage{siunitx}
\usepackage{amssymb}
\usepackage{pgfplotstable}
\usepackage[active, pdftex, tightpage]{preview}
\PreviewEnvironment[{[]}]{tikzpicture}
\setlength\PreviewBorder{2mm}
\begin{filecontents}{PJxpGrZtwos.tex.dat}
N      f             M
1024   3.407938e+01  25
1536   1.339487e+01  20
2048   1.139487e+01  19
3072   8.351236e+00  14
4049   6.466788e+00  13
6144   4.466788e+00  11
\end{filecontents}
\pgfkeys{/pgf/fpu=true}
\pgfplotstableread{PJxpGrZtwos.tex.dat}\loadedtable
\pgfplotstablegetelem{0}{N}\of{\loadedtable}
\let\relativeTo\pgfplotsretval
\pgfplotstablegetelem{0}{f}\of{\loadedtable}
\let\f\pgfplotsretval
\pgfplotstablecreatecol[create col/expr={\f / \thisrow{f}}]{f-relative}\loadedtable
\pgfkeys{/pgf/fpu=false}

\begin{document}
\begin{tikzpicture}
    \begin{loglogaxis}[axis y line*=left, legend pos=south east,xlabel={N},xtick=data, xticklabels from table={\loadedtable}{N},ytick=data, yticklabel=\pgfmathparse{exp(\tick)}\pgfmathprintnumber{\pgfmathresult}, ylabel ={$\dfrac{f(\pgfmathprintnumber\relativeTo)}{f(N)}$}]
\addplot[black] table[x=N, y expr=\thisrow{N}/\relativeTo] {\loadedtable};
\addplot[blue, mark=*] table[x=N, y=f-relative] {\loadedtable};
\legend{{Linear progression},}
\end{loglogaxis}
\begin{semilogxaxis}[ymin = 5, ytick={10, 15, ..., 30}, ymax = 35, axis x line=none,ylabel={\# M}, axis x line=none, axis y line*=right, ylabel near ticks=right, yticklabel pos=right]
\addplot[red, dotted, mark=x, mark options={solid}] table[x=N, y=M] {\loadedtable};
\end{semilogxaxis}
\end{tikzpicture}
\end{document}

What I would like here is to have \addplot[black] table[x=N, y expr=\thisrow{N}/\relativeTo] {\loadedtable}; not adding y ticks for non integer values of \thisrow{N}/\relativeTo while keeping all x ticks.

If you have any other way to draw a linear function from (x_min, 1) to (x_max, x_max/x_min), while having the nice corresponding y ticks (maybe by creating another table ?), that will also do the trick.

Keep in mind that this is for 'on the fly' plot generation, I don't know my value a priori, so I can't use in this case y tick={1,...,6}.

share|improve this question

2 Answers 2

up vote 4 down vote accepted

You can introduce some tolerance for the tick labels via some if conditions.

\documentclass{standalone}
\usepackage{amsmath,amssymb,siunitx}
\usepackage{pgfplotstable}
\begin{filecontents}{PJxpGrZtwos.tex.dat}
N      f             M
1024   3.407938e+01  25
1536   1.339487e+01  20
2048   1.139487e+01  19
3072   8.351236e+00  14
4049   6.466788e+00  13
6144   4.466788e+00  11
\end{filecontents}
\pgfkeys{/pgf/fpu=true}
\pgfplotstableread{PJxpGrZtwos.tex.dat}\loadedtable
\pgfplotstablegetelem{0}{N}\of{\loadedtable}
\let\relativeTo\pgfplotsretval
\pgfplotstablegetelem{0}{f}\of{\loadedtable}
\let\f\pgfplotsretval
\pgfplotstablecreatecol[create col/expr={\f / \thisrow{f}}]{f-relative}\loadedtable
\pgfkeys{/pgf/fpu=false}

\begin{document}
\begin{tikzpicture}
\begin{loglogaxis}[
axis y line*=left, 
legend pos=south east,
xlabel={N},
xtick=data, 
xticklabels from table={\loadedtable}{N},
ytick=data, 
yticklabel={
\pgfmathparse{abs(exp(\tick) - int(exp(\tick)))}
\ifdim\pgfmathresult pt>0.999 pt
    \pgfmathparse{exp(\tick)}
    \pgfmathprintnumber{\pgfmathresult}
\else
    \ifdim\pgfmathresult pt<0.01 pt
    \pgfmathparse{exp(\tick)}
    \pgfmathprintnumber{\pgfmathresult}
    \else{}
    \fi
\fi
}, 
ylabel ={$\dfrac{f(\pgfmathprintnumber\relativeTo)}{f(N)}$}]

\addplot[black] table[x=N, y expr=\thisrow{N}/\relativeTo] {\loadedtable};
\addplot[blue, mark=*] table[x=N, y=f-relative] {\loadedtable};
\legend{{Linear progression},}
\end{loglogaxis}
\begin{semilogxaxis}[ymin = 5, ytick={10, 15, ..., 30}, ymax = 35, axis x line=none,ylabel={\# M}, axis x line=none, axis y line*=right, ylabel near ticks=right, yticklabel pos=right]
\addplot[red, dotted, mark=x, mark options={solid}] table[x=N, y=M] {\loadedtable};
\end{semilogxaxis}
\end{tikzpicture}
\end{document}

enter image description here

share|improve this answer
    
Great ! Thanks. Do you know why neither yticklabel={ \pgfmathifisint{exp(\tick)}{\pgfmathparse{exp(\tick)}\pgfmathprintnumber{\pgfmat‌​hresult}}{} }, nor yticklabel={ \pgfmathifisint{\pgfmathparse{exp(\tick)}\pgfmathresult}{\pgfmathparse{exp(\tick‌​)}\pgfmathprintnumber{\pgfmathresult}}{} }, work ? (and by work I mean compile) –  BigDawg Aug 26 '12 at 9:04
    
I think they are in the CVS version, right? Sorry, I did not have a look at the new additions yet. But even if it was working they will fail because the integer test is not reliable. That's why I've used tolerances. –  percusse Aug 26 '12 at 9:12
    
They are in the version 1.6.1, released on the 23rd of August. And also, another question, how can I format the x ticks label ? Here, the 1000 sep is clearly missing. Thanks a lot for your help –  BigDawg Aug 26 '12 at 9:14
    
@BigDawg It's not working because they are not the actual numbers but only labels (since this is a logarithmic axis). If you wish you can use another xticklabel={\pgfmathparse{...}} etc. –  percusse Aug 26 '12 at 9:28

The obvious solution woked for me. Just delete the "ytick=data" part. Have I misunderstood something?

\documentclass[10pt]{article}
\usepackage{tikz}
\usepackage{pgfplotstable}
\usepackage[active, pdftex, tightpage]{preview}
\PreviewEnvironment[{[]}]{tikzpicture}
\setlength\PreviewBorder{2mm}
\begin{filecontents}{Dkb0rjC9L.tex.dat}
x y
1 1
2 1.5
3 2
4 3
5 4
\end{filecontents}
\begin{document}
\begin{tikzpicture}
\begin{axis}[xtick=data]
\addplot table {Dkb0rjC9L.tex.dat};
\end{axis}
\end{tikzpicture}
\end{document}

graph

share|improve this answer
    
You can also pick specific ticks by using something like ytick={1,...,4}, –  Harish Kumar Aug 25 '12 at 23:19
    
Sorry, I over simplified the problem I was facing, I updated my first post, for which both solutions don't work. –  BigDawg Aug 26 '12 at 6:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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