我目前在 tikz 中有两个图。MWE 如下:
\documentclass[10pt]{standalone}
% PACKAGES LOADING
\usepackage{tikz} % To plot almost everything.
\usepackage{pst-3d, tikz-3dplot} % To draw in 3D.
% FIGURES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\begin{tikzpicture}[my plot/.style={thick, smooth, samples=100, domain=0.1:5}, my grid/.style={densely dotted,opacity=0.45, every node/.style={black,opacity=1}}, my axis/.style={latex-latex}]
\draw[my plot] (0,0) plot (\x,{ln(\x)});
\coordinate (start plot) at (0.1,{ln(0.1)});
\coordinate (end plot) at (5,{ln(5)});
\draw[my axis] ([shift={(-0.5cm,0.5cm)}]start plot |- end plot) node[above] {$u(\cdot)$} |- node[coordinate](origin){} ([shift={(0.5cm,-0.5cm)}]start plot -| end plot) node[right] {$\cdot$};
\def\x{0.5}\def\y{4}\def\p{0.55}
\coordinate (Ux) at (\x,{ln(\x)});
\coordinate (Uy) at (\y,{ln(\y)});
\coordinate (Up) at ({\p*\x+(1-\p)*\y},{ln(\p*\x+(1-\p)*\y)});
\draw (Ux) -- coordinate[pos=1-\p] (Up-mid) (Uy);
\path let \p1=(Up-mid), \n1={pow(e,\y1*0.03514)} in (28.4576*\n1,\y1) coordinate (Up-mid2);
\draw[my grid] (Ux) |- node[below]{$x$} (origin) |- node[left]{$u(x)$} cycle;
\draw[my grid] (Uy) |- node[below]{$y$} (origin) |- node[left]{$u(y)$} cycle;
\draw[my grid] (Up) |- node[below, yshift=1.4pt]{$px+(1-p)y$} (origin) |- node[left]{$u(px+(1-p)y)$} cycle;
\draw[my grid] (Up-mid) |- (origin) |- node[left]{$pu(x)+(1-p)u(y)$} cycle;
\draw[my grid] (Up-mid) -- (Up-mid2);
\end{tikzpicture}
\begin{tikzpicture}[my plot/.style={thick, smooth, samples=100, domain=0.1:5.5}, my grid/.style={densely dotted,opacity=0.5, every node/.style={black,opacity=1}}, my axis/.style={latex-latex}]
\draw[my plot] (0,0) plot (\x,{(\x)});
\coordinate (start plot) at (0,{(0)});
\coordinate (end plot) at (5.5,{(5.5)});
\draw[my axis] ([shift={(-0.5cm,0.5cm)}]start plot |- end plot) node[above] {$u(\cdot)$} |- node[coordinate](origin){} ([shift={(0.5cm,-0.5cm)}]start plot -| end plot) node[right] {$\cdot$};
\def\x{0.5}\def\y{4}\def\p{0.55}
\coordinate (Ux) at (\x,{(\x)});
\coordinate (Uy) at (\y,{(\y)});
\coordinate (Up) at ({\p*\x+(1-\p)*\y},{(\p*\x+(1-\p)*\y)});
\draw (Ux) -- coordinate[pos=1-\p] (Up-mid) (Uy);
\path let \p1=(Up-mid), \n1={pow(e,\y1*0.03514)} in (28.4576*\n1,\y1) coordinate (Up-mid2);
\draw[my grid] (Ux) |- node[below,]{$x$} (origin) |- node[left]{$u(x)$} cycle;
\draw[my grid] (Uy) |- node[below]{$y$} (origin) |- node[left]{$u(y)$} cycle;
\draw[my grid] (Up) |- node[below, yshift=1.4pt] {$px+(1-p)y$} (origin) |- node[align=right,xshift=-55.0pt]{$pu(x)+(1-p)u(y)=$\\$=u(px+(1-p)y)$} cycle;
\end{tikzpicture}
\end{document}
从输出中可以看出,两个图的轴长度不同。我该怎么做才能确保两个图中的 y 轴和 x 轴具有相同的长度?
感谢大家的时间。
答案1
并不是pgfplots
那么难:
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\usetikzlibrary{arrows.meta,
intersections,
positioning}
\pgfplotsset{width=6cm, height=6cm, % <---
axis lines=middle,
xlabel={$\cdot$},
xlabel style={anchor=west},
ylabel={$u(\cdot)$},
ylabel style={anchor=south},
xtick=\empty, ytick=\empty,
clip=false
}
\tikzset{
ddline/.append style = {draw=gray, densely dotted},
tick/.style = {inner sep=2pt, font=\tiny, align=right}
}
\begin{document}
\begin{figure}[ht]
\begin{tikzpicture}[baseline]
\begin{axis}[
xmin = 0,
domain = 0.1:5,
samples = 100
]
\addplot [thick] {ln(\x)};
%
\draw[ddline] (0.5,0) node[tick,above] {$x$} |- coordinate[pos=0.5] (ux)
(0,{ln(0.5)}) node[tick,left] {$u(x)$};
\draw[ddline, name path=B]
(2,0) node[tick,below] {$px+(1-p)y$} |-
(0,{ln(2)}) node[tick,left] {$p(u)x+(1-p)u(y)$};
\draw[ddline] (4,0) node[tick,below] {$y$} |- coordinate[pos=0.5] (uy)
(0,{ln(4)}) node[tick,left] {$u(y)$};
\draw[name path=A, semitransparent] (ux) -- (uy);
\draw[name intersections={of=A and B, by=s}, ddline]
(s) -- (s -| 0,0) node[tick,left] {$u(px+(1-p)y)$};
\end{axis}
\end{tikzpicture}%
\hfill
\begin{tikzpicture}[baseline]
\begin{axis}[
xmin=0, ymin=0
]
\addplot [thick] coordinates {(0.1,0.1) (5,5)};
%
\draw[ddline]
(0.5,0) node[tick,below] {$x$} |- (0,0.5) node[tick,left] {$u(x)$}
(2.0,0) node[tick,below] {$px+(1-p)y$} |- (0,2.0) node[tick,left] {$p(u)x+(1-p)u(y)=$\\
$=u(px+(1-p)y)$}
(4.0,0) node[tick,below] {$y$} |- (0,4.0) node[tick,left] {$u(y)$};
\end{axis}
\end{tikzpicture}
\end{figure}
or
\begin{figure}[ht]
\begin{tikzpicture}[baseline]
\begin{axis}[
axis x line shift={-ln(0.1)}, % <---
xmin = 0,
domain = 0.1:5,
samples = 100
]
\addplot [thick] {ln(\x)};
%
\draw[ddline]
(0.5,{ln(0.1)}) node[tick,above] {$x$} |- coordinate[pos=0.5] (ux)
(0,{ln(0.5)}) node[tick,left] {$u(x)$};
\draw[ddline, name path=B]
(2,{ln(0.1)}) node[tick,below] {$px+(1-p)y$} |-
(0,{ln(2)}) node[tick,left] {$p(u)x+(1-p)u(y)$};
\draw[ddline]
(4,{ln(0.1)}) node[tick,below] {$y$} |- coordinate[pos=0.5] (uy)
(0,{ln(4)}) node[tick,left] {$u(y)$};
\draw[name path=A, semitransparent] (ux) -- (uy);
\draw[name intersections={of=A and B, by=s}, ddline]
(s) -- (s -| 0,0) node[tick,left] {$u(px+(1-p)y)$};
\end{axis}
\end{tikzpicture}%
\hfill
\begin{tikzpicture}[baseline]
\begin{axis}[
xmin=0, ymin=0
]
\addplot [thick] coordinates {(0.1,0.1) (5,5)};
%
\draw[ddline]
(0.5,0) node[tick,below] {$x$} |- (0,0.5) node[tick,left] {$u(x)$}
(2.0,0) node[tick,below] {$px+(1-p)y$} |- (0,2.0) node[tick,left] {$p(u)x+(1-p)u(y)=$\\
$=u(px+(1-p)y)$}
(4.0,0) node[tick,below] {$y$} |- (0,4.0) node[tick,left] {$u(y)$};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}