插入图像时,T​​ikz 中的绘图线会消失

插入图像时,T​​ikz 中的绘图线会消失

在以下 TikZ 图中,插入图像时情节线会消失。我需要情节线与图像一起出现在这些情节线后面。以下是我使用的代码:

\documentclass[varwidth=true]{standalone}
\usepackage{tikz,pgfplots}
\usepackage{graphicx}
\begin{document}
    \begin{figure}[!h]
            \begin{tikzpicture}[scale=0.68,font=\bfseries]
                \begin{axis}[title=Area of intrest, every axis title/.append style={at={(0.5,1.1)}}, ylabel near ticks, ymin=100,ymax=120,y tick label style={font=\boldmath},  xlabel near ticks,  xlabel=centered area, x tick label style={font=\boldmath},axis on top, ylabel=Millimeters per year, ymajorgrids=true,grid style=dashed,legend entries={A1, B2,C3 ,D4},
                    legend columns=5,
                    legend style={
                        at={(0.15,-0.3)},
                        anchor=south west,legend style={draw=none}},every axis/.append style={semithick},every axis legend/.append style={nodes={right}},enlarge x limits=false,enlarge y limits=false, clip = false,]
                    \addplot [color=magenta,mark=square*,mark size=1.7pt] coordinates {(-6,113)(-5.5,113.8)(-5,110.1)(-4.5,111)(-3,119)(0,118.6)(3,119.5)(4.5,111)(5,109)(5.5,108)(6,103.9)};
                    \addplot [color=red, mark=pentagon*]  coordinates {(-6,105)(-5.5,109)(-5,111)(-4.5,111.9)(-3,118)(0,118.7)(3,119.6)(4.5,112.1)(5,111.6)(5.5,105.8)(6,102.9)};
                    \addplot [color=green, mark=otimes*]  coordinates {(-6,110.4)(-5.5,112.6)(-5,113.4)(-4.5,111.2)(-3,114.7)(0,114.9)(3,114.3)(4.5,113.6)(5,112.4)(5.5,111.6)(6,109)};
                    \addplot graphics
                    [xmin=-3,xmax=3,ymin=100,ymax=120]
                    {example-image-a};
                    \draw[red, |<->|,thick] ([yshift = 6cm] axis description cs:0.753, 0) -- node[above] {Variable} ([yshift = 6cm] axis description cs:0.246, 0);
                    \draw[red, |->|,thick] ([yshift = 6cm] axis description cs:0.25, 0) -- node[above] {\tiny Marginal} ([yshift = 6cm] axis description cs:0, 0);
                    \draw[red, |<-,thick] ([yshift = 6cm] axis description cs:1, 0) -- node[above] { \tiny Marginal   } ([yshift = 6cm] axis description cs:0.753, 0);
                \end{axis}
            \end{tikzpicture}
    \end{figure}
\end{document}

所需的输出应如下: 在此处输入图片描述

答案1

如果你先绘制图像,则可以避免图像遮盖线条:

\documentclass[varwidth=true]{standalone}
\usepackage{tikz,pgfplots}
\usepackage{graphicx}
\begin{document}
    \begin{figure}[!h]
            \begin{tikzpicture}[scale=0.68,font=\bfseries]
                \begin{axis}[title=Area of intrest, every axis title/.append style={at={(0.5,1.1)}}, ylabel near ticks, ymin=100,ymax=120,y tick label style={font=\boldmath},  xlabel near ticks,  xlabel=centered area, x tick label style={font=\boldmath},axis on top, ylabel=Millimeters per year, ymajorgrids=true,grid style=dashed,legend entries={A1, B2,C3 ,D4},
                    legend columns=5,
                    legend style={
                        at={(0.15,-0.3)},
                        anchor=south west,legend style={draw=none}},every axis/.append style={semithick},every axis legend/.append style={nodes={right}},enlarge x limits=false,enlarge y limits=false, clip = false,]
                    \addplot graphics [xmin=-3,xmax=3,ymin=100,ymax=120]{example-image-a};
                    \addplot [color=magenta,mark=square*,mark size=1.7pt] coordinates {(-6,113)(-5.5,113.8)(-5,110.1)(-4.5,111)(-3,119)(0,118.6)(3,119.5)(4.5,111)(5,109)(5.5,108)(6,103.9)};
                    \addplot [color=red, mark=pentagon*]  coordinates {(-6,105)(-5.5,109)(-5,111)(-4.5,111.9)(-3,118)(0,118.7)(3,119.6)(4.5,112.1)(5,111.6)(5.5,105.8)(6,102.9)};
                    \addplot [color=green, mark=otimes*]  coordinates {(-6,110.4)(-5.5,112.6)(-5,113.4)(-4.5,111.2)(-3,114.7)(0,114.9)(3,114.3)(4.5,113.6)(5,112.4)(5.5,111.6)(6,109)};
                    \draw[red, |<->|,thick] ([yshift = 6cm] axis description cs:0.753, 0) -- node[above] {Variable} ([yshift = 6cm] axis description cs:0.246, 0);
                    \draw[red, |->|,thick] ([yshift = 6cm] axis description cs:0.25, 0) -- node[above] {\tiny Marginal} ([yshift = 6cm] axis description cs:0, 0);
                    \draw[red, |<-,thick] ([yshift = 6cm] axis description cs:1, 0) -- node[above] { \tiny Marginal   } ([yshift = 6cm] axis description cs:0.753, 0);
                \end{axis}
            \end{tikzpicture}
    \end{figure}
