1

I often use plots with a second x/y-axis which is proportionally related to the first one. Therefore, I would like to calculate the tick-values by multiplication. Let's say the first one is a time and the second one a related distance, then I would like to have something like:

\begin{tikzpicture}

\begin{axis}[%
xmin=0,
xmax=1,
ymin=0*50,
ymax=1*50, % here it works
ytick = {0, .2, .4, .6, .8, 1} * 50, % this does not work
%ytick = {0, 10, 20, 30, 40, 50}, % this works, but it requires manual calculation
ylabel = {distance},
axis x line=none,
axis y line*=right,
]
\end{axis}

\begin{axis}[%
xmin = 0,
xmax = 1,
ymin=0,
ymax=1,
ytick = {0, .2, .4, .6, .8, 1},
ylabel={time},
]

\end{axis}

% add a plot here

\end{tikzpicture}%

I would like just to copy the ytick-values from the one axis to the second one and specify the factor. Is this possible somehow? Thank in advance.

0

1 Answer 1

2

The easy solution is to NOT specify the tick list. ymin and ymax will do.

In any case, one can construct a list of scaled values using a foreach loop.

\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{arrows.meta}

\newcommand{\yticklist}{}% reserve global name

\begin{document}

\begin{tikzpicture}

\let\yticklist=\empty% create scaled list
\foreach \y in {0, .2, .4, .6, .8, 1}
{
  \pgfmathmultiply{\y}{50}%
  \ifx\empty\yticklist\relax \xdef\yticklist{\pgfmathresult}%
  \else \xdef\yticklist{\yticklist,\pgfmathresult}%
  \fi
}
\begin{axis}[%
xmin=0,
xmax=1,
ymin=0*50,
ymax=1*50, % here it works
ytick/.expanded = {\yticklist},
axis x line=none,
axis y line=right,
y axis line style={-Butt Cap},
ylabel = {distance},
]
\end{axis}

\begin{axis}[%
xmin = 0,
xmax = 1,
ymin=0,
ymax=1,
ytick = {0, .2, .4, .6, .8, 1},
ylabel={time}
]

\end{axis}

% add a plot here

\end{tikzpicture}%

\end{document}

demo

2
  • "The easy solution is to NOT specify the tick list. ymin and ymax will do." -> what, if the factor is not 50 but, for example, 1.85? Then, pgfplots will rather choose different values which do not fit to the grid lines ... anyways, your solution works!
    – Binabik
    Commented Jun 30, 2016 at 15:13
  • I figured there must be some reason for specifying ytick, but wasn't sure. Commented Jun 30, 2016 at 15:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .