![如何使用 pgfPlots 显示图中所有点的坐标?](https://linux22.com/image/433807/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8%20pgfPlots%20%E6%98%BE%E7%A4%BA%E5%9B%BE%E4%B8%AD%E6%89%80%E6%9C%89%E7%82%B9%E7%9A%84%E5%9D%90%E6%A0%87%EF%BC%9F.png)
我想显示用于绘制曲线的所有点的坐标。为此,我手动为所有点创建了单独的节点,有没有更好的方法?我查看了 tikz-pgf 的文档,但找不到解决方案。
以下是剧情:
\documentclass[12pt,a4paper]{article}
\usepackage[a4paper,margin=2cm]{geometry}
\usepackage{pgfplots,tikz,siunitx,float,amsfonts,amsmath,amssymb}
\pgfplotsset{compat=1.16}
\usetikzlibrary{intersections, positioning, calc, math}
\begin{document}
\begin{figure}[htb]
\centering
\begin{tikzpicture}
\begin{axis}[
axis lines=left, thick, grid=both,
width=0.8\textwidth, height=0.8\textwidth,
xmin=0,xmax=110, ymin=0,ymax=150, y=0.06cm,
xlabel={Upper Limits}, ylabel={Cumulative Frequency}
]
\addplot[color = black,smooth,very thick,mark=*,name path=h1] coordinates {
(10,3)(20,7)(30,19)(40,37)(50,57)(60,82)(70,94)(80,102)(90,108)(100,110)
};
\legend{Less-Than type Ogive Curve}
\draw (10,3) node [above]{$(10,3)$};
\draw (20,7) node [above]{$(20,7)$};
\draw (30,19) node [above]{$(30,19)$};
\draw (40,37) node [above]{$(40,37)$};
\draw (50,57) node [above]{$(50,57)$};
\draw (60,82) node [above]{$(60,82)$};
\draw (70,94) node [right]{$(70,94)$};
\draw (80,102) node [above]{$(80,102)$};
\draw (90,108) node [below]{$(90,108)$};
\draw (100,110) node [above]{$(100,110)$};
\end{axis}
\end{tikzpicture}
\caption{Less-Than type Ogive Curve.}
\end{figure}
\end{document}
答案1
通过使用,nodes near coords
您可以获得非常接近所需结果的图:
\documentclass{standalone}
\usepackage{pgfplots,tikz}
\pgfplotsset{compat=1.16}
\usetikzlibrary{intersections, positioning, calc, math}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines=left, thick, grid=both,
width=0.8\textwidth, height=0.8\textwidth,
xmin=0,xmax=110, ymin=0,ymax=150, y=0.06cm,
xlabel={Upper Limits},
ylabel={Cumulative Frequency},
nodes near coords=,
]
\addplot[color = black,smooth,very thick,mark=*,name path=h1, scatter] coordinates {
(10,3)(20,7)(30,19)(40,37)(50,57)(60,82)(70,94)(80,102)(90,108)(100,110)
};
\legend{Less-Than type Ogive Curve}
\end{axis}
\end{tikzpicture}
您可以使用参数自定义创建的节点的内容nodes near coords
:
nodes near coords={
$(\pgfmathprintnumber{\pgfkeysvalueof{/data point/x}},
\pgfmathprintnumber{\pgfkeysvalueof{/data point/y}})$
},
其中键/data point/x
和/data point/y
允许您访问数据点,并且命令\pgfmathprintnumber
格式化浮点数。