用颜色填充矢量之间的区域

用颜色填充矢量之间的区域

我有以下简单的代码:

\begin{tikzpicture}[scale=0.40]
\coordinate (Origin)   at (0,0);
    \coordinate (XAxisMin) at (-1,0);
    \coordinate (XAxisMax) at (1,0);
    \coordinate (YAxisMin) at (0,0);
    \coordinate (YAxisMax) at (0,0);

\draw[thick,->] (0,0)--(0,4.5) node[anchor=south east] {};
\draw[thick,->] (0,0)--(4.2,0) node[anchor=north west] {};
\draw[thin ,->] (0,0)--(2,3) node[anchor=east ] {\tiny $v_1$};
\draw[thin ,->] (0,0)--(4,1) node[anchor=north ] {\tiny $v_2$};
\end{tikzpicture}

得到以下图片:

在此处输入图片描述

我怎样才能用某种颜色填充两个向量之间的区域?我不太熟悉剪切填充的工作原理。

答案1

这里有一个使用背景层的建议。你说得对,cycle这里没有必要,但也许是一个好习惯。

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{backgrounds}
\begin{document}
\begin{tikzpicture}[scale=0.40]
\coordinate (Origin)   at (0,0);
    \coordinate (XAxisMin) at (-1,0);
    \coordinate (XAxisMax) at (1,0);
    \coordinate (YAxisMin) at (0,0);
    \coordinate (YAxisMax) at (0,0);

\draw[thick,->] (0,0)--(0,4.5) node[anchor=south east] {};
\draw[thick,->] (0,0)--(4.2,0) node[anchor=north west] {};
\draw[thin ,->] (0,0)--(2,3) node[anchor=east,font=\tiny] {$v_1$};
\draw[thin ,->] (0,0)--(4,1) node[anchor=north,font=\tiny] {$v_2$};
\begin{scope}[on background layer]
\fill[blue!20] (0,0) -- (4,1) -- (2,3) -- cycle;
\end{scope}
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

另一种代码是在 Marmot 的答案中添加\filldraw[fill=red, opacity=0.1, draw=none] (0,0) -- (4,1) -- (2,3);而不是:\fill[blue!20] (0,0) -- (4,1) -- (2,3) -- cycle;

在此处输入图片描述

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}[scale=0.40]
    \coordinate (Origin)   at (0,0);
    \coordinate (XAxisMin) at (-1,0);
    \coordinate (XAxisMax) at (1,0);
    \coordinate (YAxisMin) at (0,0);
    \coordinate (YAxisMax) at (0,0);

\draw[thick,->] (0,0)--(0,4.5) node[anchor=south east] {};
\draw[thick,->] (0,0)--(4.2,0) node[anchor=north west] {};
\draw[thin ,->] (0,0)--(2,3) node[anchor=east] {\tiny $v_1$};
\draw[thin ,->] (0,0)--(4,1) node[anchor=north] {\tiny $v_2$};
\filldraw[fill=red, opacity=0.1, draw=none] (0,0) -- (4,1) -- (2,3);
\end{tikzpicture}
\end{document}

相关内容