我想为我的 Keviat 图添加:图例(红色、蓝色和绿色)和图形标题(带图例)。我的 Keviat 代码如下。谢谢
\documentclass[]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[upright]{fourier}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{tkz-kiviat,numprint,fullpage}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}[label distance=.15cm,rotate=30,scale=0.75]
\tkzKiviatDiagram[radial=3,lattice=7,gap=1,step=1,label space=2]%
{Cover,
Droppings,
Other,
}
\tkzKiviatLine[thick,color=red,fill=red,label=SiteA](0.78,5.59,2.02);label{p4}
\tkzKiviatLine[thick,color=blue,fill=blue](5.92,1.57,3.06);label{p5}
\tkzKiviatLine[thick,color=green,fill=green](2.9,4.6,3.6);label{p6}
\tkzKiviatGrad[suffix=\%,unity=10](0)
\end{tikzpicture}
\end{document}
答案1
这是一种可能性。如果您希望将对象视为浮动对象,则可以使用环境figure
,然后使用标准\caption
命令来提供标题(就像我在第一个示例代码中所做的那样)。如果您不希望图表浮动,则可以使用环境minipage
和\captionof
集成到 KOMA-Script 类中的命令来提供标题(就像我在第二个示例代码中所做的那样)。
为了设计图例,您可以在所需的位置使用\node
和tabular
;当然,您可以根据需要更改设置。
首先,将图表视为浮动figure
物体:
\documentclass[]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[upright]{fourier}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{tkz-kiviat,numprint,fullpage}
\usepackage{pgfplots}
\usetikzlibrary{arrows}
\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}
\newcommand\ColorBox[1]{\textcolor{#1}{\rule{2ex}{2ex}}}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}[label distance=.15cm,rotate=30,scale=0.75]
\tkzKiviatDiagram[radial=3,lattice=7,gap=1,step=1,label space=2]%
{Cover,
Droppings,
Other,
}
\tkzKiviatLine[thick,color=red,fill=red,label=SiteA](0.78,5.59,2.02);label{p4}
\tkzKiviatLine[thick,color=blue,fill=blue](5.92,1.57,3.06);label{p5}
\tkzKiviatLine[thick,color=green,fill=green](2.9,4.6,3.6);label{p6}
\tkzKiviatGrad[suffix=\%,unity=10](0)
\node[anchor=south west,xshift=-60pt,yshift=40pt] at (current bounding box.south east)
{
\begin{tabular}{@{}lp{3cm}@{}}
\ColorBox{red!50} & red description \\
\ColorBox{green!50} & green description \\
\ColorBox{blue!50} & blue description \\
\end{tabular}
};
\end{tikzpicture}
\caption{A caption for the diagram}
\label{fig:test}
\end{figure}
\end{document}
并且没有浮动:
\documentclass[]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[upright]{fourier}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{tkz-kiviat,numprint,fullpage}
\usepackage{capt-of}
\usepackage{pgfplots}
\usetikzlibrary{arrows}
\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}
\newcommand\ColorBox[1]{\textcolor{#1}{\rule{2ex}{2ex}}}
\begin{document}
\noindent\begin{minipage}{\linewidth}
\centering
\begin{tikzpicture}[label distance=.15cm,rotate=30,scale=0.75]
\tkzKiviatDiagram[radial=3,lattice=7,gap=1,step=1,label space=2]%
{Cover,
Droppings,
Other,
}
\tkzKiviatLine[thick,color=red,fill=red,label=SiteA](0.78,5.59,2.02);label{p4}
\tkzKiviatLine[thick,color=blue,fill=blue](5.92,1.57,3.06);label{p5}
\tkzKiviatLine[thick,color=green,fill=green](2.9,4.6,3.6);label{p6}
\tkzKiviatGrad[suffix=\%,unity=10](0)
\node at (current bounding box.east)
{
\begin{tabular}{@{}ll@{}}
\ColorBox{red} & red description \\
\ColorBox{green} & green description \\
\ColorBox{blue} & blue description \\
\end{tabular}
};
\end{tikzpicture}
\captionof{figure}{A caption for the diagram}
\label{fig:test}
\end{minipage}
\end{document}
在标准类中book
,report
可以通过以下包之一访问article
该命令\captionof
capt-of
或者caption
。
答案2
使用 PSTricks。
\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-node}
\usepackage{multido}
\makeatletter
\def\LoadVerbs{\pstVerb{/pt2cm {\strip@pt\psunit\space div} bind def}}
\makeatother
\psset{opacity=0.5,fillstyle=solid}
\def\Atom#1%
{
\multido{\i=1+1}{#1}{\psline[linecolor=gray](\i,0)(0,\i)}
\psline{->}(!#1 12 pt2cm add 0)
}
\def\Molecule#1%
{
\multido{\i=0+90}{4}{\rput{\i}(0,0){\Atom{#1}}}
\multido{\ix=1+1,\i=10+10}{#1}{\rput*{*0}(!\ix\space 6 pt2cm sub -10 pt2cm){\i\%}}
\foreach \a/\t in {0/Right,90/Top,180/Left,270/Bottom}{\pnode(!#1 42 pt2cm add \a\space PtoC){\t}}
}
\def\Label(#1,#2)#3%
{
\psframe[linecolor=#3,fillcolor=#3](#1,#2)(!#1 12 pt2cm add #2 12 pt2cm add)
\rput[l](!#1 18 pt2cm add #2 6 pt2cm add){#3 description}
}
\addtopsstyle{gridstyle}{griddots=0}
\begin{document}
\begin{pspicture}[showgrid=false](-8,-8)(9,8)
\LoadVerbs
\rput{30}(0,0)
{
\Molecule{7}
\pspolygon[linecolor=red,fillcolor=red](0.75,0)(0,6.75)(-2,0)
\pspolygon[linecolor=green,fillcolor=green](3,0)(0,4.75)(-3.75,0)
\pspolygon[linecolor=blue,fillcolor=blue](6,0)(0,1.75)(-3,0)
\foreach \n/\t in {Right/Cover,Top/Droppings,Left/Other}{\rput{*0}(\n){\t}}%! removing white space
}
\foreach \y/\c in {-3/red,-4/green,-5/blue}{\Label(5,\y){\c}}
\end{pspicture}
\end{document}
它是如何制作的?
\documentclass{beamer}
\usepackage{pst-node}
\usepackage{multido}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{pspicture}
\PreviewBorder=12pt
\makeatletter
\def\LoadVerbs{\pstVerb{/pt2cm {\strip@pt\psunit\space div} bind def}}
\makeatother
\psset{opacity=0.5,fillstyle=solid}
\def\Atom#1%
{
\multido{\i=1+1}{#1}{\psline[linecolor=gray](\i,0)(0,\i)}
\psline{->}(!#1 12 pt2cm add 0)
}
\def\Molecule#1%
{
\multido{\i=0+90}{4}{\only<+->{\rput{\i}(0,0){\Atom{#1}}}}
\only<+->{\multido{\ix=1+1,\i=10+10}{#1}{\rput*{*0}(!\ix\space 6 pt2cm sub -10 pt2cm){\i}}}%!
\foreach \a/\t in {0/Right,90/Top,180/Left,270/Bottom}{\pnode(!#1 42 pt2cm add \a\space PtoC){\t}}%!
}
\def\Label(#1,#2)#3%
{
\psframe[linecolor=#3,fillcolor=#3](#1,#2)(!#1 12 pt2cm add #2 12 pt2cm add)
\rput[l](!#1 18 pt2cm add #2 6 pt2cm add){#3 description}
}
\addtopsstyle{gridstyle}{griddots=0}
\begin{document}
\begin{frame}
\begin{pspicture}[showgrid=false](-8,-8)(9,8)
\LoadVerbs
\rput{30}(0,0)
{
\Molecule{7}%!
\only<+->{\pspolygon[linecolor=red,fillcolor=red](0.75,0)(0,6.75)(-2,0)}%!
\only<+->{\pspolygon[linecolor=green,fillcolor=green](3,0)(0,4.75)(-3.75,0)}%!
\only<+->{\pspolygon[linecolor=blue,fillcolor=blue](6,0)(0,1.75)(-3,0)}%!
\foreach \n/\t in {Right/Cover,Top/Droppings,Left/Other}{\only<+->{\rput{*0}(\n){\t}}}%! removing white space
}
\foreach \y/\c in {-3/red,-4/green,-5/blue}{\only<+->{\Label(5,\y){\c}}}
\end{pspicture}
\end{frame}
\end{document}
答案3
使用 tikz 的最简单方法:
首先我定义了一个点 (LegendBox_anchor),它是图例框的锚点。然后我使用一条线来显示 tkzKiviatDiagram Line 所用的颜色。不同的线用 (LegendBox_anchor) 放置。描述是一个简单的节点,添加在线的末尾。anchor=west
用于正确对齐描述
\coordinate[xshift=-2cm] (LegendBox_anchor) at (current bounding box.south east) ;
\draw[line width=3mm,color=red] (LegendBox_anchor) -- ++(.5,0) node[anchor=west] {red description};
\draw[line width=3mm,color=blue] ([yshift=8mm]LegendBox_anchor) -- ++(.5,0) node[anchor=west] {blue description};
\draw[line width=3mm,color=green] ([yshift=16mm]LegendBox_anchor) -- ++(.5,0) node[anchor=west] {green description};
可以使用矩形或节点代替线条。节点有几个优点,可以进行一些改进,但我尝试创建一些易于使用的东西。
另一种可能性是围绕不同的描述定义一个框。如果您稍后添加这种可能性,我可以。
现在,向包中添加一个宏来获取图例可能很有趣。在这里我添加了一个简单的宏(首先尝试扩展包)
\newcommand{\LegendBox}[3][]{%
\coordinate[#1] (LegendBox_anchor) at (#2) ;
\foreach \col/\item [count=\hi from 0] in {#3} {
\draw[line width=3mm,color=\col] ([yshift=\hi*8mm]LegendBox_anchor) -- ++(.5,0)
node[anchor=west] {\item}
;}
}
它基于第一行。参数 #1 用于在必要时获取图例框的更好位置。
\documentclass[]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[upright]{fourier}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{tkz-kiviat,numprint,fullpage}
\usepackage{pgfplots}
\usetikzlibrary{arrows}
\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}
\newcommand{\LegendBox}[3][]{%
\coordinate[#1] (LegendBox_anchor) at (#2) ;
\foreach \col/\item [count=\hi from 0] in {#3} {
\draw[line width=3mm,color=\col] ([yshift=\hi*8mm]LegendBox_anchor) -- ++(.5,0)
node[anchor=west] {\item}
;}
}
\begin{document}
\begin{tikzpicture}[label distance=.15cm,scale=0.75]
\begin{scope}[rotate=30]
\tkzKiviatDiagram[radial=3,lattice=7,gap=1,step=1,label space=2]%
{Cover,
Droppings,
Other,}
\tkzKiviatLine[thick,color=red,fill=red,label=SiteA](0.78,5.59,2.02)
\tkzKiviatLine[thick,color=blue,fill=blue](5.92,1.57,3.06)
\tkzKiviatLine[thick,color=green,fill=green](2.9,4.6,3.6)
\tkzKiviatGrad[suffix=\%,unity=10](0)
\end{scope}
\LegendBox[xshift=-2cm]{current bounding box.south east}%
{red/red decription,
blue/blue description,
green/green }
\end{tikzpicture}
\end{document}
备注:不需要;
在末尾添加\tkzKiviatLine
更新 :
我在图例周围添加了一个框,删除了一些;
我使用节点而不是线条
\documentclass[]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[upright]{fourier}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{tkz-kiviat,numprint,fullpage}
\usepackage{pgfplots}
\usetikzlibrary{arrows,fit}
\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}
\newcommand{\LegendBox}[3][]{%
\xdef\fitbox{}%
\coordinate[#1] (LegendBox_anchor) at (#2) ;
\foreach \col/\item [count=\hi from 0] in {#3} {
\node[color = \col,draw,
fill = \col!50,
minimum width = 4 ex,
minimum height = 2 ex,
label={[anchor = left,name=b\hi]right:\item}] at ([yshift=\hi*4 ex]LegendBox_anchor) {};
\xdef\fitbox{\fitbox(b\hi)}
}%
\node [draw,fit=\fitbox(LegendBox_anchor)] {};
}
\begin{document}
\begin{tikzpicture}[label distance=.15cm,scale=0.75]
\begin{scope}[rotate=30]
\tkzKiviatDiagram[radial=3,lattice=7,gap=1,step=1,label space=2]%
{Cover,
Droppings,
Other}
\tkzKiviatLine[thick,color=red,fill=red,label=SiteA](0.78,5.59,2.02)
\tkzKiviatLine[thick,color=blue,fill=blue](5.92,1.57,3.06)
\tkzKiviatLine[thick,color=green,fill=green](2.9,4.6,3.6)
\tkzKiviatGrad[suffix=\%,unity=10](0)
\end{scope}
\LegendBox[shift={(-3cm,3cm)}]{current bounding box.south east}%
{red/red decription,
blue/blue description,
green/green description }
\end{tikzpicture}
\end{document}