我有这个代码:
\begin{tikzpicture}[scale=0.75]
\begin{axis}[axis lines=center,xmin=-1,xmax=9,ymin=-1,ymax=9,line width=0.4mm]
\pgfplotsset{xlabel={$x$},ylabel={$y$}}
\draw[fill=blue!40,line width=0mm] (5,-0.5) rectangle (8,0.5);
\end{axis}
\end{tikzpicture}
我尝试添加on layer=axis background
到draw
命令中,但不幸的是它不起作用,因为它必须位于命令内部addplot
。
我不想将矩形放在环境中tikzpicture
,因为这会迫使我更改其所在位置的值。有没有办法将图形(矩形、圆形等)放在环境中,axis
并放在轴后面?
谢谢!
答案1
轴背景似乎有效,我还添加了一种简单的方法来将坐标走私到轴环境之外,尽管这里不需要。顺便说一句,根据您的情节,您可能需要添加set layers, cell picture=true
。
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\begin{document}
\begin{tikzpicture}[scale=0.75]
\begin{axis}[axis lines=center,xmin=-1,xmax=9,ymin=-1,ymax=9,line
width=0.4mm, axis background/.style={%
preaction={
path picture={
\draw[fill=blue!40,line width=0mm] (axis cs:5,-0.5) rectangle (axis cs:8,0.5);
}}}]
\pgfplotsset{xlabel={$x$},ylabel={$y$}}
\coordinate (BL) at (5,-0.5);
\coordinate (TR) at (8,0.5); % just to export the coordinates outside
%the axis, not needed here
\end{axis}
\end{tikzpicture}
\end{document}
答案2
只需添加axis on top
选项。
\documentclass[tikz, border=1 cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}[scale=0.75]
\begin{axis}[axis lines=center,xmin=-1,xmax=9,ymin=-1,ymax=9,line width=0.4mm, axis on top]
\pgfplotsset{xlabel={$x$},ylabel={$y$}}
\draw[fill=blue!40,line width=0mm] (5,-0.5) rectangle (8,0.5);
\end{axis}
\end{tikzpicture}
\end{document}