3

Why does draw the circle like that? How do I best draw the circle?

enter image description here

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

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    axis equal=true,
    axis lines=middle, enlargelimits=false,
    xlabel={$x$},    ylabel={$y$},
%
    xlabel style={at={(ticklabel* cs:0.983)},anchor=west},
    ylabel style={at={(ticklabel* cs:1.03)}, anchor=north west, align=right},
%    
    tick style={},
%    
    x tick label style={below},
    y tick label style={left},
%    
    xmin=-10,          xmax=10,
    ymin=-12,          ymax=12,
    xtick={}, ytick={},
    xticklabels={},   yticklabels={},
%    
    after end axis/.code={
    \path(axis cs:0,0) 
    node [anchor=north west,yshift=0 cm, xshift=0 cm] {}
    node [anchor=north east,xshift=0 cm] {O};}]

%
\addplot[domain=-5:8,very thick,red,smooth] {4-x};
\addplot [domain=-10:10,,very thick, blue,samples=50,smooth] {4+sqrt(32-x^2)};
\addplot [domain=-10:10,,very thick, blue,samples=50,smooth] {4-sqrt(32-x^2)};

\end{axis}
\end{tikzpicture}   
\end{document}
3
  • 2
    you are plotting with only 50 samples and circles are nearly vertical at the end points so what output do you expect? why not use a circle shape that is designed to avoid such issues? Oct 16, 2021 at 10:38
  • Can you help me with what to use?@DavidCarlisle
    – cufcuf
    Oct 16, 2021 at 10:43
  • @Skillmon done it already Oct 16, 2021 at 10:44

2 Answers 2

4

You can use \draw directly to draw a circle inside your axis (like already suggested by @DavidCarlisle). The following code requires at least compat=1.11 (else you'll need to use axis cs:0,4 for the positioning and might have to adjust the circle radius), but works with the latest compat=1.18 as well.

(This is faster and more accurate compared to using some samples)

\documentclass[border=2pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}% at least 1.11

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    axis equal=true,
    axis lines=middle, enlargelimits=false,
    xlabel={$x$},    ylabel={$y$},
%
    xlabel style={at={(ticklabel* cs:0.983)},anchor=west},
    ylabel style={at={(ticklabel* cs:1.03)}, anchor=north west, align=right},
%    
    tick style={},
%    
    x tick label style={below},
    y tick label style={left},
%    
    xmin=-10,          xmax=10,
    ymin=-12,          ymax=12,
    xtick={}, ytick={},
    xticklabels={},   yticklabels={},
%    
    after end axis/.code={
    \path(axis cs:0,0) 
    node [anchor=north east,xshift=0 cm] {O};}]
  \addplot[domain=-5:8,very thick,red,smooth] {4-x};
  \draw[very thick,blue]
    (0,4) circle[radius=sqrt(32)];
\end{axis}
\end{tikzpicture}   
\end{document}

enter image description here

2
  • Understood thanks. So, how do I shorten the line to the left of the x-axis and below the y-axis?
    – cufcuf
    Oct 16, 2021 at 10:51
  • 2
    @cufcuf please don't extend your question like that, instead make sure that your original question has a clear scope, and if you have additional questions open a new thread for them. But to answer your question: Simply change your values for ymin and xmin in the options of axis.
    – Skillmon
    Oct 16, 2021 at 10:57
4

Using plain TikZ is not bad idea in such simple case. Scaling is explicit, and you can intuitively control it, as well as the length of x-axis, y-axis.

enter image description here

\documentclass[tikz,border=5mm]{standalone}
\begin{document}
\begin{tikzpicture}[scale=.4]
\draw[->] (-7,0)--(8,0) node[below]{$x$};   
\draw[->] (0,-3)--(0,11)  node[left]{$y$};
\path
(0,0) node[below left]{O}
(4,0) node[below]{$4$}
(0,4) node[right]{$4$}
;

% the circle x^2 + (y-4)^2 = 32 
\draw[thick,teal] (0,4) circle({sqrt(32)});     

% the line y=4-x from x=-6 to x=6
\draw[thick,magenta] (-6,10)--(6,-2);   
\end{tikzpicture}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .