在条形图的图例中添加阴影框

在条形图的图例中添加阴影框

我有一个用 绘制的条形图TikZ。有两对条形图 - 非常小的条形图,用灰色或黑色阴影表示。我希望在图例中为每个条形图添加一个小方块。在图例中,用黑色阴影表示的小方框应位于“较便宜的礼物”的左侧,用灰色阴影表示的小方框应位于“较昂贵的礼物”的左侧。两个方框都应与图例的左边缘齐平。(我想将代码保留在 中TikZ。)

\documentclass[10pt]{amsart}
\usepackage{tikz}
\usetikzlibrary{calc,intersections}


\begin{document}

\begin{tikzpicture}

%The two bars for the less expensive gift are drawn. They are shaded black.
\path[fill=black] (0.5,2) -- (0.75,2) -- (0.75,0) -- (0.5,0) -- cycle;
\path[fill=black] (1.75,2.55) -- (2,2.55) -- (2,0) -- (1.75,0) -- cycle;

%The two bars for the more expensive gift are drawn. They are shaded gray.
\path[fill=gray!50] (0.75,2.75) -- (1,2.75) -- (1,0) -- (0.75,0) -- cycle;
\path[fill=gray!50] (2,2.45) -- (2.25,2.45) -- (2.25,0) -- (2,0) -- cycle;


%The y-axis is drawn. Tick marks along the y-axis are drawn.
%\draw (0,0) -- (0,3.5);
\draw (-2pt,1) -- (2pt,1);
\draw (-2pt,1.5) -- (2pt,1.5);
\draw (-2pt,2) -- (2pt,2);
\draw (-2pt,2.5) -- (2pt,2.5);
\draw (-2pt,3) -- (2pt,3);
\draw (-2pt,3.5) -- (2pt,3.5);

%Labels for the tick marks are typeset.
\node[anchor=east, inner sep=0] at ($(-2pt,0) +(-0.15,0)$){$0$};
\node[anchor=east, inner sep=0] at ($(-2pt,1) +(-0.15,0)$){$4.5$};
\node[anchor=east, inner sep=0] at ($(-2pt,1.5) +(-0.15,0)$){$5.0$};
\node[anchor=east, inner sep=0] at ($(-2pt,2) +(-0.15,0)$){$5.5$};
\node[anchor=east, inner sep=0] at ($(-2pt,2.5) +(-0.15,0)$){$6.0$};
\node[anchor=east, inner sep=0] at ($(-2pt,3) +(-0.15,0)$){$6.5$};
\node[anchor=east, inner sep=0] at ($(-2pt,3.5) +(-0.15,0)$){$7.0$};

%The symbol for a broken y-axis is drawn.
\draw (0,0.25) -- ($(0,0.25) +(150:0.125)$);
\draw ($(0,0.25) +(150:0.125)$) -- ($(0,0.25) +(150:0.125) +(30:0.25)$);
\draw ($(0,0.25) +(150:0.125) +(30:0.25)$) -- ($(0,0.25) +(150:0.125) +(30:0.25) +(150:0.125)$);
\draw (0,0) -- (0,0.25);
\draw ($(0,0.25) +(150:0.125) +(30:0.25) +(150:0.125)$) -- (0,3.5);

%The x-axis is drawn.
\draw (0,0) -- (2.5,0);

%The labels "giver" and "recipient" are typeset.
\node[anchor=north, inner sep=0] at ($(0.75,0) +(0,-0.15)$){giver};
\node[anchor=north, inner sep=0] at ($(2,0) +(0,-0.15)$){recipient};

\node[anchor=south, rotate=90, align=center] at (-0.9,1.75){Mean \\ Appreciation};
\node[anchor=north] at (1.25,-0.5){Role};

\node[align=center, font=\bfseries, anchor=south, yshift=3mm] at (current bounding box.north){Givers' Perceived and Recipients' \\ Actual Gift Appreciations};

\node[draw, anchor=west, align=center] at (3,3){less expensive gift \\ more expensive gift};

\end{tikzpicture}
\vskip0.25in



\end{document}

输出

在此处输入图片描述

答案1

因此,这里有一个使用节点相对位置和 fit 库的可能解决方案:

在此处输入图片描述

编辑

参考评论的一些解释:shape=rectangle基本上定义了节点的形状。但是,要定义其大小,有三个选项,minimum widthminimum heightminimum size虽然minimum widthminimum height允许独立定义两个量,minimum width但假设两者相等。因此,定义shape=rectangle, minimum width=...结果为正方形。

