1

I'm just getting to grasp with plots and tikzpictures on latex. I can draw lines and functions now and manipulate them how i want, but i need to plot a circle.

\begin{tikzpicture}
\begin{axis}[
    axis lines = left,
    xlabel = $x$,
    ylabel = {$y$},
]
\addplot [
    domain=-4:4, 
    samples=10, 
    color=red,
]
{2*x+1};
\addlegendentry{$2x+1$}

\end{axis}
\end{tikzpicture}

This is what i'm using for the line, and i need to add a circle on there.

Zarko
  • 296,517
Kay
  • 11
  • 1
    \addplot[only marks,mark=o,red,samples at={...}] ... where the first ... lists the position of your circle(s) and the second ... is as above in your code. –  Oct 16 '18 at 16:26

1 Answers1

1

One way to graph a circle would be to graph the upper and lower portions separately. For the circle of radius 3 centered at the origin you can use:

\addplot [domain=-3:3, blue] {sqrt(9-x^2)};
\addplot [domain=-3:3, blue] {-sqrt(9-x^2)};

enter image description here

Notes:

  • Need to use axis equal=true to ensure that the scale used on the x-axis is the same as that on the y-axis. Otherwise, the circle will look like an ellipse.

  • I shifted the placement of the legend by setting legend style.

  • You can also use parametric equations if you prefer:

      \addplot[domain=-180:180, My Style, blue] ({3*cos(x)},{3*sin(x)});
    

    where the domain is in degrees as that is how the pgfplots trignometric function arguments are expected.

Code:

\documentclass[border=2pt]{standalone}
\usepackage{pgfplots}

\tikzset{My Style/.style={samples=100, thick}}

\begin{document} \begin{tikzpicture} \begin{axis}[ axis lines = middle, xlabel = $x$, ylabel = {$y$}, axis equal=true, legend style={ cells={anchor=west}, legend pos=north west }, ]

\addplot [domain=-4:4, My Style, red] {2*x+1}; \addlegendentry{$y=2x+1$}

\addplot [domain=-3:3, My Style, blue] {sqrt(9-x^2)}; \addplot [domain=-3:3, My Style, blue] {-sqrt(9-x^2)}; \addlegendentry{$x^2+y^2=9$}

\end{axis} \end{tikzpicture} \end{document}

Peter Grill
  • 223,288