用 PGFplots 和 axis equal=true 填充象限

用 PGFplots 和 axis equal=true 填充象限

我需要填充第三象限,并希望使用axis equal=true。但是,使用此设置会更改 x 的最小值。我该如何调整它以填充整个象限,而不依赖于axis equal=trueaxis equal=false

下面的 MWE 产生了我想要的结果,但如果我取消注释该axis equal=true行则不会产生结果。

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}

\newcommand*{\XAxisMin}{-4.5}
\newcommand*{\XAxisMax}{4.5}
\newcommand*{\YAxisMin}{-4.5}
\newcommand*{\YAxisMax}{4.5}

\begin{axis}[%axis equal=true,
    axis y line=center, axis x line=middle, axis on top=true,
    xmin=\XAxisMin, xmax=\XAxisMax, ymin=\YAxisMin, ymax=\YAxisMax, 
] 

\fill[red!40]
    (axis cs:\XAxisMin,\YAxisMin) -- 
    (axis cs:0.0,\YAxisMin) -- 
    (axis cs:0.0,0.0) -- 
    (axis cs:\XAxisMin,0.0) -- 
    cycle;

% Based on Jake's solution, this should have worked, but produces identical results for me (PGFversion: 2.10)
%        \fill[green!40]
%       (axis cs:\pgfkeysvalueof{/pgfplots/xmin},\pgfkeysvalueof{/pgfplots/ymin}) -- 
%       (axis cs:0.0,\pgfkeysvalueof{/pgfplots/ymin}) -- 
%       (axis cs:0.0,0.0) -- 
%       (axis cs:\pgfkeysvalueof{/pgfplots/xmin},0.0) -- 
%       cycle;


\end{axis} 
\end{tikzpicture} 
\end{document}

答案1

三种方法:

  • 使用rel axis cs坐标系,将 (0,0) 放置在图的左下角,将 (1,1) 放置在右上角
  • 使用锚点current axis.left of origincurrent axis.below origin,它们分别是轴原点在绘图左侧和下方边缘的投影
  • 或者,如果你使用 PGFplots 的 CVS 版本,则读取xminxmax使用\pgfkeysvalueof{/pgfplots/xmin}

例子:

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}

\newcommand*{\XAxisMin}{-4.5}
\newcommand*{\XAxisMax}{4.5}
\newcommand*{\YAxisMin}{-4.5}
\newcommand*{\YAxisMax}{4.5}

\begin{axis}[axis equal=true,
    axis y line=center, axis x line=middle, axis on top=true,
    xmin=\XAxisMin, xmax=\XAxisMax, ymin=\YAxisMin, ymax=\YAxisMax, 
] 

\fill[orange!50] (axis cs:0,0) rectangle (rel axis cs:1,1);

\fill[green!70!black] (current axis.right of origin) rectangle (current axis.below origin);

\fill[blue!50]
    (axis cs:\pgfkeysvalueof{/pgfplots/xmin},\pgfkeysvalueof{/pgfplots/ymax})
    rectangle (axis cs:0,0) node [pos=0.5,text=white] {CVS only};

\end{axis} 
\end{tikzpicture} 
\end{document}

带有彩色象限的 pgfplots

相关内容