![有以下 tikz 图,它过去运行良好,但现在不起作用,请有人能帮助我吗?](https://linux22.com/image/402850/%E6%9C%89%E4%BB%A5%E4%B8%8B%20tikz%20%E5%9B%BE%EF%BC%8C%E5%AE%83%E8%BF%87%E5%8E%BB%E8%BF%90%E8%A1%8C%E8%89%AF%E5%A5%BD%EF%BC%8C%E4%BD%86%E7%8E%B0%E5%9C%A8%E4%B8%8D%E8%B5%B7%E4%BD%9C%E7%94%A8%EF%BC%8C%E8%AF%B7%E6%9C%89%E4%BA%BA%E8%83%BD%E5%B8%AE%E5%8A%A9%E6%88%91%E5%90%97%EF%BC%9F.png)
\documentclass[10pt,a4paper]{book}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{tikz}
\usepackage{pgf}
\usepackage{pgf}
\begin{document}
\begin{figure}[h!]
\centering
\begin{tikzpicture}[scale=1]
\begin{axis}[xlabel=x ,ylabel=y,xtick={0,1,...,10} ,grid=major,ytick={-20,-10,...,60}]
\addplot[no marks, smooth] coordinates{
(0.5, -7.75)
(1. , -9.)
(1.5, -9.75)
(2. , -10.)
(2.5 , -9.75)
(3. , -9.)
(3.5 , -7.75)
(4. , -6.)
(4.5 , -3.75)
(5. , -1.)
(5.5 , 2.25)
(6. , 6.)
(6.5 , 10.25)
(7. , 15.)
(7.5 , 20.25)
(8. , 26.)
(8.5 , 32.25)
(9. , 39.)
(9.5 , 46.25)
(10. , 54.)
\sqrt{} };
\addplot[only marks, red!50!black] coordinates{
(0.5, -7.75)
(1. , -9.)
(1.5, -9.75)
(2. , -10.)
(2.5 , -9.75)
(3. , -9.)
(3.5 , -7.75)
(4. , -6.)
(4.5 , -3.75)
(5. , -1.)
(5.5 , 2.25)
(6. , 6.)
(6.5 , 10.25)
(7. , 15.)
(7.5 , 20.25)
(8. , 26.)
(8.5 , 32.25)
(9. , 39.)
(9.5 , 46.25)
(10. , 54.)
};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}
答案1
您的 MWE 有两个问题:
- 为了进行绘图,
pgfplots
您需要在序言中加载包pgfplots
(错过了,所以这个 MWE 从未起作用) - 在坐标系中添加
\sqrt{}
未知的坐标pgfplots
,因此您的 MWE 更新工作也会随之添加
修复两者并仅考虑图片所需的包,在pgfplots
1.11 之后的任何版本中工作的 MWE 是:
\documentclass[10pt,a4paper]{book}
\usepackage{pgfplots} % <---
\pgfplotsset{compat=1.16} % <---
\begin{document}
\begin{figure}[ht]
\centering
\begin{tikzpicture}
\begin{axis}[
grid=major,
xlabel=x,
xtick={0,1,...,10} ,
ylabel=y,
ytick={-20,-10,...,60}
]
\addplot[no marks, smooth] coordinates{
(0.5,-7.75) (1,-9.) (1.5,-9.75) (2,-10) (2.5,-9.75)
(3,-9) (3.5,-7.75) (4 ,-6.) (4.5,-3.75) (5 ,-1.)
(5.5,2.25) (6, 6) (6.5,10.25) (7, 15) (7.5,20.25)
(8,26) (8.5,32.25) (9,39) (9.5,46.25) (10,54)
}; % removed \sqrt{}
\addplot[only marks, red!50!black] coordinates{
(0.5,-7.75) (1,-9.) (1.5,-9.75) (2,-10.) (2.5,-9.75)
(3,-9) (3.5,-7.75) (4,-6) (4.5,-3.75) (5,-1)
(5.5,2.25) (6,6) (6.5,10.25) (7,15) (7.5,20.25)
(8,26) (8.5,32.25) (9,39) (9.5,46.25) (10,54)
};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}
结果:
不清楚为什么要分别绘制曲线和标记。使用以下命令可以获得相同的结果:
\documentclass[10pt,a4paper]{book}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{figure}[ht]
\centering
\begin{tikzpicture}
\begin{axis}[
grid=major,
xlabel=x,
xtick={0,1,...,10} ,
ylabel=y,
ytick={-20,-10,...,60}
]
\addplot[mark=*, mark options={fill=red!50!black}, smooth] % <---
coordinates{
(0.5,-7.75) (1,-9.) (1.5,-9.75) (2,-10) (2.5,-9.75)
(3,-9) (3.5,-7.75) (4 ,-6.) (4.5,-3.75) (5 ,-1.)
(5.5,2.25) (6, 6) (6.5,10.25) (7, 15) (7.5,20.25)
(8,26) (8.5,32.25) (9,39) (9.5,46.25) (10,54)
};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}