\end{document}

在此处输入图片描述

答案2

可以观察到,在图例条目中注册了一个新的绘图图形条目,要将其从图例条目中删除,可以执行以下操作。

\addplot[draw=none,forget plot][xmin=-3,xmax=3,ymin=100,ymax=120]{example-image-a};

完整的 MWE:

\documentclass[varwidth=true]{standalone}
\usepackage{tikz,pgfplots}
\usepackage{graphicx}
\begin{document}
    \begin{figure}[!h]
            \begin{tikzpicture}[scale=0.68,font=\bfseries]
                \begin{axis}[title=Area of intrest, every axis title/.append style={at={(0.5,1.1)}}, ylabel near ticks, ymin=100,ymax=120,y tick label style={font=\boldmath},  xlabel near ticks,  xlabel=centered area, x tick label style={font=\boldmath},axis on top, ylabel=Millimeters per year, ymajorgrids=true,grid style=dashed,legend entries={A1, B2,C3 ,D4},
                    legend columns=5,
                    legend style={
                        at={(0.15,-0.3)},
                        anchor=south west,legend style={draw=none}},every axis/.append style={semithick},every axis legend/.append style={nodes={right}},enlarge x limits=false,enlarge y limits=false, clip = false,]
                    \addplot[draw=none,forget plot] graphics [xmin=-3,xmax=3,ymin=100,ymax=120]{example-image-a};
                    \addplot [color=magenta,mark=square*,mark size=1.7pt] coordinates {(-6,113)(-5.5,113.8)(-5,110.1)(-4.5,111)(-3,119)(0,118.6)(3,119.5)(4.5,111)(5,109)(5.5,108)(6,103.9)};
                    \addplot [color=red, mark=pentagon*]  coordinates {(-6,105)(-5.5,109)(-5,111)(-4.5,111.9)(-3,118)(0,118.7)(3,119.6)(4.5,112.1)(5,111.6)(5.5,105.8)(6,102.9)};
                    \addplot [color=green, mark=otimes*]  coordinates {(-6,110.4)(-5.5,112.6)(-5,113.4)(-4.5,111.2)(-3,114.7)(0,114.9)(3,114.3)(4.5,113.6)(5,112.4)(5.5,111.6)(6,109)};
                    \draw[red, |<->|,thick] ([yshift = 6cm] axis description cs:0.753, 0) -- node[above] {Variable} ([yshift = 6cm] axis description cs:0.246, 0);
                    \draw[red, |->|,thick] ([yshift = 6cm] axis description cs:0.25, 0) -- node[above] {\tiny Marginal} ([yshift = 6cm] axis description cs:0, 0);
                        \node[draw=none,fill=none,text opacity=1] (scale) at (0.8cm,6.5cm) {\textbf{ AS}};
                    \draw[red, |<-,thick] ([yshift = 6cm] axis description cs:1, 0) -- node[above] { \tiny Marginal   } ([yshift = 6cm] axis description cs:0.753, 0);
                        \node[draw=none,fill=none,text opacity=1] (scale) at (5.8cm,6.5cm) {\textbf{ RS}};
                \end{axis}
            \end{tikzpicture}
    \end{figure}
\end{document}
[![enter image description here][1]][1]

相关内容