我想填充函数之间的区域。它需要透明才能使轴可见。第二个问题是:如何更改垂直轴标签的位置?
\usepackage{tikz}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
% style
\pgfplotsset{my style/.append style={axis x line=middle, axis y line=
middle, xlabel={$\phi$}, ylabel={$\psi$}, axis equal }}
\begin{axis}[my style, minor tick num=2,blend group = soft light]
% drawing four functions
\addplot[name path=A,domain=0:1]{x-1};
\addplot[name path=B,domain=0:1]{-x+1 };
\addplot[name path=C,domain=-1:0]{x+1};
\addplot[name path=D,domain=-1:0]{-x-1};
% drawing rectangles
\fill[blue!40!white,rotate=45] (25,25) rectangle (175,175);
\fill[red!40!white] (25,25) rectangle (175,175);
答案1
将选项compat
至少设置为值 1.11(当前版本为 1.17),您可以在一个步骤中绘制并填充矩形:
\documentclass[margin=5pt]{standalone}
\usepackage{pgfplots}% loads tikz
\pgfplotsset{compat=1.17}% <- added
% style
\pgfplotsset{
my style/.style={
axis lines=middle,
axis equal,
xlabel={$\phi$},
ylabel={$\psi$},
ylabel style=right,% <- added
xmin=-1,xmax=1,% <- added
ymin=-1,ymax=1,% <- added
}
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[my style, minor tick num=2,blend mode=soft light]
\draw[fill=red!50!white](-1,0)--(0,-1)--(1,0)--(0,1)--cycle;
\end{axis}
\end{tikzpicture}
\end{document}
或者得到相同的结果:
\documentclass[margin=5pt]{standalone}
\usepackage{pgfplots}% loads tikz
\pgfplotsset{compat=1.17}% <- added
% style
\pgfplotsset{
my style/.style={
axis lines=middle,
axis equal,
xlabel={$\phi$},
ylabel={$\psi$},
ylabel style=right% <- added
}
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[my style, minor tick num=2,blend mode=soft light]
\addplot[domain=0:1]{x-1};
\addplot[domain=0:1]{-x+1 };
\addplot[domain=-1:0]{x+1};
\addplot[domain=-1:0]{-x-1};
\fill[red!50!white](-1,0)--(0,-1)--(1,0)--(0,1)--cycle;
\end{axis}
\end{tikzpicture}
\end{document}