Latex 简约图表?

Latex 简约图表?

假设我想要的是 2 个轴、轴上的任意点和一条曲线,如何在 latex 中实现?当我尝试使用 pgfplots 执行此操作时,它要么显示轴上的所有数字,要么根本不显示。我希望实现的效果类似于下图 在此处输入图片描述

答案1

欢迎来到 TeX.SE!!! 使用 Ti 可以更轻松地绘制此图在我看来,Z 比 pgfplots 更好。这是我的 TiZ 版本。

我声明了两个函数,line 和 parabola,这将有助于定位点的 y 坐标。剩下的就是绘图和标记。

这是代码:

\documentclass[tikz,border=2mm]{standalone}

% midpoint
\def\m{2}
% parabola and line
\tikzset{declare function={f(\x)=0.25*\x*\x-1*\x+1.75; g(\x)=0.5*\x+0.5;}}

\begin{document}
\begin{tikzpicture}[line cap=round,line join=round,scale=2]
% lines and labels
\draw[gray] (1,{g(1)}) -- (1,0) node [black,below] {\strut$x_1$};
\draw[gray] (5,{g(5)}) -- (5,0) node [black,below] {\strut$x_2$};
\draw[gray] (\m,0) node[black,below] {\strut$tx_1+(1-t)x_2$} |- (0,{f(\m)}) node[black,left] {$f\bigl(tx_1+(1-t)x_2\bigr)$};
\draw[gray] (\m,{f(\m)}) |- (0,{g(\m)}) node[black,left] {$tf(x_1)+(1-t)f(x_2)$};
% axes
\draw[-latex] (-0.5,0) -- (6,0);
\draw[-latex] (0,-0.5) -- (0,4);
% functions
\draw[thick] plot[domain=0.4:5.6,samples=41] (\x,{f(\x)}) node [right] {$f(x)$};
\draw[thick,magenta] (0.4,{g(0.4)}) -- (5.6,{g(5.6)});
\end{tikzpicture}
\end{document}

输出结果如下: 在此处输入图片描述

更新:与 pgfplots 相同的图片(输出几乎相同):

\documentclass[border=2mm]{standalone}
\usepackage               {pgfplots}
\pgfplotsset              {compat=1.17}

% parabola
\tikzset{declare function={
  m=2; % midpoint
  f(\x)=0.25*\x*\x-1*\x+1.75;
  g(\x)=0.5*\x+0.5;}}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[
    width=12cm,
    axis lines=middle,
    xmin=-0.5, xmax=6.5,
    ymin=-0.5, ymax=4.5,
    domain=0.4:5.6,
    samples=161, % you'll probably must play with the number of samples (see edit)
    xtick={1,m,5},
    xticklabels={\strut$x_1$,\strut$tx_1+(1-t)x_2$,\strut$x_2$},
    ytick={{f(m)},{g(m)}},
    yticklabels={$f\bigl(tx_1+(1-t)x_2\bigr)$,$tf(x_1)+(1-t)f(x_2)$},
  ]
% lines and labels
\addplot[gray] coordinates { (1,{g(1)}) (1,0)};
\addplot[gray] coordinates { (5,{g(5)}) (5,0)};
\addplot[gray] coordinates { (m,0) (m,{g(m)}) (0,{g(m)})};
\addplot[gray] coordinates { (m,{f(m)}) (0,{f(m)})};
% functions
\addplot[thick] {f(\x)} node [right] {$f(x)$};
\addplot[thick,magenta] coordinates {(0.4,{g(0.4)}) (5.6,{g(5.6)})};
\end{axis}
\end{tikzpicture}
\end{document}

答案2

通过使用pgfplots包和TiZ 库intersections,由其确定,但有一个例外(函数f(x)所有虚线坐标的最小值:

\documentclass[border=3.14152]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usetikzlibrary{intersections}


\begin{document}
    \begin{tikzpicture}[
lbl/.style = {font=\scriptsize, anchor=base},
                        ]
\begin{axis}[
declare function={f(\x)=0.2*(\x)^2 - 0.4*\x + 0.6; 
                  g(\x)=0.2*\x + 0.4;},
axis lines=center,
xlabel={$x$},   ylabel={$f(x)$},
label style = {anchor=north east},
xtick=\empty,   ytick=\empty,
xmin=0, xmax=3,
ymin=0,
domain=0.25:3,     samples=101, no marks,
clip=false
            ]
% functions
\addplot    +[name path=A] {f(x)} node [lbl, right] {$f(x)$};
\addplot    +[name path=B] {g(x)} node [lbl, right] {$g(x)$};
% dashed lines
\coordinate (O) at (0,0);   % origin
\coordinate (m) at (1,0);   % extrem of f(x)

\path [name path=C] (m) -- ++ (0,1);
%
\draw [name intersections={of=B and C, by={c}}, densely dashed] % <---
    (O |- c) node[lbl,left] {$tf(tx_1+(1-t)x_2)$}
            -| (O -| c) node[below=2ex, lbl] {$tx_1+(1-t)x_2$};
%
\draw [name intersections={of=A and B, by={a,b}}, densely dashed] % <---
    (O |- a) -| (O -| a) node[below=2ex, lbl] {$x_1$}
    (O |- b) -| (O -| b) node[below=2ex, lbl] {$x_2$};
%
\draw [name intersections={of=A and C, by={ac}}, densely dashed] % <---
    (ac) -- (ac -| O) node[lbl,left] {$f(tx_1+(1-t)x_2)$};
\end{axis}
    \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容