1

I have a contour plot in the background, and on top of it I want to plot some data stored in a file. I would like to label my line with a text that cuts the line, but I haven't been able to get a transparent background. All examples I find online just have a white background, which is not ideal.

My attempt so far is:

\documentclass[11pt]{article}
\usepackage[top=2.54cm, bottom=2.54cm, left=2.75cm, right=2.75cm]{geometry}
\usepackage{float}
\usepackage{subcaption}
\usepackage{pgfplots}
\usepackage{amsmath}
\begin{filecontents*}{Colour}
    1 1 1
    1 2 1
    1 3 1


    2 1 2
    2 2 2
    2 3 2


    3 1 3
    3 2 3
    3 3 3
\end{filecontents*}
\begin{filecontents*}{Line}
    1 1
    3 3
\end{filecontents*}
\begin{document} 
    \begin{figure}[H]
        \begin{tikzpicture}
        \begin{axis}
        [
        view={0}{90},
        only marks,
        colorbar,
        colorbar style ={width = 6}
        ]
        \addplot3[contour filled={number=40}]
        table[x index =0,y index=1,z index=2]{Colour};
        \addplot[smooth]
        table[x index=0,y index=1]{Line} node[pos = 0.5, sloped, fill=white,minimum size=1mm,inner sep=0,rectangle]{Text};
        \end{axis}
        \end{tikzpicture}
    \end{figure}
\end{document}

Which looks like:

attempt

Daniel Duque
  • 209
  • 1
  • 12

2 Answers2

2

You can "protect" the node against being overdrawn by the line.

\documentclass[11pt]{article}
\usepackage[top=2.54cm, bottom=2.54cm, left=2.75cm, right=2.75cm]{geometry}
\usepackage{float}
\usepackage{subcaption}
\usepackage{pgfplots}
\usepackage{amsmath}
\begin{filecontents*}{Colour}
    1 1 1
    1 2 1
    1 3 1


    2 1 2
    2 2 2
    2 3 2


    3 1 3
    3 2 3
    3 3 3
\end{filecontents*}
\begin{filecontents*}{Line}
    1 1
    3 3
\end{filecontents*}
% based on 
% tex.stackexchange.com/a/38995/_ 
% tex.stackexchange.com/a/76216 
% tex.stackexchange.com/a/59168/_ 
% tex.stackexchange.com/q/_ 
\makeatletter 
\tikzset{ 
reuse path/.code={\pgfsyssoftpath@setcurrentpath{#1}} 
} 
\tikzset{even odd clip/.code={\pgfseteorule}, 
protect/.code={ 
\clip[overlay,even odd clip,reuse path=#1] 
(-6383.99999pt,-6383.99999pt) rectangle (6383.99999pt,6383.99999pt); 
}} 
\makeatother 

\begin{document} 
    \begin{figure}[H]
        \begin{tikzpicture}
        \begin{axis}
        [
        view={0}{90},
        only marks,
        colorbar,
        colorbar style ={width = 6}
        ]
        \addplot3[contour filled={number=40}]
        table[x index =0,y index=1,z index=2]{Colour};
        \addplot [smooth,save path=\LinePlot,draw=none]
        table[x index=0,y index=1]{Line} 
        node[pos = 0.5, sloped,minimum size=1mm,inner sep=0,rectangle,
        save path=\NodePath ]{Text};
        \tikzset{protect=\NodePath}
        \draw[reuse path=\LinePlot];
        \end{axis}
        \end{tikzpicture}
    \end{figure}
\end{document}

enter image description here

0

You can set text opacity=1, fill opacity=0.

\documentclass[11pt]{article}
\usepackage[top=2.54cm, bottom=2.54cm, left=2.75cm, right=2.75cm]{geometry}
\usepackage{float}
\usepackage{subcaption}
\usepackage{pgfplots}
\usepackage{amsmath}
\begin{filecontents*}{Colour}
    1 1 1
    1 2 1
    1 3 1


    2 1 2
    2 2 2
    2 3 2


    3 1 3
    3 2 3
    3 3 3
\end{filecontents*}
\begin{filecontents*}{Line}
    1 1
    3 3
\end{filecontents*}
\begin{document} 
    \begin{figure}[H]
        \begin{tikzpicture}
        \begin{axis}
        [
        view={0}{90},
        only marks,
        colorbar,
        colorbar style ={width = 6}
        ]
        \addplot3[contour filled={number=40}]
        table[x index =0,y index=1,z index=2]{Colour};
        \addplot[smooth]
        table[x index=0,y index=1]{Line} node[pos = 0.5, sloped, fill=white,minimum size=1mm,inner sep=0,rectangle, text opacity=1, fill opacity=0]{Text};
        \end{axis}
        \end{tikzpicture}
    \end{figure}
\end{document}

transparent background for node

You can also refer Node transparent background

  • But I don't want the line to go over the text. What I am looking for is an "Invisible" background around the text where the line is not plotted (so the text can be read better). In my image, imagine that the background was white, the text would seem like interrupting the line. That is what I am looking for, but with a non trivial background. – Daniel Duque Apr 29 '20 at 18:12