我想在 latex 中复制以下图表。我使用的是 pgfplots 包。我遇到的主要问题是如何实现带标签的垂直线、如何使 y 轴上的刻度标记绝对(即没有负数)以及如何在图表下方显示图例。另外,忽略任何斜率差异,我的数据与图中的数据不等同。
以下是代码:
\begin{tikzpicture}
\begin{axis}[
axis x line=middle,
axis y line=middle,
axis on top,
smooth,
xlabel = $x$,
ylabel = {$f(x)$},
width=0.5\textwidth,
height=0.5\textwidth,
every axis plot/.append style={ultra thick}
]
\addplot [
domain=200:207,
samples=100,
color=red,
]
{80};
\addplot [
domain=207:220,
samples=100,
color=red,
]
{-80/13*x+17600/13};
\addplot [
domain=220:250,
samples=100,
color=red,
]
{0};
\addplot [
domain=250:265,
samples=100,
color=red,
]
{-16/3*x+4000/3};
\end{axis}
\end{tikzpicture}
答案1
这会使 y 轴为正数并添加垂直线。有很多方法可以使输出更具吸引力。
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}
\begin{axis}[%axis lines=middle,
axis x line=middle,
axis y line=left,
axis on top,
smooth,
xlabel = $x$,
%ylabel = {$f(x)$},
width=0.5\textwidth,
height=0.5\textwidth,
xmax=280,
every axis plot/.append style={ultra thick},
ytick={-80,-40,0,0,40,80},
yticklabel=$\pgfmathparse{abs(\tick)}\pgfmathprintnumber{\pgfmathresult}\,\%$
]
\addplot [
domain=200:207,
samples=100,
color=red,
]
{80};
\addplot [
domain=207:220,
samples=100,
color=red,
]
{-80/13*x+17600/13};
\addplot [
domain=220:250,
samples=100,
color=red,
]
{0};
\addplot [
domain=250:265,
samples=100,
color=red,
]
{-16/3*x+4000/3};
\pgfplotsinvokeforeach{207,220,250,265}{\path (#1,0) coordinate (p-#1);}
\end{axis}
\foreach \X [count=\Y] in {207,220,250,265}
{\draw (current axis.south-|p-\X) -- (current axis.north-|p-\X)
node[above,font=\sffamily]{v\Y};}
\end{tikzpicture}
\end{document}