我想绘制以下图片,其中我可以为文本、形状和线条着色。形状是三角形和星形。
到目前为止,我所做的是
\documentclass[12pt]{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[step=.5cm,gray!20, very thin] (-2,-2) grid (8,8);
\draw[thick,->] (0,0) -- (7,0) node[anchor=north west] {x};
\draw[thick,->] (0,0) -- (0,7) node[anchor=south east] {y};
\coordinate (O) at (2,1);
\coordinate (A) at (2,2);
\coordinate (B) at (3,1.5);
\draw[line width=.2mm, fill=red] (O)--(A)--(B)--cycle;
\coordinate (O) at (1,1.8);
\coordinate (A) at (1,2.8);
\coordinate (B) at (2,2.3);
\draw[line width=.2mm, fill=black] (O)--(A)--(B)--cycle;
\end{tikzpicture}
\end{document}
因此,输出是
答案1
按照您的意愿调整星星:
\documentclass[tikz]{standalone}
\usetikzlibrary{shapes.geometric}
\begin{document}
\begin{tikzpicture}
[
my star/.append style={star, draw, star points=4, minimum height=10mm, star point ratio=2.5}
]
\draw[step=.5cm,gray!20, very thin] (-2,-2) grid (8,8);
\draw[thick,->] (0,0) -- (7,0) node[anchor=north west] {x};
\draw[thick,->] (0,0) -- (0,7) node[anchor=south east] {y};
\coordinate (O) at (2,1);
\coordinate (A) at (2,2);
\coordinate (B) at (3,1.5);
\draw[line width=.2mm, fill=red] (O)--(A)--(B)--cycle;
\coordinate (O) at (1,1.8);
\coordinate (A) at (1,2.8);
\coordinate (B) at (2,2.3);
\draw[line width=.2mm, fill=black] (O)--(A)--(B)--cycle;
\foreach \i/\j/\k [count=\ijno] in {{6,-1}/blue/a,{1.5,5}/yellow/b,{2.5,4.5}/green/c,{4.5,4}/cyan/d,{5.5,3.5}/orange/e}
\node (star \ijno) [my star, fill=\j, label=\k] at (\i) {};
\path [draw] (1.5,2.3)
edge node [left] {text} (star 2.center)
edge node [below right] {text} (star 4.center);
\path [draw] (2.5,1.5)
edge node [right, pos=.75] {text} (star 3.center)
edge node [below right] {text} (star 5.center)
edge node [below left, pos=.6] {text} (star 1.center);
\end{tikzpicture}
\end{document}
更新
正如 Pier Paolo 所说,如果您希望在形状的“后面”绘制线条,最简单的方法是将三角形绘制为节点,并删除锚点,.center
其唯一目的是在星形节点前面绘制线条。
\documentclass[tikz]{standalone}
\usetikzlibrary{shapes.geometric}
\begin{document}
\begin{tikzpicture}
[
my star/.append style={star, draw, star points=4, minimum height=10mm, star point ratio=2.5},
my triangle/.append style={isosceles triangle, draw, minimum height=10mm},
]
\draw[step=.5cm,gray!20, very thin] (-2,-2) grid (8,8);
\draw[thick,->] (0,0) -- (7,0) node[anchor=north west] {x};
\draw[thick,->] (0,0) -- (0,7) node[anchor=south east] {y};
\foreach \i/\j [count=\ijno] in {{1.5,2.3}/magenta,{2.5,1.5}/purple}
\node (triangle \ijno) [my triangle, fill=\j] at (\i) {};
\foreach \i/\j/\k [count=\ijno] in {{6,-1}/blue/a,{1.5,5}/yellow/b,{2.5,4.5}/green/c,{4.5,4}/cyan/d,{5.5,3.5}/orange/e}
\node (star \ijno) [my star, fill=\j, label=\k] at (\i) {};
\path [draw] (triangle 1)
edge node [left] {text} (star 2)
edge node [below right] {text} (star 4);
\path [draw] (triangle 2)
edge node [right, pos=.75] {text} (star 3)
edge node [below right] {text} (star 5)
edge node [below left, pos=.6] {text} (star 1);
\end{tikzpicture}
\end{document}