考虑以下简单的 PV 图:
下面的代码创建了它,输出很好。我使用了一种变通方法,即使用数组来获取两个轴上的正确刻度标签。这对于低值来说很好,但如果我想设置V_f = 200
,那么获取正确的刻度标签就会有点问题。
有什么方法可以正确获取刻度标签?tikz
每次实际打印刻度时,我是否可以告诉它使用数组的“下一个”元素?我可以想到一种解决方案,仅在打印时增加计数器,但肯定有解决方案吗tikz
?
\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\usetikzlibrary{arrows,arrows.meta, decorations.markings,backgrounds, intersections}
\makeatletter
\tikzset{
nomorepostaction/.code=\makeatletter\let\tikz@postactions\pgfutil@empty,
my axis/.style={
postaction={
decoration={
markings,
mark=at position 1 with {
\arrow[ultra thick]{latex}
}
},
decorate,
nomorepostaction
},
thin,
-, % switch off other arrow tips
every path/.append style=my axis % this is necessary so it works both with "axis lines=left" and "axis lines=middle"
}
}
\makeatother
\begin{document}
\begin{figure}
\def\xt{{0,"$V_i=1$",0,"$V_f=3$"}}\relax
\def\yt{{0,"$P_i=1$",0,"$P_f=3$"}}\relax
\begin{tikzpicture}
\begin{axis}[
axis lines=left,
width=.95\linewidth,
axis line style={my axis},
xmin = 0, xmax = 4,
ymin = 0, ymax = 4,
xticklabel={\pgfmathparse{\xt[\tick]}\pgfmathresult},
xtick={1,3},
yticklabel={\pgfmathparse{\yt[\tick]}\pgfmathresult},
ytick={1,3},
xlabel={Volume},
ylabel={Pressure}
]
\path[name path=axisa] (axis cs:\pgfkeysvalueof{/pgfplots/xmin},1) -- (axis cs:\pgfkeysvalueof{/pgfplots/xmax},1);
\path[name path=axisb] (axis cs:\pgfkeysvalueof{/pgfplots/xmin},3) -- (axis cs:\pgfkeysvalueof{/pgfplots/xmax},3);
\addplot[name path=graph, black,very thick, -latex ] coordinates
{(1,1) (3,3)}
;
\path[name intersections={of=axisa and graph, by={interpa}}];
\path[name intersections={of=axisb and graph, by={interpb}}];
\draw[ultra thin, dashed] (axis cs:0,1) -- (interpa);
\draw[ultra thin, dashed] (axis cs:1,0) -- (interpa);
\draw[ultra thin, dashed] (axis cs:0,3) -- (interpb);
\draw[ultra thin, dashed] (axis cs:3,0) -- (interpb);
\end{axis}
\end{tikzpicture}
\caption{}
\end{figure}
\end{document}
答案1
您在寻找xticklabels
andyticklabels
选项吗?列表中的刻度标签xticklabels
被分配给列表给出的刻度位置xtick
。
例子:
xtick={10,30},
xticklabels={$V_i=10$,$V_f=30$},
代码
\documentclass[margin=10pt]{standalone}
\usepackage{pgfplots} % loads also tikz
\pgfplotsset{compat=1.10}
\usetikzlibrary{arrows,arrows.meta}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines=left,
axis line style=-{Latex[scale=1.5]},
width=.95\linewidth,
xmin = 0, xmax = 40,
ymin = 0, ymax = 40,
xtick={10,30},
xticklabels={$V_i=10$,$V_f=30$},
ytick={10,30},
yticklabels={$P_i=10$,$P_f=30$},
xlabel={Volume},
ylabel={Pressure}
]
\addplot[very thick,-latex ] coordinates{(10,10) (30,30)}
coordinate[at start](interpa)coordinate[at end](interpb);
\pgfplotsinvokeforeach {a,b}{
\draw[ultra thin, dashed]
({axis cs:0,0}|-interp#1)--(interp#1)--(interp#1|-{axis cs:0,0});}
\end{axis}
\end{tikzpicture}
\end{document}