Mathematica:我可以将Barchart Legend与堆叠的酒吧匹配吗?

发布于 2024-12-05 07:15:07 字数 696 浏览 1 评论 0原文

我希望堆叠条中颜色的垂直排列与图表图例中颜色的排列相匹配。但无论我如何尝试,它们都不匹配。情况如下

BarChart[{{5, 37, 56}, {22, 49, 28}, {31, 60, 10}},
   ChartLayout -> "Percentile",
   ChartLegends -> Placed[{"1-Volume", "2-Area", "3-Length"}, Right],
   ChartLabels -> {{"Before", "During", "After"}, None}]

Chart1

在现实世界的示例中,图例还有相当多的条目 (6),所以它是如果图例颜色的顺序与条中的顺序匹配,那就太好了。我意识到我可以将 ChartLegends 设置为在 Bottom 显示,但考虑到许多图例条目,看起来不太好。

此外,反转图例列表也无法按预期工作。图例的文本已重新排序,但颜色未重新排序(见下文),因此图例标题不再与图表中的数据匹配。

更改数据(或数据和图例项)的顺序也不起作用。

有什么建议吗?

在此处输入图像描述

I would like to have the vertical arrangement of colors in stacked bars match the arrangement of colors in the chart legend. But no matter what I try, they don't match. Here's the situation

BarChart[{{5, 37, 56}, {22, 49, 28}, {31, 60, 10}},
   ChartLayout -> "Percentile",
   ChartLegends -> Placed[{"1-Volume", "2-Area", "3-Length"}, Right],
   ChartLabels -> {{"Before", "During", "After"}, None}]

Chart1

In the real-world example the legend has quite a few more entries (6), so it would be nice if the order of the legend colors matched the order in the bars. I realize that I could set the ChartLegends to display at Bottom, but doesn't look good given the many legend entries.

Also, reversing the Legends list does not work as desired. The text of the legends was re-ordered, but the colors were not reordered (see below), so the legend captions no longer match the data in the chart.

Changing the order of the data (or the data and the legend items) does not work either.

Any suggestions?

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

酒绊 2024-12-12 07:15:07
BarChart[{{5, 37, 56}, {22, 49, 28}, {31, 60, 10}}, 
  ChartLayout -> "Percentile", 
  ChartLegends -> {"1-Volume", "2-Area", "3-Length"}, 
  ChartLabels -> {{"Before", "During", "After"}, None}] /. 
 Column[List[a : Grid[List[___]] ..]] :> Column[Reverse@List@a]

“在此处输入映像”

edit 记住

记住使用full fullform时用图形/图表/图内部弄乱

BarChart[{{5, 37, 56}, {22, 49, 28}, {31, 60, 10}}, 
  ChartLayout -> "Percentile", 
  ChartLegends -> {"1-Volume", "2-Area", "3-Length"}, 
  ChartLabels -> {{"Before", "During", "After"}, None}] /. 
 Column[List[a : Grid[List[___]] ..]] :> Column[Reverse@List@a]

enter image description here

Edit

Remember to use FullForm when you want to mess up with Graphics/Chart/Plot internals

别念他 2024-12-12 07:15:07

基于 Belisarius 给出的好答案,使用 Part 的替代方法

bc[[2,1,1,1]]= Reverse@bc[[2,1,1,1]];bc

这可以从FullForm

Position[bc, #, Infinity]& /@ {Framed[___],
Column[___],List[___,"1-Volume",___]}

/或来自其中任何一个,也许,以及反复试验。

虽然不是问题的一部分,西蒙的技巧(参见此处)可用于进一步操纵传说。

bc/.Labeled[g_,Framed[leg_],pos_]:>
Labeled[g,Framed[leg,FrameStyle->Orange,RoundingRadius->10,
Background->LightYellow],pos]

例如,给出以下内容:

在此处输入图像描述

Part 也可用于删除图例周围的框架(请参阅问题)
Simon 的方法更加通用。

bc[[2]]=bc[[2,1]];bc

Building on the nice answer given by Belisarius, an alternative method using Part

bc[[2,1,1,1]]= Reverse@bc[[2,1,1,1]];bc

This may be inferred from FullForm and

Position[bc, #, Infinity]& /@ {Framed[___],
Column[___],List[___,"1-Volume",___]}

or from any one of these, perhaps, and trial-and-error.

Although not part of the question, Simon's trick (see here) may be used to further manipulate the legend.

bc/.Labeled[g_,Framed[leg_],pos_]:>
Labeled[g,Framed[leg,FrameStyle->Orange,RoundingRadius->10,
Background->LightYellow],pos]

for example, gives the following:

enter image description here

Part may also be used to remove the frame around the legend (see this question)
but Simon's method is much more versatile.

bc[[2]]=bc[[2,1]];bc
迷迭香的记忆 2024-12-12 07:15:07

您可以使用 /代码> 为此。

SetOptions[Legending`GridLegend, 
  Legending`LegendContainer -> (Framed@MapAt[Reverse, #, {1, 1}] &)];

BarChart[{{5, 37, 56}, {22, 49, 28}, {31, 60, 10}}, 
 ChartLayout -> "Percentile", 
 ChartLegends -> {"1-Volume", "2-Area", "3-Length"}, 
 ChartLabels -> {{"Before", "During", "After"}, None}]

“与belisarius'Graph”

You can use LegendContainer for this.

SetOptions[Legending`GridLegend, 
  Legending`LegendContainer -> (Framed@MapAt[Reverse, #, {1, 1}] &)];

BarChart[{{5, 37, 56}, {22, 49, 28}, {31, 60, 10}}, 
 ChartLayout -> "Percentile", 
 ChartLegends -> {"1-Volume", "2-Area", "3-Length"}, 
 ChartLabels -> {{"Before", "During", "After"}, None}]

same as belisarius' graph

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文