我想绘制带填充区域的图。我尝试了此代码
\documentclass[a4paper,12pt]{report}
\usepackage{pgfplots}
\definecolor{plotmain}{HTML}{97bbcd}
\pgfplotsset{every axis legend/.append style={at={(0.5,-0.17)},anchor=north,legend cell align=left}}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[
xlabel={x},
ylabel={y},
ymin=2.56286,
height=9cm,
width=15cm,
grid=major,
]
\addplot[color=plotmain, draw=none,fill, fill opacity = 0.5,thick] coordinates {
(-4.77778,2.56286)
(-4.77778,27.60977)
(-3.55556,3.84069)
(-2.33333,2.58953)
(-1.11111,4.50066)
(0.11111,6.66082)
(1.33333,2.56286)
(2.55556,3.40638)
(3.77778,19.24780)
(5.00000,39.56775)
(5.00000,2.56286)
};
\addplot[color=plotmain, mark=*,mark options={plotmain,opacity = 1, fill=white,thick},thick] coordinates {
(-4.77778,27.60977)
(-3.55556,3.84069)
(-2.33333,2.58953)
(-1.11111,4.50066)
(0.11111,6.66082)
(1.33333,2.56286)
(2.55556,3.40638)
(3.77778,19.24780)
(5.00000,39.56775)
};
\addlegendentry{estimate}
\end{axis}
\end{tikzpicture}
\caption{A larger example}
\end{figure}
\end{document}
我看到这个结果: 我的图例有问题。第一个图的线没有标记。我该如何修复它?
更新。
如果我使用\closedcycle
线条,它们会以循环方式出现。它们看起来像是主线的延续。如果我使用负数,图表会在零处画一条线。
如果我使用第二种解决方案(改变顺序)并尝试添加两个(当前的四个)图例,则会画出错误的线。
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[
xlabel={x},
ylabel={y},
ymax=42,
xmax=6.5,
height=9cm,
width=15cm,
grid=major,
axis lines=left,
]
\addplot[color=plotmain, mark=*,mark options={plotmain,opacity = 1, fill=white,thick},thick] coordinates {
(-4.77778,27.60977)
(-3.55556,3.84069)
(-2.33333,2.58953)
(-1.11111,4.50066)
(0.11111,6.66082)
(1.33333,2.56286)
(2.55556,3.40638)
(3.77778,19.24780)
(5.00000,39.56775)
};
\addlegendentry{estimate}
\addplot[color=plotmain, draw=none,fill, fill opacity = 0.5,thick] coordinates {
(-4.77778,2.56286)
(-4.77778,27.60977)
(-3.55556,3.84069)
(-2.33333,2.58953)
(-1.11111,4.50066)
(0.11111,6.66082)
(1.33333,2.56286)
(2.55556,3.40638)
(3.77778,19.24780)
(5.00000,39.56775)
(5.00000,2.56286)
};
\addplot[color=red, mark=*,mark options={red,opacity = 1, fill=white,thick},thick] coordinates {
(-2,20)
(2.,3)
(5,10)
};
\addlegendentry{yueu}
\addplot[color=red, draw=none,fill, fill opacity = 0.5,thick] coordinates {
(-2,2.56286)
(-2,20)
(2.,3)
(5,10)
(5,2.56286)
};
\end{axis}
\end{tikzpicture}
\caption{A larger example}
\end{figure}
答案1
不必使用\addplot
两次。改用\closedcycle
。
\documentclass[a4paper,12pt]{report}
\usepackage{pgfplots}
\definecolor{plotmain}{HTML}{97bbcd}
\pgfplotsset{every axis legend/.append style={at={(0.5,-0.17)},anchor=north,legend cell align=left}}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[
xlabel={x},
ylabel={y},
ymin=2.56286,
height=9cm,
width=15cm,
grid=major,
]
\addplot[color=plotmain, mark=*,mark options={plotmain,opacity = 1, fill=white,thick},fill,fill opacity = 0.5,thick] coordinates {
(-4.77778,27.60977)
(-3.55556,3.84069)
(-2.33333,2.58953)
(-1.11111,4.50066)
(0.11111,6.66082)
(1.33333,2.56286)
(2.55556,3.40638)
(3.77778,19.24780)
(5.00000,39.56775)
}\closedcycle;
\addlegendentry{estimate}
\end{axis}
\end{tikzpicture}
\caption{A larger example}
\end{figure}
\end{document}
如果您仍然坚持使用addplot
两次,请更改顺序:
\documentclass[a4paper,12pt]{report}
\usepackage{pgfplots}
\definecolor{plotmain}{HTML}{97bbcd}
\pgfplotsset{every axis legend/.append style={at={(0.5,-0.17)},anchor=north,legend cell align=left}}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[
xlabel={x},
ylabel={y},
ymin=2.56286,
height=9cm,
width=15cm,
grid=major,
]
\addplot[color=plotmain, mark=*,mark options={plotmain,opacity = 1, fill=white,thick},thick] coordinates {
(-4.77778,27.60977)
(-3.55556,3.84069)
(-2.33333,2.58953)
(-1.11111,4.50066)
(0.11111,6.66082)
(1.33333,2.56286)
(2.55556,3.40638)
(3.77778,19.24780)
(5.00000,39.56775)
};
\addlegendentry{estimate}
\addplot[color=plotmain, draw=none,fill, fill opacity = 0.5,thick] coordinates {
(-4.77778,2.56286)
(-4.77778,27.60977)
(-3.55556,3.84069)
(-2.33333,2.58953)
(-1.11111,4.50066)
(0.11111,6.66082)
(1.33333,2.56286)
(2.55556,3.40638)
(3.77778,19.24780)
(5.00000,39.56775)
(5.00000,2.56286)
};
\end{axis}
\end{tikzpicture}
\caption{A larger example}
\end{figure}
\end{document}
编辑:对于更新的
\closedcycle
确实是一个循环,并且行为符合预期。对于第二个查询,请再次使用正确的顺序:
\documentclass[a4paper,12pt]{report}
\usepackage{pgfplots}
\definecolor{plotmain}{HTML}{97bbcd}
\pgfplotsset{every axis legend/.append style={at={(0.5,-0.17)},anchor=north,legend cell align=left}}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[
xlabel={x},
ylabel={y},
ymax=42,
xmax=6.5,
height=9cm,
width=15cm,
grid=major,
axis lines=left,
]
\addplot[color=plotmain, mark=*,mark options={plotmain,opacity = 1, fill=white,thick},thick] coordinates {
(-4.77778,27.60977)
(-3.55556,3.84069)
(-2.33333,2.58953)
(-1.11111,4.50066)
(0.11111,6.66082)
(1.33333,2.56286)
(2.55556,3.40638)
(3.77778,19.24780)
(5.00000,39.56775)
};
\addlegendentry{estimate}
\addplot[color=red, mark=*,mark options={red,opacity = 1, fill=white,thick},thick] coordinates {
(-2,20)
(2.,3)
(5,10)
};
\addlegendentry{yueu}
\addplot[color=plotmain, draw=none,fill, fill opacity = 0.5,thick] coordinates {
(-4.77778,2.56286)
(-4.77778,27.60977)
(-3.55556,3.84069)
(-2.33333,2.58953)
(-1.11111,4.50066)
(0.11111,6.66082)
(1.33333,2.56286)
(2.55556,3.40638)
(3.77778,19.24780)
(5.00000,39.56775)
(5.00000,2.56286)
};
\addplot[color=red, draw=none,fill, fill opacity = 0.5,thick] coordinates {
(-2,2.56286)
(-2,20)
(2.,3)
(5,10)
(5,2.56286)
};
\end{axis}
\end{tikzpicture}
\caption{A larger example}
\end{figure}
\end{document}