我正在研究这个图表:
\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\clip (-0.1,-0.1) rectangle (5,3);
\draw[help lines,->] (0,0) -- (4.2,0);
\draw[help lines,->] (0,0) -- (0,3);% draw axis lines
\draw[gray,dashed] (0,2) -- (4.2,2); % draw asymptote
\draw[domain=0.1:4.6,very thick,red,->,samples=400] plot ({\x - 0.4},{1/(-\x) + 2} );% draw plot
\draw[help lines,->] (1,0) -- (1,1.2);
\draw[help lines,->] (2,0) -- (2,1.5);
\draw[help lines,->] (3,0) -- (3,1.65);
\draw[help lines,->] (4,0) -- (4,1.7);
% scale to fit marginfigure
\end{tikzpicture}
\end{document}
它或多或少完成了工作,但我想知道是否有更干净或更优雅的方式来完成它。具体来说,我想知道:
有没有办法将曲线绘制为端点为 (0,0) 的射线?绘制半双曲线然后进行裁剪的问题是我想标记该路径上的几个点(垂直箭头就是为此而设的)。如果我必须将图表裁剪得如此紧密,我就无法做到这一点。
有没有办法描述垂直线相对于曲线图的高度(即,我可以说
tikz
“从(1,0)画一条带箭头的垂直线,直到它与曲线相交”)?
答案1
如果我理解正确的话,这就是你想要的:
\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\clip (-0.1,-0.1) rectangle (5,3);
\draw[help lines,->] (0,0) -- (4.2,0);
\draw[help lines,->] (0,0) -- (0,3); % draw axis lines
\draw[gray,dashed] (0,2) -- (4.2,2); % draw asymptote
\draw[domain=0.5:4.6,very thick,red,->,samples=400] plot ({\x - 0.5},{1/(-\x) + 2} );% draw plot
\foreach \x in {1,2,3,4}
{%%
\draw[help lines,->] (\x,0) -- ($(\x,{1/(-(\x+0.5)) +2})-(0,0.6pt)$);
}%%
\end{tikzpicture}
\end{document}
使用tikzlibrary
calc
可以让您考虑红色曲线的粗细(因此偏移量0.6pt
)。
您可以编写一个宏来处理 y 坐标:
\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\def\mycurve#1{{1/(-(\x+#1))+2}}
\begin{document}
\begin{tikzpicture}
\clip (-0.1,-0.1) rectangle (5,3);
\draw[help lines,->] (0,0) -- (4.2,0);
\draw[help lines,->] (0,0) -- (0,3); % draw axis lines
\draw[gray,dashed] (0,2) -- (4.2,2); % draw asymptote
\draw[domain=0.5:4.6,very thick,red,->,samples=400] plot ({\x - 0.5},\mycurve{0} );% draw plot
\foreach \x in {0.25,0.5,...,4}
{
\draw[help lines,->] (\x,0) -- ($(\x,\mycurve{0.5})-(0,0.6pt)$);
}
\end{tikzpicture}
\end{document}
答案2
该解决方案建议找到交点,然后绘制箭头线。需要intersections
库tikz
。每行代码后面都有详细说明。
代码
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{intersections}
\begin{document}
\begin{tikzpicture}[scale=2]
\clip (-\pgflinewidth,-\pgflinewidth) rectangle (5,3);
\draw[help lines,->] (0,0) -- (4.2,0);
\draw[help lines,->] (0,0) -- (0,3); % draw axis lines
\draw[gray,dashed] (0,2) -- (4.2,2); % draw asymptote
\draw[name path=curve,domain=0.1:4.6,very thick,red,->,samples=400] plot ({\x - 0.5},{1/(-\x) + 2} );
% draw plot
\foreach \l in {0.25,0.5,...,4}{ % locations for vertical lines
\path[name path global=l\l, help lines,->] (\l,0) -- (\l,2); % draw path
\fill [name intersections={of=curve and l\l, name=i, total=\t}] % to find
[red, opacity=1, every node/.style={above left, black, opacity=1}] % intersections
\foreach \s in {1,...,\t}{(i-\s) circle (0.3pt)}; % and label it
\draw[->] (\l,0)--(i-1); % then draw arrows
}
\end{tikzpicture}
\end{document}
答案3
我会选择pgfplots
这个。绘制完成后,您可以在每个想要的位置放置坐标(我手动设置,但您可以使用 foreach 列表语法填充更多)。然后,您可以根据角或绘图原点绘制垂直线和水平线。为了缩短箭头,我使用了shorten <
键。对于渐近线,我使用了一条显式线,但您也可以使用机制extra y ticks
。
如果您不希望它从实际零点开始,而是从与轴相交的点开始,请删除xmin=0
或调整域以从开始0.4
。
\documentclass[]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\begin{document}
\begin{tikzpicture}
\begin{axis}[axis x line=bottom,axis y line=left,
xtick=\empty,ytick=\empty,enlarge y limits={upper,value=0.2},xmin=0,ymin=0,
]
\addplot[domain=0.1:4.6,very thick,red,->,samples=100] ({\x - 0.4},{1/(-\x) + 2} )
\foreach\t[count=\tx] in{0.95,0.87,...,0.5}{coordinate[pos=\t] (c\tx)};
\pgfplotsinvokeforeach{1,...,4}{\draw[<-,shorten <=2mm] (c#1) -- (c#1|-{axis cs:0,0});}
\draw[dashed,blue,ultra thick] (axis cs:0,1.9)
-- ({axis cs:0,1.9} -| {axis description cs:1,0});
\end{axis}
\end{tikzpicture}
\end{document}
答案4
以下是使用元帖子。
代码
\documentclass[border=5mm]{standalone}
\usepackage[shellescape]{gmp}
\begin{document}
\begin{mpost}
% define suitable a point
z1 = (233,89);
% define the path representing the function
path f; f = origin { dir 75 } .. { dir 2 } z1;
% draw the function line with a fat red arrow
drawarrow f withpen pencircle scaled 1 withcolor .67 red;
% clip the arrow at the bottom left so it does not stick out below the axes
clip currentpicture to unitsquare xscaled 1.2x1 yscaled 1.2y1;
% draw the axes
drawarrow origin -- (x1,0);
drawarrow origin -- (0,1.5y1);
% draw the dashed line
draw (0,1.05y1) -- (x1,1.05y1) dashed evenly;
% draw the marker arrows
for i=1 upto 4:
x := 0.95x1*i/4;
drawarrow (x,0) -- (x,y1) cutafter (f shifted 2 down);
endfor
\end{mpost}
\end{document}
笔记
Metapost 允许您通过在花括号中指定每个节点的方向来定义弯曲路径。因此,
{dir 75}
路径以高于水平 75° 的角度开始。如果您使用符号定义一个点
z$
,其中$
是任何有效的后缀,您可以免费获得x$
和定义。y$
构造函数返回与路径 相交后被切断的
p cutafter q
路径。在这里,我将相交路径向下移动了一点,以便箭头远离红线。p
q
您需要使用
-shellescape
选项运行它,如gmp
文档中所述。