2
\begin{tikzpicture}
   \begin{axis}[
        axis x line = center,
        axis y line = center,
        xmin        = -3.0,
        xmax        =  5.0,
        ymin        = -3.0,
        ymax        =  5.0]
        \addplot[
            domain = 0.1:5.0,
        ] {1/x};
    \end{axis}

    \foreach \Point/\PointLabel in {(4,4)/x_0, (3,1)/B, (1,4)/P_1, (3,4)/P_1}
    \draw[fill=black] \Point circle (0.05) node[above right] {$\PointLabel$};
\end{tikzpicture}

The code above will generate the following graph:

enter image description here

I was trying to add the text x0 around the point (1,1) in the axis environment. But it seems that the coordinate system between the axis environment and the tikzpicture is different.

Could you help me with explaining the environment misalignment problem?

And how to put the x0 label at the (1,1) coordinate under the axis environment in an elegant way?

Qrrbrbirlbel
  • 119,821
  • 2
    Inside the axis environment one can use \coordinate (A) at (axis cs: 4,4}; to define a global coordinate, then use \draw (A) etcoutside the axis environment. – John Kormylo May 24 '21 at 23:02
  • @JohnKormylo: Do not use axis cs: explicitly - it has been default since long time ago. – hpekristiansen Jan 22 '23 at 10:20
  • If you really really want to learn about the relationship between coordinates inside and outside the axis then there is chapter 4.26 in the manual or see e.g. https://tex.stackexchange.com/a/667195/8650 – hpekristiansen Jan 22 '23 at 10:31
  • @hpekristiansen, I don't trust defaults, especially ones which change. In C, after int was redefined from short to long, I stopped using int altogether. – John Kormylo Jan 22 '23 at 16:26
  • @JohnKormylo: The default for a specific compat level does not change. I can only say that you should not write it - it is a bad habit that is perpetuated by old posts on this site. – hpekristiansen Jan 22 '23 at 17:26
  • @hpekristiansen - Clarity is never a bad habit. – John Kormylo Jan 23 '23 at 04:15

2 Answers2

2

The points P_1, P_2, B and x_o˙I would draw wit separate addplot function with options only marks, nodes near coords, and point meta=explicit symbolic:

enter image description here

\documentclass[border=3.141592]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document} \begin{tikzpicture} \begin{axis}[ axis lines = center, xmin = -3.0, xmax = 5.0, ymin = -3.0, ymax = 5.0, ] % \addplot [domain = 0.1:5.0, samples=50] {1/x}; \addplot [only marks, mark=*, % <--- nodes near coords, % <--- point meta=explicit symbolic] % <--- coordinates {(1,1) [$x_0$] % given coordinate (1,-2)[$B$] % coordinate estimated from your picture (1,4) [$P_1$] % <--- (-2,4)[$P_1$]}; % <--- \end{axis} \end{tikzpicture} \end{document}

Used coordinates (except for x_0) I estimated from your image in question. If needed, correct their positions as you like to have.

Zarko
  • 296,517
1

The better methode is in answer by @Zarko. You can also just create your points as you are doing, but inside axis. The only thing is that you can not use foreach in that way. If you need a loop, you need to use \pgfplotsinvokeforeach and friends - see manual chapter 8.1.

\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines=center,
xmin=-3.0, xmax=5.0,
ymin=-3.0, ymax=5.0,
]
\addplot[domain=0.1:5.0, samples=50, smooth] {1/x};
\fill (1,1) circle[radius=0.05] node[above right] {$x_0$};
\end{axis}
\end{tikzpicture}
\end{document}

Graph with curve and a single point