我正在尝试想出一种方法来让我的数据点标签不重叠。我已经能够一次更改所有标签的锚点或对齐选项,但我无法像定义标记或标签的样式那样定义每个单独点的锚点。
其次,我还想把轴标签也去掉。更准确地说,我想要的样式是右对齐并ylabel
换行,但我读过的关于轴标签放置的所有有希望的答案都对我得到的结果毫无影响。
这是我希望的第一步以及最终结果的工作环境。
顺便说一下,数据来自https://doi.org/10.2873/725585对于任何感兴趣的人。
\documentclass[border=6pt,dvipsnames]{standalone}
\usepackage[T1]{fontenc}
\usepackage[main=french, english]{babel}
\usepackage[version=4]{mhchem}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\usetikzlibrary{babel, backgrounds}
\usepackage[dvipsnames]{xcolor}
\begin{document}
\begin{tikzpicture}
\begin{axis}[xmin=0, xmax=8, ymin=0, ymax=5,%
xlabel near ticks, xlabel={Economic importance},%
ylabel near ticks, ylabel={Supply risk},%
axis lines=middle,%
xtick={0, 2.8, 8},%
ytick={0, 1.0, 5},%
unit vector ratio={5 8}
]
\addplot[
scatter/classes={%
CRM={mark=*},%
SRM={fill=gray, mark=*},%
non={scale=.5,mark=*}},
scatter, only marks,
scatter src=explicit symbolic,
nodes near coords*={\Label},
visualization depends on={value \thisrow{label} \as \Label}
] table [meta=class] {
EI SR class label
4.9 4.0 CRM \ce{Ce}
6.8 2.8 CRM \ce{Co}
7.2 0.7 non \ce{Cr}
4.0 0.1 SRM \ce{Cu}
7.2 0.5 non \ce{Fe}
2.9 0.5 non \ce{H}
6.4 3.9 CRM \ce{Ir}
2.9 3.5 CRM \ce{La}
6.9 1.2 CRM \ce{Mn}
6.7 0.8 non \ce{Mo}
5.7 0.5 SRM \ce{Ni}
6.9 2.1 CRM \ce{Pt}
6.3 1.6 CRM \ce{Ti}
};
\node (O) at (0,0) {};
\node (A) at (2.8,0 |- current axis.south) {};
\node (B) at (2.8,0 |- current axis.north) {};
\node (C) at (current axis.west |- 0,1.0) {};
\node (D) at (current axis.east |- 0,1.0) {};
\node (M) at (A |- C) {};
\node (X) at (D |- B) {};
\begin{scope}[on background layer]
\fill[gray!5] (O) rectangle (X);
\fill[red!20] (M) rectangle (X);
\draw[dashed] (A)--(B);
\draw[dashed] (C)--(D);
\end{scope}
\end{axis}
\end{tikzpicture}
\end{document}
编辑:
我可以通过在可选参数中添加以下两行来回答我的第一个问题addplot
:
nodes near coords style={anchor=\Anchor},
visualization depends on={value \thisrow{anchor} \as \Anchor}
并在我的表中添加第五列:
EI SR class label anchor
4.9 4.0 CRM \ce{Ce} south
6.8 2.8 CRM \ce{Co} south
7.2 0.7 non \ce{Cr} west
4.0 0.1 SRM \ce{Cu} south
7.2 0.5 non \ce{Fe} north
2.9 0.5 non \ce{H} west
6.4 3.9 CRM \ce{Ir} south
2.9 3.5 CRM \ce{La} west
6.9 1.2 CRM \ce{Mn} south
6.7 0.8 non \ce{Mo} east
5.7 0.5 SRM \ce{Ni} east
6.9 2.1 CRM \ce{Pt} south
6.3 1.6 CRM \ce{Ti} south
我还发现标签位置与命令绑定,axis lines=middle
但这就是我从中获得的全部信息。
axis lines=left
至少给你留下了一个可读的图表。
答案1
和往常一样,有多种方法可以做到这一点。这里是其中一种方法。一些评论。
(A)可能有一种方法可以通过 pgfplots 选项和命令实现所有功能。但是,我发现尝试所有功能非常繁琐。// 因此,让我们Inventive Thinking
以(待解释)的形式应用一些Inventive Principles (IP)
。
(二) Making a copy (of addplot)
+ Taking out (data)
。复制整个\addplot
语句并相互删除数据很容易。这样,您将有机会以您想要的方式(即以不同的方式)处理两个图。 // 再次,尝试所有 pgfplots 选项还不够快,所以让我们再应用另一个IP
。
(C) Do a little more (coding)
。那么,为什么不把几个有问题的数据点放在\addplot
(上面已经做过了) 上,并把一些 放在\node
缺失的标签上呢?//为了更好地看到它们,我给它们涂上了颜色red
。你可以看到各种锚定或移动标签的方法。做出你的选择。(是的,你可以用 替换所有内容\foreach
,但代码不会很短,可能不那么容易理解。)
...
% ~~~ Inventive Principle: Do a little more (coding) ~~~~~~~~~~~
% ~~~ Poor mans solution: place those labels manually ~~~~~~~
\node[red,anchor=west] at (7.2,.7){\ce{Cr}};% using standard names
\node[red,anchor=160] at (7.2,.5){\ce{Fe}};% using polar notation
...
(四)好吧,放置轴标签的工作就留给你了 ;-)
(英)另外,您还可以将彩色区域和虚线分配给另一个\addplot
,该另一个应该是第一个。这样,您就不需要\node
在代码末尾添加 s ( IP: Improve by total replacement
)。
\documentclass[border=6pt,dvipsnames]{standalone}
\usepackage[T1]{fontenc}
\usepackage[main=french, english]{babel}
\usepackage[version=4]{mhchem}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\usetikzlibrary{babel, backgrounds}
\usepackage[dvipsnames]{xcolor}
\begin{document}
\begin{tikzpicture}
\begin{axis}[xmin=0, xmax=8, ymin=0, ymax=5,%
% ~~~ these puzzle me, so take them away for the moment ~~~~~
%% xlabel near ticks,
% xlabel={Economic importance},%
%% ylabel near ticks,
% ylabel={Supply risk},%
% axis lines=middle,%
xtick={0, 2.8, 8},%
ytick={0, 1.0, 5},%
unit vector ratio={5 8}
]
% ~~~ See next comment for modifications done ~~~~~~~~~
\addplot[
scatter/classes={%
CRM={mark=*},%
SRM={fill=gray, mark=*},%
non={scale=.5,mark=*}},
scatter, only marks,
scatter src=explicit symbolic,
nodes near coords*={\Label},
visualization depends on={value \thisrow{label} \as \Label}
] table [meta=class] {
EI SR class label
4.9 4.0 CRM \ce{Ce}
6.8 2.8 CRM \ce{Co}
4.0 0.1 SRM \ce{Cu}
6.4 3.9 CRM \ce{Ir}
5.7 0.5 SRM \ce{Ni}
6.9 2.1 CRM \ce{Pt}
6.3 1.6 CRM \ce{Ti}
};
% ~~~ Inventive Principles: Making a copy (of addplot) + Taking out (data) ~~~~~~~~~
\addplot[
scatter/classes={%
CRM={mark=*},%
SRM={fill=gray, mark=*},%
non={scale=.5,mark=*}},
scatter, only marks,
scatter src=explicit symbolic,
% nodes near coords*={\Label},
% visualization depends on={value \thisrow{label} \as \Label},
label distance=1cm,
] table [meta=class] {
EI SR class label
7.2 0.7 non \ce{Cr}
7.2 0.5 non \ce{Fe}
6.9 1.2 CRM \ce{Mn}
6.7 0.8 non \ce{Mo}
2.9 0.5 non \ce{H}
2.9 3.5 CRM \ce{La}
};
% ~~~ Inventive Principle: Do a little more (coding) ~~~~~~~~~~~
% ~~~ Poor mans solution: place those labels manually ~~~~~~~
\node[red,anchor=west] at (7.2,.7){\ce{Cr}};% using standard names
\node[red,anchor=160] at (7.2,.5){\ce{Fe}};% using polar notation
\node[red,anchor=200] at (6.9,1.2){\ce{Mn}};
\node[red,anchor=70 ] at (6.7,.8){\ce{Mo}};
\node[red,anchor=250] at (2.9,.5){\ce{H}};
\node[red,xshift=3mm] at (2.9,3.5){\ce{La}};% alternative way to place labels
% ~~~ unchanged ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
\node (O) at (0,0) {};
\node (A) at (2.8,0 |- current axis.south) {};
\node (B) at (2.8,0 |- current axis.north) {};
\node (C) at (current axis.west |- 0,1.0) {};
\node (D) at (current axis.east |- 0,1.0) {};
\node (M) at (A |- C) {};
\node (X) at (D |- B) {};
\begin{scope}[on background layer]
\fill[gray!5] (O) rectangle (X);
\fill[red!20] (M) rectangle (X);
\draw[dashed] (A)--(B);
\draw[dashed] (C)--(D);
\end{scope}
\end{axis}
\end{tikzpicture}
\end{document}