我经常使用具有第二个 x/y 轴的绘图,该轴与第一个轴成比例。因此,我想通过乘法来计算刻度值。假设第一个是时间,第二个是相关距离,那么我希望得到如下结果:
\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}%
我只想将 ytick 值从一个轴复制到第二个轴并指定因子。这可能吗?提前谢谢。
答案1
简单的解决方案是不指定勾选列表。 ymin
就ymax
可以了。
无论如何,都可以使用foreach
循环构建缩放值列表。
\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}