条形图图例上的框架边框可以删除吗?

发布于 2024-11-29 12:07:11 字数 1251 浏览 1 评论 0原文

我正在 Mathematica 中创建用于各种绘图/图表绘制的应用程序。最终它将有一个 GUI,但第一步是获得正确的代码,并且足够简单以便 GUI 可以管理。我很难将图例设置为没有框架。

这是一个最小的示例(已经使用 SetOptions 自定义了 BarChart 上的一些选项。

mydata = {4.5644, 5.546, 6.8674, 2.7688, 1.742, 5.3952, 4.3392, 4.5016, \
 3.7748, 1.838, 2.24, 0.693, 2.818, 4.9, 3.939, 3.459, 3.755, 4.475, \
 3.857, 3.215, 2.206, 2.206, 2.117, 3.403, 3.277, 3.761, 4.276, 2.559, \
 3.486, 4.778, 2.281, 2.865, 3.629, 4.916, 4.572, 5.244, 5.395, 2.865, \
 -0.524, 5.01, 4.401, 4.513, 4.54}

BarChart[mydata, 
 ChartStyle -> {Join[
  Table[RGBColor[0.5, 0.5, 0.95], {Length[mydata] - 3}], {Magenta,
   Magenta, Magenta}]}, PlotRange -> {-2, 8}, 
 ChartLegends -> {Join[
  Table[None, {Length[mydata] - 3}], {Placed[
   Style["Forecasts", FontFamily -> "Arial", FontSize -> 18], 
   Bottom]}]}, BarSpacing -> 0.4, 
 LegendAppearance ->  Directive[Background -> Red, 
   Frame -> None, ImageSize -> 15]]

这是我得到的: 在此处输入图像描述

尽我所能,我无法摆脱图例周围的边框。您可以看到 LegendAppearance 没有执行任何操作 - 我也尝试了其他一些方法。

我不太愿意手动编写小矩形,因为在最终的 GUI 中很难做到这一点。 ChartLabels 也不起作用,因为它已经被用于图表的真实版本中的日期标签。

有人有什么建议吗?

I am creating an application for various kinds of plot/chart drawing in Mathematica. Ultimately it will have a GUI, but the first step is to get the code right, and simple enough for a GUI to manage. I am having difficulty setting legends to have no frame around them.

Here is a minimal example (with some options on BarChart already customised using SetOptions.

mydata = {4.5644, 5.546, 6.8674, 2.7688, 1.742, 5.3952, 4.3392, 4.5016, \
 3.7748, 1.838, 2.24, 0.693, 2.818, 4.9, 3.939, 3.459, 3.755, 4.475, \
 3.857, 3.215, 2.206, 2.206, 2.117, 3.403, 3.277, 3.761, 4.276, 2.559, \
 3.486, 4.778, 2.281, 2.865, 3.629, 4.916, 4.572, 5.244, 5.395, 2.865, \
 -0.524, 5.01, 4.401, 4.513, 4.54}

BarChart[mydata, 
 ChartStyle -> {Join[
  Table[RGBColor[0.5, 0.5, 0.95], {Length[mydata] - 3}], {Magenta,
   Magenta, Magenta}]}, PlotRange -> {-2, 8}, 
 ChartLegends -> {Join[
  Table[None, {Length[mydata] - 3}], {Placed[
   Style["Forecasts", FontFamily -> "Arial", FontSize -> 18], 
   Bottom]}]}, BarSpacing -> 0.4, 
 LegendAppearance ->  Directive[Background -> Red, 
   Frame -> None, ImageSize -> 15]]

And here is what I get:
enter image description here

Try as I might, I can't get rid of that border round the legend. You can see that LegendAppearance does nothing - I've tried a few other approaches to it, too.

I'm reticent to code up little rectangles by hand, because that will be very difficult to get right in the eventual GUI. ChartLabels won't work either, because that is already being used for date labels in the real version of the graph.

Does anyone have any suggestions?

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

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

发布评论

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

评论(3

三生池水覆流年 2024-12-06 12:07:11

我找不到任何关闭框架的选项。 LegendAppearance 的文档相当少,并且图例的样式通常不会得到很多讨论(参见 [2] 以及其中的链接)。

我能想到的最简单的解决方案是手动修改图形。带有图例的图表会生成带标签图形对象。对于单个图例,生成的 Labeled 对象看起来像 Labeled[Graphics[...], Framed[...], pos],因此您所需要的一切要做的就是删除 Framed 部分。只需使用 ReplaceAll 删除所有 Framed 头即可完成此操作(例如 BarChart[...] /.Framed -> Identity) ,但也许更有针对性的事情会更安全。

mydata = {4.5644, 5.546, 6.8674, 2.7688, 1.742, 5.3952, 4.3392, 
   4.5016, 3.7748, 1.838, 2.24, 0.693, 2.818, 4.9, 3.939, 3.459, 
   3.755, 4.475, 3.857, 3.215, 2.206, 2.206, 2.117, 3.403, 3.277, 
   3.761, 4.276, 2.559, 3.486, 4.778, 2.281, 2.865, 3.629, 4.916, 
   4.572, 5.244, 5.395, 2.865, -0.524, 5.01, 4.401, 4.513, 4.54};

bc = BarChart[{Legended[Style[mydata[[;; -4]], Red], "Data"], 
   Legended[Style[mydata[[-3 ;;]], Blue], "Forecasts"]}, 
  PlotRange -> {-2, 8}, BarSpacing -> 0.4, LegendAppearance -> "Row"]

带框架

bc /. Labeled[g_, Framed[leg_], pos_] :> Labeled[g, leg, pos]

也可以使用 Replace[bc, Framed[leg_] :>; leg, {1}]MapAt[Apply[Identity, #] &, bc, 2] 或类似结构。如果您有更多标签或不同类型的图形对象,则不需要太多修改代码。

I can't find any options to turn the frame off. The documentation for LegendAppearance is fairly minimal and the styling of legends in general does not get much discussion (see [2] and links within).

The easiest solution I can think of is to manually modify the graphics. Charts with legends produce Labeled graphics objects. For a single legend, the Labeled object that is produced looks like Labeled[Graphics[...], Framed[...], pos], so all you need to do is remove the Framed part. This could be done just be removing all Framed heads using a ReplaceAll (e.g. BarChart[...] /. Framed -> Identity), but maybe something more targeted would be safer.

mydata = {4.5644, 5.546, 6.8674, 2.7688, 1.742, 5.3952, 4.3392, 
   4.5016, 3.7748, 1.838, 2.24, 0.693, 2.818, 4.9, 3.939, 3.459, 
   3.755, 4.475, 3.857, 3.215, 2.206, 2.206, 2.117, 3.403, 3.277, 
   3.761, 4.276, 2.559, 3.486, 4.778, 2.281, 2.865, 3.629, 4.916, 
   4.572, 5.244, 5.395, 2.865, -0.524, 5.01, 4.401, 4.513, 4.54};

bc = BarChart[{Legended[Style[mydata[[;; -4]], Red], "Data"], 
   Legended[Style[mydata[[-3 ;;]], Blue], "Forecasts"]}, 
  PlotRange -> {-2, 8}, BarSpacing -> 0.4, LegendAppearance -> "Row"]

with frame

bc /. Labeled[g_, Framed[leg_], pos_] :> Labeled[g, leg, pos]

with frame removed

The above could also be produced using Replace[bc, Framed[leg_] :> leg, {1}] or MapAt[Apply[Identity, #] &, bc, 2] or similar constructions. It wouldn't take much to modify the code if you have more labels or different types of graphics objects.

煮酒 2024-12-06 12:07:11

您可以通过设置暂时全局终止框架:

SetOptions[Legending`GridLegend, Legending`LegendContainer -> Identity]

要恢复默认行为,请设置:

SetOptions[Legending`GridLegend, Legending`LegendContainer -> Automatic]

You can temporarily, globally kill the frame by setting:

SetOptions[Legending`GridLegend, Legending`LegendContainer -> Identity]

To restore the default behavior, set:

SetOptions[Legending`GridLegend, Legending`LegendContainer -> Automatic]
留蓝 2024-12-06 12:07:11

不如 Simon 给出的方法通用上面,但仍然可能值得发布。 (我在阅读这个问题时发现了这一点)

使用Part,其中bc< /code> 的定义见 Simon 的 答案

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

给出

在此处输入图像描述

Not as versatile as Simon's method given above, but nevertheless possibly worth posting. (I found this out while reading this question)

Using Part, where bc is as defined in Simon's answer:

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

giving

enter image description here

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