我在背景中有一个轮廓图,我想在它上面绘制一些存储在文件中的数据。我想用一条文本标记我的线,该文本会切断该线,但我无法获得透明的背景。我在网上找到的所有示例都只有白色背景,这并不理想。
我迄今为止的尝试是:
\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}
看起来像:
答案1
您可以“保护”节点以免被线路透支。
\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}
答案2
您可以设置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}
您还可以参考节点透明背景