图例标记颜色与箱线图不一致的问题

图例标记颜色与箱线图不一致的问题

我无法使图例颜色与箱线图线条的颜色相同。

这是我的代码:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{statistics}
\usepgfplotslibrary{groupplots}

\begin{document}
\begin{tikzpicture}
\pgfplotsset{
        ushift/.style={
        yshift=0.5cm/1.5},
%        
        dshift/.style={
        yshift=-0.5cm/1.5},
%
    dshift scale/.initial=3em,
    ushift scale/.initial=3em,
%
    /pgfplots/custom legend/.style={
    legend image code/.code={
    \draw [only marks,draw=white,mark=*]
    plot coordinates {(0.3cm,0cm)};},},    
    }
\begin{groupplot}[%
  custom legend,
  legend style={
  legend columns=1,
  column sep=0.5em,
  draw=none,}
  ]
\nextgroupplot[boxplot/draw direction=x,
xmode=log,
ytick={1,2,3},
yticklabels={Grp1, Grp2, Grp3},
xlabel={Counts},
]
%
\addplot+ [% (Primary):
boxplot prepared={
lower whisker= 430000, lower quartile= 220250000,
median= 2000000000,
upper quartile= 10000000000, upper whisker= 1000000000000,
box extend=0.25},
blue, solid, thick, forget plot, dshift] coordinates {};
\addlegendentry{Primary}
%
\addplot+ [% (Secondary):
boxplot prepared={
lower whisker= 430000, lower quartile= 100000000,
median= 2000000000,
upper quartile= 5000000000, upper whisker= 800000000000,
box extend=0.25},
black, solid, thick, ushift] coordinates {};
\addlegendentry{Secondary}
%%
\addplot+ [% (Primary):
boxplot prepared={
lower whisker= 213000, lower quartile= 375000000,
median= 3000000000,
upper quartile= 10000000000, upper whisker= 1000000000000,
box extend=0.25},
blue, solid, thick, forget plot, dshift] coordinates {};
%
\addplot+ [% (Secondary):
boxplot prepared={
lower whisker= 250000, lower quartile= 227000000,
median= 2500000000,
upper quartile= 10750000000, upper whisker= 450000000000,
box extend=0.25},
black, solid, thick, ushift] coordinates {};
%%
\addplot+ [% (Primary):
boxplot prepared={
lower whisker= 75000000, lower quartile= 76100000,
median= 76100000,
upper quartile= 100000000, upper whisker= 500000000,
box extend=0.25},
blue, solid, thick, forget plot, dshift] coordinates {};
%
\addplot+ [% (Secondary):
boxplot prepared={
lower whisker= 1000000000, lower quartile= 1000000000,
median= 1000000000,
upper quartile= 1000000000, upper whisker= 1000000000,
box extend=0.25},
black, solid, thick, ushift] coordinates {};
%
\end{groupplot}
\end{tikzpicture}
\end{document}

以下是相应的输出:

测试

“次要”箱线图的线条颜色为黑色的,但图例中对应的颜色是红色的

此外,标记位置似乎高于图例文本的行。

答案1

更新剧情

三对对数刻度的箱线图。每对都有一个蓝色箱线图(表示主图)和一个黑色箱线图(表示次图)。图表右上角有一个图例,表示与主图和次图相对应的颜色。

代码变更

  • 组图是针对多个轴对齐在一起的,而不仅仅是多个图。因此,我们可以更改groupplot为正常axis环境。
  • 官方文档中的所有箱线图都有\addplot+,但我不知道为什么。不过,加号告诉 PGFPlots 应用默认样式选项,您指定的样式选项,例如blue, solid, thick。由于第一个图的默认绘图颜色为深蓝色,而第二个图的默认绘图颜色为深红色,因此您的图例会显示这两种颜色。通过删除加号,我们告诉 PGFPlots 仅使用您的样式,并忽略默认样式。
  • 在您原来的箱线图中,该forget plot选项告诉 PGFPlots 在生成图例时应该忽略某个图。删除此选项可确保 PGFPlots 将这些图包含在图例中。
  • 您在中定义的向上移位和向下移位样式\pgfplotsset适用于一切与这些图(包括图例)相关联。这就是图例中的点标记看起来向上移动的原因:它们都与ushift图相关联(dshift由于 ,图被跳过forget plot)。相反,我们可以使用draw position = ...将箱线图对移得更近。

代码

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{statistics}

\begin{document}
\begin{tikzpicture}
\pgfplotsset{
  /pgfplots/custom legend/.style={
    legend image code/.code={
      \draw [only marks,draw=white,mark=*]
      plot coordinates {(0.3cm,0cm)};
    },
  },    
}
\begin{axis}[%
  custom legend,
  legend style={
    legend columns=1,
    column sep=0.5em,
    draw=none,
  },
  ytick={1,2,3},
  yticklabels={Grp1, Grp2, Grp3},
  xmode=log,
  xlabel={Counts},
  boxplot/draw direction=x,
]
\addplot [% (Primary):
boxplot prepared={
  draw position  = 0.82,
  lower whisker  = 430000,
  lower quartile = 220250000,
  median         = 2000000000,
  upper quartile = 10000000000,
  upper whisker  = 1000000000000,
  box extend = 0.25},
blue, solid, thick] coordinates {};
%
\addplot [% (Secondary):
boxplot prepared={
  draw position  = 1.18,
  lower whisker  = 430000,
  lower quartile = 100000000,
  median         = 2000000000,
  upper quartile = 5000000000,
  upper whisker  = 800000000000,
  box extend = 0.25},
black, solid, thick] coordinates {};
%
% --------------------------------------------
%
\addplot [% (Primary):
boxplot prepared={
  draw position  = 1.82,
  lower whisker  = 213000,
  lower quartile = 375000000,
  median         = 3000000000,
  upper quartile = 10000000000,
  upper whisker  = 1000000000000,
  box extend = 0.25},
blue, solid, thick] coordinates {};
%
\addplot [% (Secondary):
boxplot prepared={
  draw position  = 2.18,
  lower whisker  = 250000,
  lower quartile = 227000000,
  median         = 2500000000,
  upper quartile = 10750000000,
  upper whisker  = 450000000000,
  box extend = 0.25},
black, solid, thick] coordinates {};
%
% --------------------------------------------
%
\addplot [% (Primary):
boxplot prepared={
  draw position  = 2.82,
  lower whisker  = 75000000,
  lower quartile = 76100000,
  median         = 76100000,
  upper quartile = 100000000,
  upper whisker  = 500000000,
  box extend = 0.25},
blue, solid, thick] coordinates {};
%
\addplot [% (Secondary):
boxplot prepared={
  draw position  = 3.18,
  lower whisker  = 1000000000,
  lower quartile = 1000000000,
  median         = 1000000000,
  upper quartile = 1000000000,
  upper whisker  = 1000000000,
  box extend=0.25},
black, solid, thick] coordinates {};
%
\addlegendentry{Primary}
\addlegendentry{Secondary}
\end{axis}
\end{tikzpicture}
\end{document}

相关内容