使用以下代码,我经过反复试验,手动绘制线条以定位刻度线的坐标。有没有办法将刻度线位置作为参考来绘制。
\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{frame}[fragile,t]
\frametitle{1}
\begin{tikzpicture}[scale=.9, transform shape]
\begin{axis}[ axis lines=center,axis line style={black, thick,-latex},
axis y line=left,axis x line=bottom,
tick style={line width=.04cm, color=black, line cap=round},
font=\normalsize,color=black,
xmin=0,xmax=64,
xtick={0,16,32},xticklabels={0,16,32},
ymin=0,ymax=16,
ytick={4,8},yticklabels={4,8},
tickwidth=.2cm,
xlabel={P}, xlabel style={right},
ylabel={M}, ylabel style={above},
xticklabel style={inner xsep=0cm, xshift=0cm,yshift=-.1cm},
yticklabel style={inner ysep=0cm,xshift=-.1cm,yshift=0cm},
samples=200]
\end{axis}
\draw [ultra thick,blue] (0,2.84) -- +(-40:4.44cm) node [pos=.5,circle, draw, black, fill=black, scale=0.5]{} node [pos=0,circle, draw, black, fill=black, scale=0.5]{} node [pos=1,circle, draw, black, fill=black, scale=0.5]{};
\draw [thick, black, densely dotted] (1.7128,1.424) node [black, xshift=.46cm, yshift=.05cm] {A} -- (0,1.424);
\draw [thick, black, densely dotted] (1.7128,1.424) -- (1.7128,0);
\end{tikzpicture}
\end{frame}
\end{document}
还有其他点的位置与刻度线位置相关;如下图所示
答案1
如果你画里面环境axis
,您可以使用axis cs:
。为了显示它适用于任意函数,我添加了一个“复杂”函数。请注意,我clip=false
在轴上添加了选项以防止标记被剪裁。
\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{tikz}
\usepackage{pgfplots}
\tikzset{
declare function={
% myslope2(\x) = 24 - \x*(24/48);
myslope(\x) = 8 - \x*(8/32);
mycomplexfunction(\x) = sin(60*pow(\x,1.2)) + 6;
},
}
\begin{document}
\begin{frame}[fragile,t]
\frametitle{1}
\begin{tikzpicture}[scale=.9, transform shape]
\begin{axis}[ axis lines=center,axis line style={black, thick,-latex},
axis y line=left,axis x line=bottom,
tick style={line width=.04cm, color=black, line cap=round},
font=\normalsize,color=black,
xmin=0,xmax=64,
xtick={0,16,32},xticklabels={0,16,32},
ymin=0,ymax=16,
ytick={4,8},yticklabels={4,8},
tickwidth=.2cm,
xlabel={P}, xlabel style={right},
ylabel={M}, ylabel style={above},
xticklabel style={inner xsep=0cm, xshift=0cm,yshift=-.1cm},
yticklabel style={inner ysep=0cm,xshift=-.1cm,yshift=0cm},
samples=200,
clip=false]
\draw[ultra thick,blue] plot[samples at={0,16,32},mark=*,mark options={black}] (axis cs:\x,{myslope(\x)});
\draw[thick,black,densely dotted] (axis cs:0,{myslope(16)}) -- (axis cs:16,{myslope(16)}) node [black, xshift=.46cm, yshift=.05cm] {A} -- (axis cs:16,0);
\draw[thin,red] plot[domain=0.1:32,smooth,samples=200] (axis cs:\x,{mycomplexfunction(\x)});
\draw[thin,red,densely dotted] (axis cs:0,{mycomplexfunction(20)}) -- (axis cs:20,{mycomplexfunction(20)}) -- (axis cs:20,0);
\end{axis}
\end{tikzpicture}
\end{frame}
\end{document}
它看起来像这样:
对于您展示的示例图像,您需要函数myslope2(\x) = 24 - \x*(24/48);
。只需按照与上述相同的方式添加样本即可。
编辑
正如@TorbjørnT 在评论中指出的那样,使用\pgfplotset{compat=1.11}
或更高版本可以让axis cs
成为命令中轴内的默认坐标系\draw
。此外,如果您使用\addplot
而不是\draw plot
,则标记在突出轴区域时不会被剪裁,因此您不再需要该clip=false
选项。\addplot
还有一些额外的优点,例如添加图例条目,\addlegendentry{}
如以下示例所示:
\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\tikzset{
declare function={
% myslope2(\x) = 24 - \x*(24/48);
myslope(\x) = 8 - \x*(8/32);
mycomplexfunction(\x) = sin(60*pow(\x,1.3)) + 6;
},
}
\begin{document}
\begin{frame}[fragile,t]
\frametitle{1}
\begin{tikzpicture}[scale=.9, transform shape]
\begin{axis}[
axis lines=center,
axis line style={black, thick,-latex},
axis y line=left,axis x line=bottom,
tick style={line width=.04cm, color=black, line cap=round},
font=\normalsize,
color=black,
xmin=0,
xmax=64,
xtick={0,16,32},
xticklabels={0,16,32},
ymin=0,
ymax=16,
ytick={4,8},
yticklabels={4,8},
tickwidth=.2cm,
xlabel={P},
xlabel style={right},
ylabel={M},
ylabel style={above},
xticklabel style={inner xsep=0cm, xshift=0cm,yshift=-.1cm},
yticklabel style={inner ysep=0cm,xshift=-.1cm,yshift=0cm},
samples=200
]
\addplot[ultra thick,blue,samples at={0,16,32},mark=*,mark options={black}] {myslope(x)};
\addlegendentry{Simple slope};
\draw[thick,black,densely dotted] (0,{myslope(16)}) -- (16,{myslope(16)}) node [black, xshift=.46cm, yshift=.05cm] {A} -- (16,0);
\addplot[thin,red,samples=200,smooth,domain=0:32] {mycomplexfunction(x)};
\addlegendentry{Complex function};
\draw[thin,red,densely dotted] (0,{mycomplexfunction(20)}) -- (20,{mycomplexfunction(20)}) -- (20,0);
\end{axis}
\end{tikzpicture}
\end{frame}
\end{document}
答案2
是的。你只需要说
\draw [thick, black, densely dotted] (A -| 0,0) -- (A) -- (A |- 0,0) ;
完成 MWE。
\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{frame}[fragile,t]
\frametitle{1}
\begin{tikzpicture}[scale=.9, transform shape]
\begin{axis}[ axis lines=center,axis line style={black, thick,-latex},
axis y line=left,axis x line=bottom,
tick style={line width=.04cm, color=black, line cap=round},
font=\normalsize,color=black,
xmin=0,xmax=64,
xtick={0,16,32},xticklabels={0,16,32},
ymin=0,ymax=16,
ytick={4,8},yticklabels={4,8},
tickwidth=.2cm,
xlabel={P}, xlabel style={below},
ylabel={M}, ylabel style={left},
xticklabel style={inner xsep=0cm, xshift=0cm,yshift=-.1cm},
yticklabel style={inner ysep=0cm,xshift=-.1cm,yshift=0cm},
samples=200]
\end{axis}
\draw [ultra thick,blue] (0,2.84) -- +(-40:4.44cm)
node [pos=.5,circle, draw, black, fill=black, scale=0.5,label=right:A] (A){}
node [pos=0,circle, draw, black, fill=black, scale=0.5]{}
node [pos=1,circle, draw, black, fill=black, scale=0.5]{};
\draw [thick, black, densely dotted] (A -| 0,0) -- (A)
-- (A |- 0,0) ;
\end{tikzpicture}
\end{frame}
\end{document}