pgfplot 基本线性绘图问题

pgfplot 基本线性绘图问题

我正在尝试绘制类似于下图的简单图表

示例图

到目前为止,我已经弄清楚了如何使用下面的方法绘制一些基本图形,但不知道如何绘制坐标和 y 截距以及直线

\documentclass[fleqn]{article}
\usepackage[left=1in, right=1in, top=1in, bottom=1in]{geometry}
\usepackage{mathexam}
\usepackage{amsmath}
\usepackage{pgfplots}

\ExamHead{\today}

\let\ds\displaystyle

\begin{document}
\ExamInstrBox{
Please show \textbf{all} your work! Answers without supporting work will not be marked. Write answers in spaces provided. You have 1 hour to complete this exam.}
\ExamNameLine
\begin{enumerate}
   \item Find the equation of these lines
    \begin{enumerate}
       \item \begin{tikzpicture}
            \begin{axis}[xlabel = x, ylabel = y, axis lines=center, axis on top=true]
            \addplot [mark=none,draw=black,ultra thick] {1/2*x+3};
            \end{axis}
            \end{tikzpicture}
            \answer
        \item \begin{tikzpicture}
            \begin{axis}[xlabel = x, ylabel = y, axis lines=center, axis on top=true]
            \addplot [mark=none,draw=black,ultra thick] {3*x-1};
            \end{axis}
            \end{tikzpicture}
            \answer

    \end{enumerate}
\end{enumerate}
\end{document}

答案1

稍微修改了一下@PeterGrill 的回答(+1):

在此处输入图片描述

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

\begin{document}
    \begin{tikzpicture}
\begin{axis}[
    axis lines=middle, axis on top,
    xlabel = $x$, ylabel = $y$,
    x label style={anchor=west},
    y label style={anchor=south},
    axis equal,
    xtick=\empty,   ytick=\empty,           % <---
    extra x ticks=0,
    x tick label style={anchor=north east}, % <---
    xmin=-3.5, xmax=5.5,                    % <---
    ymin=-0.5, ymax=5.5,                    % <---
            ]
\addplot [red,domain=-3:5,very thick, <->] {1/2*x+3};
\addplot [only marks] coordinates {(4,1/2*4+3)} node[above left] {(2,4)};
\addplot [only marks] coordinates {(0,1/2*0+3)} node[above left] {(0,3)};
\end{axis}
    \end{tikzpicture}
\end{document}

答案2

您可以使用\addplot coordinates将点放置在所需的点:

\addplot [draw=none, mark=*] coordinates{(4,5)} node [left] {(4,5)};

在此处输入图片描述

笔记

代码:

\documentclass[fleqn]{article}
\usepackage[left=1in, right=1in, top=1in, bottom=1in]{geometry}
\usepackage{mathexam}
\usepackage{amsmath}
\usepackage{pgfplots}
\pagecolor{white}

\ExamHead{\today}

\let\ds\displaystyle

\begin{document}
\ExamInstrBox{
Please show \textbf{all} your work! Answers without supporting work will not be marked. Write answers in spaces provided. You have 1 hour to complete this exam.}
\ExamNameLine
\begin{enumerate}
   \item Find the equation of these lines
    \begin{enumerate}
       \item \begin{tikzpicture}
            \begin{axis}[xlabel = $x$, ylabel = $y$, axis lines=center, axis on top=true]
            \addplot [mark=none,draw=black,ultra thick] {1/2*x+3};
            \addplot [draw=none, mark=*] coordinates{(4,5)}
                node [above left] {(4,5)};
            \end{axis}
            \end{tikzpicture}
            \answer
        \item \begin{tikzpicture}
            \begin{axis}[xlabel = $x$, ylabel = $y$, axis lines=center, axis on top=true]
            \addplot [mark=none,draw=black,ultra thick] {3*x-1};
            \end{axis}
            \end{tikzpicture}
            \answer

    \end{enumerate}
\end{enumerate}
\end{document}

相关内容