通过剪辑消除 pgfplot 的透明度

通过剪辑消除 pgfplot 的透明度

考虑使用 pgfplots 的以下 MWE:

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\usepgfplotslibrary{
 fillbetween,
}

\begin{filecontents}{pts.csv}
x,y
3.6386627120750026,2.1736884761115127
3.445726599388463,2.4739683300121
3.2488975045071724,2.72839686622134
3.045860273891269,2.95342761631814
2.8296613845140923,3.1611599454957977
2.5898429681858754,3.360090284645052
2.3119519875664714,3.554875977630425
1.9769852629946454,3.7434434935980674
1.5644535283817826,3.908253774974142
1.0699110160834255,3.9984070914427603
0.551246275257379,3.918459976640702
0.15654951570299858,3.593489883938215
0.003113585010036779,3.095053166719702
0.05186045127151018,2.582197060323091
0.20097818182360894,2.1427808559688635
0.38513515308470336,1.7855916116635968
0.5786775397544923,1.4921039487439738
0.7760351464360633,1.2421530476510645
0.9802641029213808,1.0198665978161137
1.1987699746275555,0.8136295575445396
1.4424651159481976,0.6153774878062035
1.7263626552162032,0.4210558820695427
2.0699690264601402,0.23402494621167258
2.49321492376476,0.07483533646144647
2.9958788526825186,5.653529380200695e-06
3.5084390802646976,0.1084772215676606
3.8765195310672818,0.46256927627306954
3.9997188115807782,0.9710974201840858
3.9331620478247658,1.477449800059868
3.7770989934353656,1.9060821007211945
\end{filecontents}

\begin{document}
  \begin{tikzpicture}
  \begin{axis}[width=0.50\linewidth, height=0.50\linewidth,
               scale only axis, axis on top,
               xmin=0, xmax=4,
               ymin=0, ymax=4]
   \addplot+[gray,thick, smooth, mark=none,fill=red!50, fill
   opacity=0.1] table [x=x, y=y, col sep=comma]
   {pts.csv} --cycle;
   \addplot+[gray,thick,mark=none,name path=tf] coordinates {(0,3)
   (1,1) (3,0)};
   \path[name path=axis] (0,0) -- (3,0);
   \addplot[color=blue,fill=cyan!10] fill between[of=tf and axis];
  \end{axis}
 \end{tikzpicture}
\end{document}

产生以下渲染:

阴谋

但是,我目前使用的工作流程无法很好地处理透明 PDF。因此,我正在寻找最简单的方法来获得相同的结果,而无需任何不透明度。

我目前的想法是先画出蓝色区域(没有描边),然后画出椭圆(没有描边),接着是相交部分,最后是顶部的描边。但是,我不确定填充两个图相交区域的最佳方法。直观地看,这似乎是一种剪辑解决方案(剪辑到椭圆,然后再次绘制第一个区域,但使用紫色),但我不确定如何使用前一个图作为剪辑。

当然,我可以手动从数据中找出路径,但似乎应该有一个更优雅的解决方案。

答案1

\path对于更复杂的交叉点,您可以将常规(或\draw\fill)与选项一起使用intersection segments。您需要先命名两个相交路径,然后可以(重新)使用交叉点产生的线段。此选项记录在PGFlots 手册参见 5.7 节“填充之间”。

请注意,该pgfplots包已加载包。此外,设置以访问包的最新功能也是tikz一个好主意。compat=1.18

\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=1.18}

\begin{filecontents}{pts.csv}
x,y
3.6386627120750026,2.1736884761115127
3.445726599388463,2.4739683300121
3.2488975045071724,2.72839686622134
3.045860273891269,2.95342761631814
2.8296613845140923,3.1611599454957977
2.5898429681858754,3.360090284645052
2.3119519875664714,3.554875977630425
1.9769852629946454,3.7434434935980674
1.5644535283817826,3.908253774974142
1.0699110160834255,3.9984070914427603
0.551246275257379,3.918459976640702
0.15654951570299858,3.593489883938215
0.003113585010036779,3.095053166719702
0.05186045127151018,2.582197060323091
0.20097818182360894,2.1427808559688635
0.38513515308470336,1.7855916116635968
0.5786775397544923,1.4921039487439738
0.7760351464360633,1.2421530476510645
0.9802641029213808,1.0198665978161137
1.1987699746275555,0.8136295575445396
1.4424651159481976,0.6153774878062035
1.7263626552162032,0.4210558820695427
2.0699690264601402,0.23402494621167258
2.49321492376476,0.07483533646144647
2.9958788526825186,5.653529380200695e-06
3.5084390802646976,0.1084772215676606
3.8765195310672818,0.46256927627306954
3.9997188115807782,0.9710974201840858
3.9331620478247658,1.477449800059868
3.7770989934353656,1.9060821007211945
\end{filecontents}

\begin{document}
  \begin{tikzpicture}
    \begin{axis}[
        width=0.50\linewidth, height=0.50\linewidth,
        scale only axis, 
        axis on top,
        xmin=0, xmax=4,
        ymin=0, ymax=4
    ]
      \addplot+[gray, thick, smooth, mark=none, fill=red, name path=A] 
        table [x=x, y=y, col sep=comma] {pts.csv} --cycle;
      \addplot+[gray, thick, mark=none, name path=tf] 
        coordinates {(0,3) (1,1) (3,0)};
      \path[name path=axis] (0,0) -- (3,0);
      \addplot[color=blue, fill=cyan, name path=B] 
        fill between [of=tf and axis];

      \path[draw=gray, thick, fill=green, intersection segments={
        of=A and B, sequence={L2 -- L3 -- L4 -- R3[reverse] -- R2[reverse]}
      }] -- cycle;
    \end{axis}
  \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容