条形图图例上的框架边框可以删除吗?
我正在 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:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我找不到任何关闭框架的选项。 LegendAppearance 的文档相当少,并且图例的样式通常不会得到很多讨论(参见 [2] 以及其中的链接)。
我能想到的最简单的解决方案是手动修改图形。带有图例的图表会生成带标签图形对象。对于单个图例,生成的
Labeled
对象看起来像Labeled[Graphics[...], Framed[...], pos]
,因此您所需要的一切要做的就是删除Framed
部分。只需使用ReplaceAll
删除所有Framed
头即可完成此操作(例如BarChart[...] /.Framed -> Identity
) ,但也许更有针对性的事情会更安全。也可以使用
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 likeLabeled[Graphics[...], Framed[...], pos]
, so all you need to do is remove theFramed
part. This could be done just be removing allFramed
heads using aReplaceAll
(e.g.BarChart[...] /. Framed -> Identity
), but maybe something more targeted would be safer.The above could also be produced using
Replace[bc, Framed[leg_] :> leg, {1}]
orMapAt[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.您可以通过设置暂时全局终止框架:
要恢复默认行为,请设置:
You can temporarily, globally kill the frame by setting:
To restore the default behavior, set:
不如 Simon 给出的方法通用上面,但仍然可能值得发布。 (我在阅读这个问题时发现了这一点)
使用
Part
,其中bc< /code> 的定义见 Simon 的 答案:
给出
Not as versatile as Simon's method given above, but nevertheless possibly worth posting. (I found this out while reading this question)
Using
Part
, wherebc
is as defined in Simon's answer:giving