![问题:雷达图超出页面范围](https://linux22.com/image/372130/%E9%97%AE%E9%A2%98%EF%BC%9A%E9%9B%B7%E8%BE%BE%E5%9B%BE%E8%B6%85%E5%87%BA%E9%A1%B5%E9%9D%A2%E8%8C%83%E5%9B%B4.png)
我尝试在一行中创建 3 个雷达图,但我的图表超出了页面范围。我猜是因为设置了 ylim。但我没有找到任何方法来设置 y 轴的限制。我从网上的某个地方获取了这个示例代码。我只使用那里的默认设置更改了我的数据。我的代码是:
\documentclass{article}
\usepackage{tkz-kiviat,pgfplots}
\usepackage{geometry}
\geometry{margin=1cm}
\makeatletter
\def\tkz@KiviatGrad[#1](#2){%
\begingroup
\pgfkeys{/kiviatgrad/.cd,
graduation distance= 0 pt,
prefix ={},
suffix={},
unity=1
}
\pgfqkeys{/kiviatgrad}{#1}%
\let\tikz@label@distance@tmp\tikz@label@distance
\global\let\tikz@label@distance\tkz@kiv@grad
\foreach \nv in {1,...,\tkz@kiv@lattice}{
\pgfmathparse{\tkz@kiv@unity*\nv}
\pgfmathsetmacro{\result}{\pgfmathresult} % change from \pgfmathtruncatemacro
\protected@edef\tkz@kiv@gd{\tkz@kiv@prefix\pgfmathprintnumber[precision=1,fixed]{\result}\tkz@kiv@suffix} % used \pgfmathprintnumber instead of "$\result$"
\path[/kiviatgrad/.cd,#1] (0:0)--(360/\tkz@kiv@radial*#2:\nv*\tkz@kiv@gap)
node[label=(360/\tkz@kiv@radial*#2)-90:\tiny\tkz@kiv@gd] {}; % added \tiny
}
\let\tikz@label@distance\tikz@label@distance@tmp
\endgroup
}%
\makeatother
\begin{document}
\begin{figure}[!h]
\centering
\begin{tikzpicture}[label distance=.1cm,scale=0.37]
% Correlation coefficient
\begin{scope}[local bounding box=a,shift={(0,0)}]
\tkzKiviatDiagram[radial style/.style ={-},rotate=90, lattice style/.style ={black!30}]%
{A,B,C,D,E,F}
\tkzKiviatLine[thick,color=red!20,
mark=ball,
ball color=red!20,
mark size=4pt,
fill=red!20](0.69967,0.518884,0.666573,0.641628,0.260964,-0.097147)
\tkzKiviatLine[thick,color=blue!20,mark=ball,
mark size=4pt,
mark color=blue!20,
fill=blue!20,
opacity=.5](0.69967,0.51442,0.741518,0.683882,0.845317,0.268581)
\tkzKiviatLine[thick,color=blue!20,mark=ball,
mark size=4pt,
mark color=yellow!20,
fill=yellow!20,
opacity=.5](0.69967,0.516279,0.716755,0.674239,0.84688,0.85819)
\tkzKiviatGrad[unity=0.1](1) % set unity as 0.1
\end{scope}
%PBIAS
\begin{scope}[local bounding box=b,shift={(-17,0)}]
\tkzKiviatDiagram[radial style/.style ={-},rotate=90, lattice style/.style ={black!30}]%
{A,B,C,D,E,F}
\tkzKiviatLine[thick,color=red!20,
mark=ball,
ball color=red!20,
mark size=4pt,
fill=red!20](0.549194,31.436088,41.019637,48.748227,-34.859838,243.311831)
\tkzKiviatLine[thick,color=blue!20,mark=ball,
mark size=4pt,
mark color=blue!20,
fill=blue!20,
opacity=.1](0.549194,31.947443,34.558203,40.005646,-157.190631,-70.267567)
\tkzKiviatLine[thick,color=blue!20,mark=ball,
mark size=4pt,
mark color=yellow!20,
fill=yellow!20,
opacity=.5](0.549194,34.251384,36.154543,41.913,-139.627415,-20.655737)
\tkzKiviatGrad[unity=0.5](1) % set unity as 0.1
\end{scope}
% RMSE
\begin{scope}[local bounding box=c,shift={(17,0)}]
\tkzKiviatDiagram[radial style/.style ={-},rotate=90, lattice style/.style ={black!30}]%
{A,B,C,D,E,F}
\tkzKiviatLine[thick,color=red!20,
mark=ball,
ball color=red!20,
mark size=4pt,
fill=red!20](0.030665,0.0591,0.043113,0.058399,0.022864,0.031734)
\tkzKiviatLine[thick,color=blue!20,mark=ball,
mark size=4pt,
mark color=blue!20,
fill=blue!20,
opacity=.5](0.030665,0.059414,0.038696,0.053704,0.023005,0.011813)
\tkzKiviatLine[thick,color=blue!20,mark=ball,
mark size=4pt,
mark color=yellow!20,
fill=yellow!20,
opacity=.5](0.030665,0.060144,0.040277,0.055047,0.02086,0.002801)
\tkzKiviatGrad[unity=0.1](1) % set unity as 0.1
\end{scope}
\end{tikzpicture}
\end{figure}
\end{document}
还有如何为每个带有共同图例的图表设置标题?任何帮助都将不胜感激。谢谢!
答案1
我认为您需要step
为每个设置不同的\tkzKiviatDiagram
值,该值决定了缩放比例。我选择的值是为了让绘图几乎到达边缘,您可能需要调整它。
tkz-kiviat
我认为仅适用于正值,因此需要对进行一些修改才能获得负值的合理结果。虽然下面的代码似乎可以工作,但使用起来可能不太方便。我添加了一些注释来描述与包代码中\tkz@KiviatLine
的定义相比发生了哪些变化。\tkz@KiviatLine
注意:我对 的重新定义做了一些更改\tkz@KiviatGrad
,以便更好地放置轴标签,并能够设置标签的精度。我还从0
而不是开始循环1
,这样您就可以获得中心点的标签。
为了让图表更整洁一些,我设置了lattice=5,gap=1
。请注意lattice
,gap
和step
都是相连的,因此当gap
加倍(默认为0.5
)时,step
必须减半。
要为每个图表获取一个标题,您可以利用您已命名 es 的事实local bounding box
,这样您就可以放置\node
与它们相关的内容,如下面的代码所示。
我还展示了一种制作图例的方法。为了使这更容易,我为三个图定义了三种样式,这减少了代码重复,并使更改外观更加方便。
\documentclass[border=5mm]{standalone}
\usepackage{tkz-kiviat,pgfplots}
\makeatletter
\def\tkz@KiviatGrad[#1](#2){%
\begingroup
\pgfkeys{/kiviatgrad/.cd,
graduation distance= 0 pt,
prefix ={},
suffix={},
unity=1,
% added the following four lines
label precision/.store in=\gradlabel@precision,
label precision=1,
zero point/.store in=\tkz@grad@zero,
zero point=0
}
\pgfqkeys{/kiviatgrad}{#1}%
\let\tikz@label@distance@tmp\tikz@label@distance
\global\let\tikz@label@distance\tkz@kiv@grad
% started loop at zero, to get label for center point
\foreach \nv in {0,...,\tkz@kiv@lattice}{
% skipped the \pgfmathparse, moved
% \tkz@kiv@unity*\nv-\tkz@grad@zero
% into \pgfmathsetmacro. The last term is added
\pgfmathsetmacro{\result}{\tkz@kiv@unity*\nv-\tkz@grad@zero} % change from \pgfmathtruncatemacro
% NOTE: use \gradlabel@precision for the precision
\protected@edef\tkz@kiv@gd{%
\tkz@kiv@prefix%
\pgfmathprintnumber[precision=\gradlabel@precision,fixed]{\result}%% used \pgfmathprintnumber instead of "$\result$"
\tkz@kiv@suffix}
\path[/kiviatgrad/.cd,#1] (0:0)--(360/\tkz@kiv@radial*#2:\nv*\tkz@kiv@gap)
% NOTE: (360/\tkz@kiv@radial*#2)-90 -> (360/\tkz@kiv@radial*#2)
% removed 90 because you rotate all the diagrams
node[label=(360/\tkz@kiv@radial*#2):\tiny\tkz@kiv@gd] {}; % added \tiny
}
\let\tikz@label@distance\tikz@label@distance@tmp
\endgroup
}%
\def\tkz@KiviatLine[#1](#2,#3){%
\begingroup
\pgfkeys{/kiviatline/.cd,
fill= {},
opacity=.5,
% add these two lines, similar to for kiviatline
zero point/.store in=\tkz@line@zero,
zero point=0
}
%
% the code below has a lot of polar coordinates, of the form
% (angle: <value> * <some factor>)
% all have been modified to get them on the form
% (angle:{(<value>+\tkz@line@zero) * <some factor>})
% note extra braces, which are required when a component contains ()
%
\pgfqkeys{/kiviatline}{#1}% opacity ??????
\ifx\tkzutil@empty\tkz@kivl@fill \else
\begin{scope}[on background layer]
\path[fill=\tkz@kivl@fill,opacity=\tkz@kivl@opacity] (360/\tkz@kiv@radial*0:{(#2+\tkz@line@zero)*\tkz@kiv@gap*\tkz@kiv@step})
\foreach \v [count=\rang from 1] in {#3}{%
-- (360/\tkz@kiv@radial*\rang:{(\v+\tkz@line@zero)*\tkz@kiv@gap*\tkz@kiv@step}) } -- (360/\tkz@kiv@radial*0:{(#2+\tkz@line@zero)*\tkz@kiv@gap*\tkz@kiv@step});
\end{scope}
\fi
% added overlay option because something weird happened with the bounding box
\draw[#1,opacity=1,overlay] (0:{(#2+\tkz@line@zero)*\tkz@kiv@gap}) plot coordinates {(360/\tkz@kiv@radial*0:{(#2+\tkz@line@zero)*\tkz@kiv@gap*\tkz@kiv@step})}
\foreach \v [count=\rang from 1] in {#3}{%
-- (360/\tkz@kiv@radial*\rang:{(\v+\tkz@line@zero)*\tkz@kiv@gap*\tkz@kiv@step}) plot coordinates {(360/\tkz@kiv@radial*\rang:{(\v+\tkz@line@zero)*\tkz@kiv@gap*\tkz@kiv@step})}} -- (360/\tkz@kiv@radial*0:{(#2+\tkz@line@zero)*\tkz@kiv@gap*\tkz@kiv@step});
\endgroup
}%
\makeatother
\begin{document}
\begin{tikzpicture}[
label distance=.1cm,
scale=0.37,
plot1/.style={
thick,
draw=red!40,
fill=red!20,
mark=ball,
mark options={
ball color=red, % note ball color inside mark options
mark size=4pt
}
},
plot2/.style={
thick,
draw=blue!40,
fill=blue!40,
mark=ball,
mark options={
mark size=4pt,
ball color=blue
},
opacity=.5
},
plot3/.style={
thick,
draw=blue!20,
fill=yellow!20,
mark=ball,
mark options={
mark size=4pt,
ball color=yellow
},
opacity=.5
}
]
% Correlation coefficient
\begin{scope}[local bounding box=a,shift={(0,0)}]
% define some macros for convenience
\newcommand\KivStep{4}
\pgfmathsetmacro\Unity{1/\KivStep}
\newcommand\zeroshift{0.2}
\tkzKiviatDiagram[
radial style/.style ={-},
rotate=90,
lattice style/.style ={black!30},
step=\KivStep,
gap=1,
lattice=5
]%
{A,B,C,D,E,F}
% I don't really know why /kiviatline/ is necessary here
% but it is, unfortunately
\tkzKiviatLine[
/kiviatline/zero point=\zeroshift,
plot1
](0.69967,0.518884,0.666573,0.641628,0.260964,-0.097147)
\tkzKiviatLine[
/kiviatline/zero point=\zeroshift,
plot2
](0.69967,0.51442,0.741518,0.683882,0.845317,0.268581)
\tkzKiviatLine[
/kiviatline/zero point=\zeroshift,
plot3
](0.69967,0.516279,0.716755,0.674239,0.84688,0.85819)
\tkzKiviatGrad[unity=\Unity, label precision=2, zero point=\zeroshift](0) % set unity as 0.1
\end{scope}
%PBIAS
\begin{scope}[local bounding box=b,shift={(-15,0)}]
\newcommand\KivStep{0.01}
\pgfmathsetmacro\Unity{1/\KivStep}
\newcommand\zeroshift{200}
\tkzKiviatDiagram[
radial style/.style ={-},
rotate=90,
lattice style/.style ={black!30},
step=\KivStep,
gap=1,
lattice=5
]%
{A,B,C,D,E,F}
\tkzKiviatLine[
/kiviatline/zero point=\zeroshift,
plot1
](0.549194,31.436088,41.019637,48.748227,-34.859838,243.311831)
\tkzKiviatLine[
/kiviatline/zero point=\zeroshift,
plot2
](0.549194,31.947443,34.558203,40.005646,-157.190631,-70.267567)
\tkzKiviatLine[
/kiviatline/zero point=\zeroshift,
plot3
](0.549194,34.251384,36.154543,41.913,-139.627415,-20.655737)
\tkzKiviatGrad[unity=\Unity, zero point=\zeroshift](0) % set unity as 0.1
\end{scope}
% RMSE
\begin{scope}[local bounding box=c,shift={(15,0)}]
\newcommand\KivStep{75}
\pgfmathsetmacro\Unity{1/\KivStep}
\tkzKiviatDiagram[
radial style/.style ={-},
rotate=90,
lattice style/.style ={black!30},
step=\KivStep,
gap=1,
lattice=5
]%
{A,B,C,D,E,F}
\tkzKiviatLine[plot1](0.030665,0.0591,0.043113,0.058399,0.022864,0.031734)
\tkzKiviatLine[plot2](0.030665,0.059414,0.038696,0.053704,0.023005,0.011813)
\tkzKiviatLine[plot3](0.030665,0.060144,0.040277,0.055047,0.02086,0.002801)
% need more decimals for labels
\tkzKiviatGrad[unity=\Unity, label precision=3](0)
\end{scope}
\node [above] at (a.north) {Foo};
\node [above] at (b.north) {Bar};
\node [above] at (c.north) {Baz};
\matrix [above] at (current bounding box.north) {
\filldraw [plot1,scale=0.5] plot coordinates {(0,0)(1,0)(1,0.5)(0,0.5)} -- cycle; & \node[anchor=base]{Lorem}; &
\filldraw [plot2,scale=0.5] plot coordinates {(0,0)(1,0)(1,0.5)(0,0.5)} -- cycle; & \node[anchor=base]{ipsum}; &
\filldraw [plot3,scale=0.5] plot coordinates {(0,0)(1,0)(1,0.5)(0,0.5)} -- cycle; & \node[anchor=base]{dolor}; \\
};
\end{tikzpicture}
\end{document}