![曲线之间的阴影区域](https://linux22.com/image/289196/%E6%9B%B2%E7%BA%BF%E4%B9%8B%E9%97%B4%E7%9A%84%E9%98%B4%E5%BD%B1%E5%8C%BA%E5%9F%9F.png)
我是一个老 LaTeX 用户,但刚刚开始接触 PGF 和 TikZ。我试图在 3 条曲线之间着色,从我读到的内容来看,我可以使用 min 和 max 函数来实现,但不知何故,编译器不喜欢我的方程式。所以我尝试使用 shadeareabetween 函数,\usepgfplotslibrary{fillbetween}
但编译器也不喜欢写LaTeX或 Mac 编译器。所以我不得不手动从一条曲线中减去另一条曲线,但随后我必须将下部区域涂成白色,这会破坏我的轴刻度。你们能帮我解决这个问题吗?也许可以给我展示一些技巧,让这段代码更短。我基本上是在绘制 2014 年 8 美分的 3 个通货膨胀率。
代码:
\documentclass{article}
\usepackage{pgfplots,tikz}
\pgfplotsset{compat=newest}
\usetikzlibrary{intersections}
\usepackage{ wasysym }
\pagestyle{empty}
\usetikzlibrary{decorations.markings}
\begin{document}
\begin{tikzpicture}[scale =1.25,label/.style={%
postaction={ decorate,transform shape,
decoration={ markings, mark=at position .39 with \node #1;}}}]
\begin{axis}[
width=\textwidth,
legend cell align=left,
legend pos=outer north east,
legend style={draw=none},
%height=9cm,
xlabel=Year,
ylabel=\$/kWh,
xmin=2004, xmax=2050,
ymin=5, ymax=35,
xticklabel style={/pgf/number format/1000 sep=},
%width=9cm,
xtick={2005, 2010, 2013, 2020, 2030, 2040,2050},
xticklabels={05,10,13,2020,2030,2040,2050},
ytick={6,8,10,15,...,30},
]
%\addplot {8*1.08^x};
%\addlegendentry{8\%}
\addplot[ color=black,
solid,
mark=*,
mark options={solid},
smooth] coordinates {
(2005,6)
(2006,7.32)
(2007,9.42)
(2008,10.29)
(2009,8.09)
(2010,6.73)
(2011,9.60)
(2012,9.62)
(2013,8.66)
(2014,8)
};
\addlegendentry{Retail}
\addplot+[mark=none,draw=none,fill=orange!70,domain=2014:2050] {8*1.07^(x-2014)}\closedcycle;
\addplot+[mark=none,draw=none,fill=white,domain=2014:2050] {8*1.03^(x-2014)}\closedcycle;
\addplot[name path global=T,color=black,line width=0.75,dashed,domain=2013.75:2050,label={[below]{3\%}}]{8.*(1.03)^( x - 2014 )};
%\addlegendentry{3\% inflation}
\addplot[label={[below]{5\%}},name path global = F,color=black,line width=0.75,dashed,domain=2013.85:2048,]{8.*(1.05)^( x - 2014 )};
%\addlegendentry{5\% inflation}
\addplot[label={[below]{7\%}},name path global=S,color=black,line width=0.75,dashed,domain=2013.85:2050]{8.*(1.07)^( x - 2014 )};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
axis on top
使用适合环境的选项axis
:
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots,tikz}
\pgfplotsset{compat=newest}
\usetikzlibrary{intersections}
\usepackage{ wasysym }
\pagestyle{empty}
\usetikzlibrary{decorations.markings}
\begin{document}
\begin{tikzpicture}[scale =1.25,label/.style={%
postaction={ decorate,transform shape,
decoration={ markings, mark=at position .39 with \node #1;}}}]
\begin{axis}[
axis on top,
width=\textwidth,
legend cell align=left,
legend pos=outer north east,
legend style={draw=none},
%height=9cm,
xlabel=Year,
ylabel=\$/kWh,
xmin=2004, xmax=2050,
ymin=5, ymax=35,
xticklabel style={/pgf/number format/1000 sep=},
%width=9cm,
xtick={2005, 2010, 2013, 2020, 2030, 2040,2050},
xticklabels={05,10,13,2020,2030,2040,2050},
ytick={6,8,10,15,...,30},
]
%\addplot {8*1.08^x};
%\addlegendentry{8\%}
\addplot[ color=black,
solid,
mark=*,
mark options={solid},
smooth] coordinates {
(2005,6)
(2006,7.32)
(2007,9.42)
(2008,10.29)
(2009,8.09)
(2010,6.73)
(2011,9.60)
(2012,9.62)
(2013,8.66)
(2014,8)
};
\addlegendentry{Retail}
\addplot+[mark=none,draw=none,fill=orange!70,domain=2014:2050] {8*1.07^(x-2014)}\closedcycle;
\addplot+[mark=none,draw=none,fill=white,domain=2014:2050] {8*1.03^(x-2014)}\closedcycle;
\addplot[name path global=T,color=black,line width=0.75,dashed,domain=2013.75:2050,label={[below]{3\%}}]{8.*(1.03)^( x - 2014 )};
%\addlegendentry{3\% inflation}
\addplot[label={[below]{5\%}},name path global = F,color=black,line width=0.75,dashed,domain=2013.85:2048,]{8.*(1.05)^( x - 2014 )};
%\addlegendentry{5\% inflation}
\addplot[label={[below]{7\%}},name path global=S,color=black,line width=0.75,dashed,domain=2013.85:2050]{8.*(1.07)^( x - 2014 )};
\end{axis}
\end{tikzpicture}
\end{document}
我standalone
只是为了示例而将类更改为避免框过满(您的图像对于标准边距来说太宽)。
答案2
您也可以使用填充,但是该解决方案很脆弱。
\documentclass{article}
\usepackage{pgfplots,tikz}
\pgfplotsset{compat=newest}
\usetikzlibrary{intersections}
\usepackage{ wasysym }
\pagestyle{empty}
\usetikzlibrary{decorations.markings}
\usepgfplotslibrary{fillbetween}% note addition
\begin{document}
\noindent
\begin{tikzpicture}[label/.style={%
postaction={ decorate,transform shape,
decoration={ markings, mark=at position .2 with \node #1;}}}]
\begin{axis}[
width=\textwidth,
legend cell align=left,
legend pos=outer north east,
legend style={draw=none},
%height=9cm,
xlabel=Year,
ylabel=\$/kWh,
xmin=2004, xmax=2050,
ymin=5, ymax=35,
%xticklabel style={/pgf/number format/1000 sep=},
%width=9cm,
xtick={2005, 2010, 2013, 2020, 2030, 2040,2050},
xticklabels={05,10,13,2020,2030,2040,2050},
ytick={6,8,10,15,...,30},
]
%\addplot {8*1.08^x};
%\addlegendentry{8\%}
\addplot[ color=black,
solid,
mark=*,
mark options={solid},
smooth] coordinates {
(2005,6)
(2006,7.32)
(2007,9.42)
(2008,10.29)
(2009,8.09)
(2010,6.73)
(2011,9.60)
(2012,9.62)
(2013,8.66)
(2014,8)
};
\addlegendentry{Retail}
\addplot[name path=S,draw=none,mark=none,domain=2013.85:2050] {8.*(1.07)^( x - 2014 )};
%\addplot[name path=F,draw=none,mark=none,domain=2013.85:2050] {8.*(1.05)^( x - 2014 )};
\addplot[name path=T,draw=none,mark=none,domain=2013.85:2050] {8.*(1.03)^( x - 2014 )};
\addplot[orange] fill between[of=S and T];
\addplot[color=black,line width=0.75,dashed,label={[below]{7\%}},domain=2013.85:2050] {8.*(1.07)^( x - 2014 )};
\addplot[color=black,line width=0.75,dashed,label={[below]{5\%}},domain=2013.85:2050] {8.*(1.05)^( x - 2014 )};
\addplot[color=black,line width=0.75,dashed,label={[below]{3\%}},domain=2013.85:2050] {8.*(1.03)^( x - 2014 )};
\end{axis}
\end{tikzpicture}
\end{document}