常规绘图

常规绘图

我想创建一个通用图表来说明一个点(例如,mx+b显示b截距和m斜率)。我知道我可以创建一个带有手动轴命名的图表,但我希望还能够b在截距处放置一个标签,标记几个点并标记m为斜率。我知道我可能必须输入不相关的整数值,但标签才是最重要的。

作为额外的奖励,我还想展示如何调整某些功能以及线条如何改变。(也许箭头上还有一个标签,但我忘了将其包含在草图中)

为了清晰起见,我在下面附上了几张总体思路的速写。我该如何实现这一点?

在此处输入图片描述

答案1

另一种选择是元帖子包裹在 中luamplib。用 编译它lualatex

点击上面的链接可以查看解释 MP 如何工作的教程和手册。

在此处输入图片描述

\documentclass[border=5mm]{standalone}
\usepackage{luamplib}
\begin{document}
\mplibtextextlabel{enable}
\begin{mplibcode}
beginfig(1);
    numeric u, m, m', b, b';
    u = 1.44cm;
    b = 3.6u;  b' = b + 7/8 u;
    m = -1;  m' = 7/8 m; 

    path xx, yy;
    xx = (left -- 5 right) scaled u;
    yy = xx rotated 90;

    numeric minx, maxx; path ff, gg;
    minx = xpart point 1/16 of xx;
    maxx = xpart point 15/16 of xx;
    ff = (minx, minx * m + b) -- (maxx, maxx * m + b); 
    gg = (minx, minx * m' + b') -- (maxx, maxx * m' + b'); 

    z0 = point 0.4 of ff;
    z1 = point 0.54 of ff;
    z1 0 = whatever [point 0 of gg, point 1 of gg]; x1 0 = x0;
    z1 1 = whatever [point 0 of gg, point 1 of gg]; x1 1 = x1;

    forsuffixes @=0, 1:
        draw (x@, 0) -- z@ -- (0, y@) dashed evenly scaled 3/4;
        draw z@ -- z1 @ -- (0, y1 @) dashed withdots scaled 1/2;
        label.bot("$x_{" & decimal @ & "}$", (x@, 0));
        label.lft("$y_{" & decimal @ & "}$", (0, y@));
        label.lft("$y'_{" & decimal @ & "}$", (0, y1 @));
    endfor

    draw ff withcolor 2/3 red;
    draw gg withcolor 3/4 blue;
    drawarrow xx; drawarrow yy;

    label.rt("$x$", point 1 of xx);
    label.top("$y$", point 1 of yy);

    dotlabel.urt("$b$", (0, b));
    dotlabel.urt("$b'$", (0, b'));

    draw thelabel("slope: $m=" & decimal m & "$", 7 up)
        rotated angle (1, m) shifted point 2/3 of ff;
    draw thelabel("slope: $m'=" & decimal m' & "$", 7 up)
        rotated angle (1, m') shifted point 2/3 of gg;
        
endfig;
\end{mplibcode}
\end{document}

获取点的语法y'有点棘手;但 MP 允许变量元素之间有空格,suffix因此z0 1对于变量来说它是一个有效的名称,而通常的z宏魔法意味着x0 1y0 1像往常一样引用 x 和 y 部分。

答案2

作为起点且仅适用于第一张图像。

\documentclass[margin=3mm]{standalone}
\usepackage{tikz}

\newcommand{\LinearEquation}
{%
\pgfmathsetmacro{\Slopef}{-1}% slope of the line 1
\pgfmathsetmacro{\Interceptf}{6}% intercept
\pgfmathsetmacro{\Slopes}{-0.9}% slope of the line 2
\pgfmathsetmacro{\Intercepts}{5}% intercept
\begin{tikzpicture}[>=latex]
\draw[->] (-1,0)--(8.3,0)node[below]{$x$};
\draw[->] (0,-1)--(0,8.3)node[left]{$y$};
\draw[very thick,red, domain=0:5] plot (\x,\Slopef*\x+\Interceptf);
\node at (0,\Interceptf)(b)[left]{$b$} ;
\def\x1{1.5}
\def\y1{\Slopef*\x1+\Interceptf}
\draw [dashed,blue](\x1,0)node[below]{$x1$}--(\x1,\y1)--(0,\y1)node[left]{$y1$};
\def\x2{3}
\def\y2{\Slopef*\x2+\Interceptf}
\draw [dashed,blue](\x2,0)node[below]{$x2$}--(\x2,\y2)--(0,\y2)node[left]{$y2^\prime$};

\draw[very thick,red, domain=0:5] plot (\x,\Slopes*\x+\Intercepts);
\node at (0,\Intercepts)(b)[left]{$b$} ;
\def\x1{1.5}
\def\y1{\Slopes*\x1+\Intercepts}
\draw [dashed,blue](\x1,0)node[below]{$x1$}--(\x1,\y1)--(0,\y1)node[left]{$y1^\prime$};
\def\x2{3}
\def\y2{\Slopes*\x2+\Intercepts}
\draw [dashed,blue](\x2,0)node[below]{$x2$}--(\x2,\y2)--(0,\y2)node[left]{$y2$};
\draw [<-](2.8,3.5)--(5,3.5)node[right]{Slope $m$};
\draw [<-](2.9,2.5)--(5,2.5)node[right]{Slope $m^\prime$};
\end{tikzpicture}%
}

\begin{document}
\LinearEquation
\end{document}

在此处输入图片描述

相关内容