我需要将图表第一行和第二行之间的区域涂成绿色,将图表第二行和第三行之间的区域涂成红色:我尝试使用 来做到这一点fillbetween
,因为稍后我必须使用曲线来实现这一点。我看了看这个问题,但我完全不明白它的soft clip
作用是什么,而且说实话,我有点迷茫了……我提供了一个我想要的示例,这是我用 独特编码的图tikz
。第二张图片是我使用 卡住的地方pgfplot
。
编辑:我有点明白了,但我仍然有轴也是彩色的问题,在这里我无法在绘制函数之前填充路径......
\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{width=9cm,compat=1.10}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{figure}[t]
\centering
\begin{tikzpicture}
\begin{axis}[
title = {$\delta = 0.9$},
xlabel = {$\xi$},
ylabel = {$c(\xi)$},
xmin = 0,
xmax = 1,
ymin = 0.17,
ymax= 1,
]
\addplot[
name path = A,
domain= 0:1,
samples= 100,
line width = 1.2 pt,
]
{(1+x)/2};
\node at (axis cs: .8, .84){\footnotesize$C(\xi)$};
\addplot[
name path = B,
domain= 0:1,
samples= 100,
style = dashed,
]
{(1/4 + (0.9 +x)/2};
\node at (axis cs: 0.2,0.87){\footnotesize$\bar{c}(\xi;\delta)$};
\addplot[
name path = C,
domain= 0:1,
samples= 100,
style = dashed,]
{(3/4 + (-0.9 +x)/2};
\node at (axis cs: .8, .63){\footnotesize$\underline{c}(\xi;\delta)$};
\addplot[green!20] fill between [of = A and C, soft clip={domain=0:1}];
\addplot[red!20] fill between [of = B and A, soft clip={domain=0:1}];
\end{axis}
\end{tikzpicture}
\label{fig:SustProf}
\caption{Sustainability of the ABS with a competitive fringe of retailers for fixed b.}
\end{figure}
\end{document}
答案1
我不确定这是否是你想要实现的,但如果你想要fill between
在特定位置剪切创建的彩色区域X-values,那么你应该把这些X-values 作为domain
选项键的值soft clip
,例如soft clip = {domain = 0:0.6}
(表示该区域从X= 0 并结束于X= 0.6)。关键域的值采用两个数字(以冒号分隔:
),表示X-values 创建的区域fill between
应该从哪里开始以及应该在哪儿结束。
我删除了samples = 100
,因为对于一条直线来说,你不需要那么多样本。这只会增加编译时间。此外,如果你加载pgfplots
,则无需额外加载tikz
或xcolor
包(后者将由以及tikz
包加载pgfplots
)。
要在彩色区域上方放置刻度标记,请使用axis on top
以下选项axis
:
\documentclass[12pt]{article}
\usepackage{pgfplots}
\pgfplotsset{width=9cm, compat=1.10}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{figure}[t]
\centering
\begin{tikzpicture}
\begin{axis}[
title = {$\delta = 0.9$},
xlabel = {$\xi$},
ylabel = {$c(\xi)$},
xmin = 0,
xmax = 1,
ymin = 0.17,
ymax = 1,
axis on top
]
\addplot[
name path = A,
domain = 0:1,
line width = 1.2 pt,
]
{(1+x)/2};
\node at (axis cs: .8, .84){\footnotesize$C(\xi)$};
\addplot[
name path = B,
domain = 0:1,
style = dashed,
]
{(1/4 + (0.9 +x)/2};
\node at (axis cs: .2, .87){\footnotesize$\bar{c}(\xi;\delta)$};
\addplot[
name path = C,
domain = 0:1,
style = dashed,]
{(3/4 + (-0.9 +x)/2};
\node at (axis cs: .8, .63){\footnotesize$\underline{c}(\xi;\delta)$};
\addplot[green!20] fill between [of = A and C, soft clip = {domain = 0:0.6}];
\addplot[red!20] fill between [of = B and A, soft clip = {domain = 0:0.6}];
\end{axis}
\end{tikzpicture}
\label{fig:SustProf}
\caption{Sustainability of the ABS with a competitive fringe of retailers for fixed b.}
\end{figure}
\end{document}