我的文档中有很多 pgfplot,其中的曲线垂直穿过我想要 x 刻度标签的点。这使得这些标签难以阅读。
我确实希望这些标签仍然在那里。我在想,有没有办法将所有 x 刻度标签的背景颜色设为白色,以便在标签所在的曲线上留出一点空隙?
理想情况下,我希望将一些命令放在文档的开头,然后文档中的所有图表都会执行此操作。(而不是每个图表的命令。)
电流输出
期望输出
平均能量损失
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}
\begin{document}
\pgfplotsset{
standard/.style={
axis x line=middle,
axis y line=left,
}
}
\begin{tikzpicture}[]
\begin{axis}[standard,
domain=0:2,
xtick={1},
xticklabels={label},
]
\addplot[blue]
coordinates {
(0,-1)
(1,-1)
(1,1)
(2,1)
};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
要填充刻度标签的背景,您可以使用ticklabel style={fill=white}
。但它们是在绘图之前绘制的...要更改此顺序,您可以定义并使用新的图层集。
\pgfplotsset{
standard/.style={
axis x line=middle,
axis y line=left,
ticklabel style={fill=white},
set layers=tick labels on top% use layers and choose the new layer set
},
layers/tick labels on top/.define layer set=% define the new layer set based on the standard one
{axis background,axis grid,axis ticks,axis lines,main,%
axis tick labels,% <- tick labels before main
axis descriptions,axis foreground}
{/pgfplots/layers/standard}
}
代码:
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}
\pgfplotsset{
standard/.style={
axis x line=middle,
axis y line=left,
ticklabel style={fill=white},
set layers=tick labels on top% use layers and choose the new layer set
},
layers/tick labels on top/.define layer set=% define the new layer set based on the standard one
{axis background,axis grid,axis ticks,axis lines,main,%
axis tick labels,% <- tick labels before main
axis descriptions,axis foreground}
{/pgfplots/layers/standard}
}
\begin{document}
\begin{tikzpicture}[]
\begin{axis}[standard,
domain=0:2,
xtick={1},
xticklabels={label},
]
\addplot[blue]
coordinates {
(0,-1)
(1,-1)
(1,1)
(2,1)
};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
将xticklabel style = {fill=white}
和添加set layers = axis on top
到轴上:
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}
\begin{document}
\pgfplotsset{
standard/.style={
axis x line=middle,
axis y line=left,
fill = blue
}
}
\begin{tikzpicture}[]
\begin{axis}[standard,
domain=0:2,
xtick={1},
xticklabels={label},
xticklabel style = {fill=white},
set layers = axis on top
]
\addplot[blue]
coordinates {
(0,-1)
(1,-1)
(1,1)
(2,1)
};
\end{axis}
\end{tikzpicture}
\结束{文档}
答案3
您能否检查一下这是否有帮助:
\begin{tikzpicture}[]
\begin{axis}[standard,
domain=0:2,
xtick={1},
% xticklabels={label},
]
\addplot[blue]
coordinates {
(0,-1)
(1,-1)
(1,1)
(2,1)
};
\node[fill=white] at (axis description cs:.5,.445) {label};
\end{axis}
\end{tikzpicture}