我有一个 TikZ 图形,其中一个轴有点拥挤。我的解决方案是使用
extra x ticks={...},
extra x tick label={...},
extra x style={tick label style={yshift=...}},
但是,我只能用这种方法添加第二行。我知道一个选项就是在所需位置添加一个带有一些文本的节点,但我想知道是否有使用轴选项的解决方案。我的代码不起作用:
\documentclass[10pt,a4paper]{article}
\usepackage{tikz,pgfplots}
\usetikzlibrary{intersections}
\pgfplotsset{compat=1.14}
\begin{document}
\begin{tikzpicture}
% Define parameters
\newcommand*{\DELTAb}{0.25}%
\newcommand*{\DELTAa}{0.4}%
\pgfmathsetmacro{\LOWB}{1-\DELTAb}%
\pgfmathsetmacro{\HIGHB}{1+\DELTAb}%
\pgfmathsetmacro{\LOWA}{1-\DELTAa}%
\pgfmathsetmacro{\HIGHA}{1+\DELTAa}%
\newcommand*{\ALPHA}{0.5}%
\begin{axis}[
% Define axis properties
axis x line=bottom,
axis y line=left,
enlargelimits=false,
clip=false,
ytick=\empty,%{1},
ymin=0,
%ymax=1,
xtick={0, \LOWB, 1, \HIGHB},
xticklabels={$0$, $w-\Delta_b$, $w$, $w+\Delta_b$},
extra x ticks={0.9},
extra x tick labels={$w - \Delta_b(\delta_{b,2} - \delta_{b,1})$},
extra x tick style={tick label style={yshift=-4mm}},
extra x ticks={\LOWA, \HIGHA},
extra x tick labels={$w-\delta_b$, $w+\delta_b$},
extra x tick style={tick label style={yshift=-8mm}},
xmin=0.43,
xmax=1.8,
samples=250,
domain=0.55:1.6]
% Draw functions
\addplot[name path=A,smooth,thick,mark=none] {(1/x)^(1/(1-\ALPHA))} node[right,pos=1,yshift=-0.1cm] {$f(w_i)=C{w_i}^{\frac{-\rho}{1-\rho}}$};
% Find intersections
\path[name path=C] (\pgfkeysvalueof{/pgfplots/xmin},0) -- (\pgfkeysvalueof{/pgfplots/xmax},0); % X axis named
\path[name path=D] (0,\pgfkeysvalueof{/pgfplots/ymin}) -- (0,\pgfkeysvalueof{/pgfplots/ymax}); % Y axis named
\path[name path=vline0] (1,0) -- (1,5); %vertical line1 in w
\path[name path=vline1] (\LOWB,0) -- (\LOWB,5); %vertical line in -delta
\path[name path=vline2] (\HIGHB,0) -- (\HIGHB,5); %vertical line in +delta
\path[name path=vline3] (\LOWA,0) -- (\LOWA,5); %vertical line in -delta
\path[name path=vline4] (\HIGHA,0) -- (\HIGHA,5); %vertical line in +delta
\path[name intersections={of=vline0 and A,name=v0A}]; % Create intersection at average
\path[name intersections={of=vline1 and A,name=v1A}]; % Create intersection-1
\path[name intersections={of=vline2 and A,name=v2A}]; % Create intersection-3
\path[name intersections={of=vline3 and A,name=v1B}]; % Create intersection-1
\path[name intersections={of=vline4 and A,name=v2B}]; % Create intersection-3
% Draw lines
\draw[dashed,thick,gray] (v1B-1) -- (\LOWA,0);
\draw[dashed,thick,gray] (v2B-1) -- (\HIGHA,0);
\draw[dashed,thick,gray] (v1A-1) -- (\LOWB,0);
\draw[dashed,thick,gray] (v2A-1) -- (\HIGHB,0);
\draw[dashed,thick,gray] (1, 0) -- (v0A-1);
% Draw averages
\path[draw,solid,red,name path=P1] (v2A-1) -- (v1A-1);
\path[name path=vline0b] (0.9,0) -- (0.9,5); %vertical line1 in w
\path[name intersections={of=vline0b and P1,name=v0Ab}]; % Create intersection below average
\draw[dashed,gray] (v0Ab-1-|{axis cs:0.43,0}) -- (v0Ab-1) node[left,black,pos=0] {$\lambda^{HET}_a$};
\draw[dashed,gray] (0.9,0) -- (v0Ab-1);
\path[draw,solid,red,name path=P2] (v2B-1) -- (v1B-1);
\end{axis}
\end{tikzpicture}
\end{document}
结果:
因此基本上,您不能有两组extra x ...
命令,因为最后一个命令会覆盖任何前一个命令。
答案1
你可以有一组额外的标签,并且有不同的班次,例如根据位置确定单独的刻度标签样式:
extra x ticks={0.9,\LOWA, \HIGHA},
extra x tick labels={{$w - \Delta_b(\delta_{b,2} - \delta_{b,1})$},$w-\delta_b$, $w+\delta_b$},
extra x tick style={tick label style={yshift={\ticknum == 0 ? "-4mm" : "-8mm"}}},
是<comparison> ? <value1> : <value2>
的简写ifthenelse
,您也可以使用更明显的ifthenelse(\ticknum==0,"-4mm","-8mm")
。
在下面的代码中,我还建议使用一种不太冗长的方法来制作该图表,即定义一个函数并使用一个ycomb
图而不是计算所有这些交点。
\documentclass[10pt,a4paper]{article}
\usepackage{pgfplots}
\usetikzlibrary{intersections}
\pgfplotsset{compat=1.14}
\begin{document}
\begin{tikzpicture}
% Define parameters
\newcommand*{\DELTAb}{0.25}%
\newcommand*{\DELTAa}{0.4}%
\pgfmathsetmacro{\LOWB}{1-\DELTAb}%
\pgfmathsetmacro{\HIGHB}{1+\DELTAb}%
\pgfmathsetmacro{\LOWA}{1-\DELTAa}%
\pgfmathsetmacro{\HIGHA}{1+\DELTAa}%
\newcommand*{\ALPHA}{0.5}%
\begin{axis}[
% Define axis properties
axis x line=bottom,
axis y line=left,
enlargelimits=false,
clip=false,
ytick=\empty,
ymin=0,
xtick={0, \LOWB, 1, \HIGHB},
xticklabels={$0$, $w-\Delta_b$, $w$, $w+\Delta_b$},
extra x ticks={0.9,\LOWA, \HIGHA},
extra x tick labels={{$w - \Delta_b(\delta_{b,2} - \delta_{b,1})$},$w-\delta_b$, $w+\delta_b$},
extra x tick style={tick label style={yshift={ifthenelse(\ticknum==0,"-4mm","-8mm")}}},
xmin=0.43,
xmax=1.8,
samples=50,
domain=0.55:1.6,
declare function={f(\x)=(1/\x)^(1/(1-\ALPHA));}
]
% Draw functions
\addplot[name path=A,smooth,thick,mark=none] {f(x)}
node[right,yshift=-0.1cm] {$f(w_i)=C{w_i}^{\frac{-\rho}{1-\rho}}$};
\addplot [ycomb,mark=none,dashed,thick,gray,samples at={1,\LOWB,\HIGHB,\LOWA,\HIGHA}] {f(x)};
\draw [red] (\LOWA,{f(\LOWA)}) -- (\HIGHA,{f(\HIGHA)});
\draw [red,name path=A] (\LOWB,{f(\LOWB)}) -- (\HIGHB,{f(\HIGHB)});
\path [name path=B] (0.9,\pgfkeysvalueof{/pgfplots/ymin}) -- (0.9,\pgfkeysvalueof{/pgfplots/ymax});
%
\draw [
name intersections={of=A and B,name=I},
thick,dashed,gray
]
(0.9,\pgfkeysvalueof{/pgfplots/ymin}) -- (I-1) -- (\pgfkeysvalueof{/pgfplots/xmin},0 |- I-1) node[left,black] {$\lambda^{HET}_a$};
\end{axis}
\end{tikzpicture}
\end{document}