是否有可以重现的 LaTeX 宏经合组织的“魔法钻石”这可以表明一个国家的经济表现吗?
以下是“魔法钻石”的概述:
特定的“魔法钻石”可能看起来是这样的,它显示了“前后”或“现在和当时”或“国家 A 和国家 B”,其中两个钻石位于相同的轴上(轴未标记,也未按照经济指标缩放 - 也没有图例)。
理想情况下,宏将接受四个经济指标数字,或它们的一系列集合、每个指标的轴限制,然后生成如上图或下图所示的图表。
答案1
我通常不会添加两个答案,但这是一个与另一个非常不同的策略。它使用groupplots
,并且基于pgfplots
,因此可高度自定义。您需要做的就是在表中指定数据,例如
CB PCB UNR GDP
1 2 3 4
1.5 2.5 1.5 2.5
2 3 2.5 3.5
4 3 2 1
代码将完成其余工作,例如提取列名、查找范围和生成图表。
\documentclass[tikz,border=3mm]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=1.17}
\pgfplotsset{% https://tex.stackexchange.com/a/75811/121799
name nodes near coords/.style={nodes near coords={},
every node near coord/.append style={anchor=center,coordinate,
name=#1-\coordindex,
alias=#1-last,
},
},
name nodes near coords/.default=coordnode
}
\begin{document}
\begin{tikzpicture}
\pgfplotstableread{
CB PCB UNR GDP
1 2 3 4
1.5 2.5 1.5 2.5
2 3 2.5 3.5
4 3 2 1
}{\loadedtable}
\edef\mystyles{"solid","dashed","red","orange","blue"}
\pgfplotstableforeachcolumn\loadedtable\as\col{%
\ifcase\pgfplotstablecol
\edef\mycolA{\col}%
\or
\edef\mycolB{\col}%
\or
\edef\mycolC{\col}%
\or
\edef\mycolD{\col}%
\or
\fi}
\pgfplotstablegetrowsof{\loadedtable}%
\pgfmathtruncatemacro{\numrows}{\pgfplotsretval-1}%
\begin{groupplot}[
group style={group name=top,group size=2 by 2,
horizontal sep=0pt,vertical sep=0pt},height=4cm,width=4cm]
\nextgroupplot[xtick=\empty,yticklabel pos=lower,ylabel=\mycolA,
enlarge y limits=0.3,xmin=-1,xmax=0]
\addplot[only marks,name nodes near coords=\mycolA]
table[y expr=\thisrow{\mycolA},x expr=0] {\loadedtable};
\nextgroupplot[ytick=\empty,xticklabel pos=upper,xlabel=\mycolB,
enlarge x limits=0.3,ymax=1,ymin=0]
\addplot[only marks,name nodes near coords=\mycolB]
table[x expr=\thisrow{\mycolB},y expr=0] {\loadedtable};
\nextgroupplot[ytick=\empty,xticklabel pos=lower,xlabel=\mycolC,
enlarge x limits=0.3,ymax=0,ymin=-1,
xticklabel={\pgfmathparse{-1*\tick}\pgfmathprintnumber\pgfmathresult}]
\addplot[only marks,name nodes near coords=\mycolC]
table[x expr=-1*\thisrow{\mycolC},y expr=0] {\loadedtable};
\nextgroupplot[xtick=\empty,yticklabel pos=upper,ylabel=\mycolD,
enlarge y limits=0.3,xmin=0,xmax=1,
yticklabel={\pgfmathparse{-1*\tick}\pgfmathprintnumber\pgfmathresult}]
\addplot[only marks,name nodes near coords=\mycolD]
table[y expr=-1*\thisrow{\mycolD},x expr=0] {\loadedtable};
\end{groupplot}
\foreach \X in {0,...,\numrows}
{\pgfmathsetmacro{\mystyle}{{\mystyles}[\X]}
\draw[style/.expanded=\mystyle]
(\mycolA-\X) -- (\mycolB-\X) -- (\mycolD-\X) -- (\mycolC-\X) -- cycle;}
\end{tikzpicture}
\end{document}
编辑:修复了轴刻度标签指向左侧或下方的错误。
答案2
答案是肯定的,这是可以做到的。这是一个原型示例。它为不同的坐标系设置了各自的缩放比例,并在其中绘制坐标。
\documentclass[tikz,border=3mm]{standalone}
\tikzset{pics/Magic Diamond/.style={code={
\tikzset{Magic Diamond/.cd,#1}%
\def\pv##1{\pgfkeysvalueof{/tikz/Magic Diamond/##1}}%
\draw (-\pv{size}/2,-\pv{size}/2) rectangle (\pv{size}/2,\pv{size}/2);
\foreach \XX [count=\YY] in {A,B,C,D}
{\begin{scope}[shift={(-\YY*90:\pv{size}/2)},rotate={-90-\YY*90}]
\pgfmathsetmacro{\first}{\pv{\XX min}}
\pgfmathsetmacro{\next}{\first+\pv{\XX step}}
\pgfmathifisint{\next}{\pgfmathtruncatemacro{\next}{\next}}{}
\pgfmathsetmacro{\last}{\pv{\XX max}}
\foreach \ZZ in {\first,\next,...,\last}
{\draw ({(\ZZ-\first)*\pv{size}/(2*(\last-\first))},0)
node[anchor=180-\YY*90,node contents/.expanded=\ZZ] --
++ (0,-\pv{tick length});}
\draw[-stealth] (0,1) -- node[midway,anchor=180-\YY*90,
node contents/.expanded=\pv{\XX}]{} (\pv{size}/2,1);
\end{scope}}
\edef\lstABCD{"X","A","B","C","D"}
\edef\DataSets{\pv{data}}
\foreach \DataSet [count=\nData] in \DataSets
{%\typeout{\DataSet}
\foreach \DataPoint [count=\nDataPoint] in \DataSet
{
\pgfmathsetmacro{\currid}{{\lstABCD}[\nDataPoint]}
\pgfmathsetmacro{\xtransformed}{(\DataPoint-\pv{\currid min})*%
\pv{size}/(2*(\pv{\currid max}-\pv{\currid min}))}
%\typeout{\nDataPoint,\DataPoint,\xtransformed}
\path (-90-90*\nDataPoint:\xtransformed pt) coordinate (tmp-\nDataPoint);
}
\pgfmathsetmacro{\mystyle}{{\pv{styles}}[\nData-1]}
\draw[style/.expanded=\mystyle] (tmp-1) -- (tmp-2) -- (tmp-3) -- (tmp-4) -- cycle;
}
}},Magic Diamond/.cd,size/.initial=10cm,tick length/.initial=3pt,
Amax/.initial=5,Amin/.initial=0,Astep/.initial=1,A/.initial=CB,
Bmax/.initial=5,Bmin/.initial=0,Bstep/.initial=1,B/.initial=PCB,
Cmax/.initial=5,Cmin/.initial=0,Cstep/.initial=1,C/.initial=UNR,
Dmax/.initial=5,Dmin/.initial=0,Dstep/.initial=1,D/.initial=GDP,
data/.initial={{1,2,3,4},{1.5,2.5,1.5,2.5}},
styles/.initial={"solid","dashed","red","orange","blue"}}
\begin{document}
\begin{tikzpicture}
\pic{Magic Diamond};
\end{tikzpicture}
\end{document}