注释的对齐是在定位库的帮助下完成的。例如,我通过说将注释放置less expensive gift在先前定义的节点的右侧。您也可以只说,但我不喜欢输出,所以我通过在和之间添加(字体大小的一半)来微调距离。这里,leg 只是对先前定义的节点的引用。我对第二个注释执行相同的操作。legright=0.5em of legright= of leg0.5emright=of leg

此外,我利用所有四个元素都是预定义节点的事实,在注释周围绘制了框架。通过说\node[draw, fit = (leg) (meg) (legtext) (megtext)] {};我只是创建一个节点,该节点在大小上适合其他四个节点,被绘制并且没有其他内容(因此花括号为空)。

代码

\documentclass[10pt]{amsart}
\usepackage{tikz}
\usetikzlibrary{calc,intersections, positioning, fit}


\begin{document}

\begin{tikzpicture}

%The two bars for the less expensive gift are drawn. They are shaded black.
\path[fill=black] (0.5,2) -- (0.75,2) -- (0.75,0) -- (0.5,0) -- cycle;
\path[fill=black] (1.75,2.55) -- (2,2.55) -- (2,0) -- (1.75,0) -- cycle;

%The two bars for the more expensive gift are drawn. They are shaded gray.
\path[fill=gray!50] (0.75,2.75) -- (1,2.75) -- (1,0) -- (0.75,0) -- cycle;
\path[fill=gray!50] (2,2.45) -- (2.25,2.45) -- (2.25,0) -- (2,0) -- cycle;


%The y-axis is drawn. Tick marks along the y-axis are drawn.
%\draw (0,0) -- (0,3.5);
\draw (-2pt,1) -- (2pt,1);
\draw (-2pt,1.5) -- (2pt,1.5);
\draw (-2pt,2) -- (2pt,2);
\draw (-2pt,2.5) -- (2pt,2.5);
\draw (-2pt,3) -- (2pt,3);
\draw (-2pt,3.5) -- (2pt,3.5);

%Labels for the tick marks are typeset.
\node[anchor=east, inner sep=0] at ($(-2pt,0) +(-0.15,0)$){$0$};
\node[anchor=east, inner sep=0] at ($(-2pt,1) +(-0.15,0)$){$4.5$};
\node[anchor=east, inner sep=0] at ($(-2pt,1.5) +(-0.15,0)$){$5.0$};
\node[anchor=east, inner sep=0] at ($(-2pt,2) +(-0.15,0)$){$5.5$};
\node[anchor=east, inner sep=0] at ($(-2pt,2.5) +(-0.15,0)$){$6.0$};
\node[anchor=east, inner sep=0] at ($(-2pt,3) +(-0.15,0)$){$6.5$};
\node[anchor=east, inner sep=0] at ($(-2pt,3.5) +(-0.15,0)$){$7.0$};

%The symbol for a broken y-axis is drawn.
\draw (0,0.25) -- ($(0,0.25) +(150:0.125)$);
\draw ($(0,0.25) +(150:0.125)$) -- ($(0,0.25) +(150:0.125) +(30:0.25)$);
\draw ($(0,0.25) +(150:0.125) +(30:0.25)$) -- ($(0,0.25) +(150:0.125) +(30:0.25) +(150:0.125)$);
\draw (0,0) -- (0,0.25);
\draw ($(0,0.25) +(150:0.125) +(30:0.25) +(150:0.125)$) -- (0,3.5);

%The x-axis is drawn.
\draw (0,0) -- (2.5,0);

%The labels "giver" and "recipient" are typeset.
\node[anchor=north, inner sep=0] at ($(0.75,0) +(0,-0.15)$){giver};
\node[anchor=north, inner sep=0] at ($(2,0) +(0,-0.15)$){recipient};

\node[anchor=south, rotate=90, align=center] at (-0.9,1.75){Mean \\ Appreciation};
\node[anchor=north] at (1.25,-0.5){Role};

\node[align=center, font=\bfseries, anchor=south, yshift=3mm] at (current bounding box.north){Givers' Perceived and Recipients' \\ Actual Gift Appreciations};

%drawing legend
\node[shape=rectangle, minimum size=1em, fill=black] (leg) at (3.5,3) {};
\node[shape=rectangle, minimum size=1em, fill=gray!50, below= 1em of leg] (meg) {};
\node[anchor=west, right=0.5em of leg] (legtext) {less expensive gift};
\node[anchor=west, right=0.5em of meg] (megtext) {more expensive gift};
\node[draw, fit = (leg) (meg) (legtext) (megtext)] {};

\end{tikzpicture}
\vskip0.25in
%
\end{document}

相关内容