1

Here, I would like to add the secondary y-axis ticks labels and make them function of the primary y-axis ticks lables instead of specifying them manually.

For example, I need secondary labels to follow this pseudo-function:

secondary y tick label = primary y tick label * 100

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}
\begin{tikzpicture}

\begin{axis}[
xmin=0,xmax=10,
xtick={0,2,...,10},
xticklabels={
    ,
    x1,
    x2,
    x3,
    x4,
    }
]
\addplot+[
only marks,
error bars/.cd,
y dir=both,
y explicit,
]
table
[
y error plus=ey+,
y error minus=ey-,
]{  
    x       y       ey+     ey-
    2       0       .5      1
    4       0       0       0.5
    6       0       1       0
    8       0       0.5     0.5
};

\end{axis} 
\end{tikzpicture}

\end{document}

enter image description here

2

If you want a second y-axis in your graph you can add another axis on top of the existing one with only y axis. Therefore first will have x and y1 and second one only y2. Further information can be found in chapter 4.9 in pgfplots manual.

Please note that most probably there are better ways to achieve this, this is the first one I've thought of.

Edit:

As to having second axis labels as a math function of the first one: How about using a two variables varymax and varymin? Then you can specify their value as a function, therefore you wouldn't need to do it manually.

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}
\begin{tikzpicture}

\newcommand{\varymin}{-1.2} 
\newcommand{\varymax}{1.2}

\begin{axis}[
xmin=0,xmax=10,
ymin=\varymin,ymax=\varymax,
xtick={0,2,...,10},
xticklabels={
    ,
    x1,
    x2,
    x3,
    x4,
    }
]
\addplot+[
only marks,
error bars/.cd,
y dir=both,
y explicit,
]
table
[
y error plus=ey+,
y error minus=ey-,
]{  
    x       y       ey+     ey-
    2       0       .5      1
    4       0       0       0.5
    6       0       1       0
    8       0       0.5     0.5
};

\end{axis} 
\begin{axis}[
xmin=0,xmax=10,
ymin=\varymin*100,ymax=\varymax*100,
axis y line*=right,
axis x line=none,
ylabel=Second label]
\end{axis}
\end{tikzpicture}

\end{document}

Here is the result:

result

  • Thanks for the answer. However, as I said in my question, I need the labels to follow a mathematical operation instead of doing it manually. In this example, it is easy to do manually, but in my real problem with different y scales, it will not be handy. – Diaa Apr 20 '17 at 21:05
  • 1
    I've edited my post. My solution looks really "IT gruduate" level though. – SPQR211 Apr 20 '17 at 21:22

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.