在Mathematica中如何真正实现BarSpacing选项? [英] How is the BarSpacing option really implemented in Mathematica?

查看:139
本文介绍了在Mathematica中如何真正实现BarSpacing选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一个DateListBarChart函数,该函数可以获取已过时的数据并输出与DateListPlot相同位置的条形图.如果给定相同的数据,则必须在相同的水平位置绘制数据,因此可以使用Show进行组合.我发现很难正确设置BarSpacing的设置,以使绘图的水平范围不变,并且条形图基本上保持在同一位置.

I'm trying to implement a DateListBarChart function that takes dated data and outputs a bar chart with the same placements as DateListPlot. It's essential that they plot data in the same horizontal position if given the same data, so they can be combined using Show. I am finding it difficult to get the settings for BarSpacing right so that the horizontal range of the plot doesn't change, and the bars stay in essentially the same place.

我无法推断出正确的缩放比例,因此BarSpacing->{0.2,0.3}导致该组钢筋可用的x轴长度的20%被该组钢筋之间的间距占据,而30%作为组的酒吧.出于技术原因,我这样做是通过将内容传递给RectangleChart来实现的.根据文档,在RectangleChart中将BarSpacing视为绝对单位.显然,如果有更多的序列,则间隙的绝对大小必须较小,并且条的宽度必须更窄.

I have been unable to infer the correct scaling so that BarSpacing->{0.2,0.3} results in 20% of the x-axis length available for that group of bars is taken up with spacing between bars in that group, and 30% as spacing between groups of bars. For technical reasons I am doing this by passing things to RectangleChart. According to the documentation, BarSpacing is treated as absolute units in RectangleChart. Obviously the absolute sizes of the gaps need to be smaller if there are more series, and the bars need to be narrower.

一些例子:

arList = FoldList[0.9 #1 + #2 &, 0.01, RandomReal[NormalDistribution[0, 1], 24]]

{0.01, 0.334557, 2.02709, 1.1878, 1.9009, 3.08604, 2.36652, 3.04111, 
3.32364, 3.22662, 3.12626, 2.59118, 1.69334, 1.21069, 0.23171, 
0.689415, -0.852649, -0.124624, 0.58604, -0.481886, 0.221074, 
-0.300329, 2.36137, 0.427789, -1.47747}

dists = RandomChoice[{3, 4}, Length[arList]]
{4, 4, 4, 3, 4, 3, 4, 3, 4, 4, 3, 4, 4, 3, 4, 4, 4, 4, 3, 4, 3, 3, 3, 3, 3}

结果:

RectangleChart[Transpose[{dists - 0 - 0/2, arList}], 
 PlotRange -> {{0, 100}, {-2, 4}}, ChartStyle -> EdgeForm[None], 
 Frame -> True, GridLines -> Automatic, BarSpacing -> {0, 0}]

RectangleChart[Transpose[{dists - 0.7 - 0.5/2, arList}], 
 PlotRange -> {{0, 100}, {-2, 4}}, ChartStyle -> EdgeForm[None], 
 Frame -> True, GridLines -> Automatic, BarSpacing -> {0.7, 0.5}]

请注意,数据在x轴上的跨距与上一个示例不同.

Notice how the data aren't spanning the same distance along the x-axis as the previous example.

当尝试绘制多个序列的图表时,它甚至变得更加混乱(在此示例中,该示例也是如此).

It gets even messier when trying to chart multiple series (the same in this example, for illustration).

RectangleChart[
 Transpose[{{dists - i/2 - j/2, arList}, {dists - i/2 - j/2, 
  arList}}, {2, 3, 1}], PlotRange -> {{0, 180}, {-2, 4}}, 
 ChartStyle -> EdgeForm[None], Frame -> True, Ticks -> None, 
  GridLines -> Automatic, BarSpacing -> {i, j}]

我一直在努力寻找正确的公式,以使自定义函数的BarSpacing设置(此处未显示)引起正确的间距和条形宽度,因此水平绘图范围不会随着BarSpacing可以.

I've been futzing for ages trying to find the right formula so that BarSpacing settings for the custom function (not seen here) induce the correct spacings and bar widths so that the horizontal plot range doesn't change as the BarSpacing does.

我想念什么?

针对belisarius,这是我要去的地方的一个例子.它可以正常工作(条形图与线条不太对齐,但这可能是我使用的日期),但是堆叠条形图的情况无法像任何类型的条形图那样绘制它们应在的位置条形图本身有多个系列. (我为日期标签放置算法感到非常自豪:正在发挥作用的功能并不想放弃这种外观.)

In response to belisarius, this is an example of where I am heading. It works, kind of (the bars aren't quite in alignment with the line, but this is probably the dates I am using) but the cases with stacked bars fail to plot with the bars where they should be, as do any kind of bar graph on its own where there are multiple series. (I'm quite proud of the date label placement algorithm: the powers that be at work don't want to give up that look.)

这是一个行不通的文件.数据应填充水平范围. (不同的宽度条是有意的,它是年度和季度数据的组合.)

And here is one that just isn't working. The data should fill the horizontal range. (The different width bars are deliberate - it's a combination of annual and quarterly data.)

编辑2

我记得为什么我没有像Mike Honeychurch的包装那样在DateListPlot中使用Filling绘制条形-如果您除了很细的条形之外还拥有其他任何东西,它们最终会在错误的位置放置顶部边缘

I remember why I didn't use Filling in a DateListPlot to draw the bars as in Mike Honeychurch's package - if you have anything other than very skinny bars, they end up having the top edge in the wrong place.

DateListPlot[{dateARList}, 
 PlotStyle -> {AbsolutePointSize[6], Yellow}, Filling -> {1 -> 0}, 
 FillingStyle -> {1 -> {{AbsoluteThickness[12], Darker[Red, 0.25]}}}, 
 PlotRange -> All]

推荐答案

也许使用 ChartElementFunction 选项代替 BarSpacing 会有所帮助.例如,代码中的barplot将绘制条形图,以使每个条形在左边具有gapl且在右边具有gapr的边距,其中gaplgapr是条形的总宽度的分数

Maybe using the ChartElementFunction option instead of BarSpacing helps. For example barplot in the code would plot a bar chart such that each bar has margins of gapl on the left and gapr on the right where gapl and gapr are fractions of the total width of the bar

scale[{{xmin_, xmax_}, {ymin_, ymax_}}, {gapl_, gapr_}] :=
 {{xmin (1 - gapl) + xmax gapl, ymin}, {xmax (1 - gapr) + xmin gapr, ymax}}

barplot[dists_, arList_, {gapl_, gapr_}, opts___] := 
 RectangleChart[Transpose[{dists, arList }], opts, 
  Frame -> True, 
  GridLines -> Automatic, BarSpacing -> 0,
  ChartElementFunction -> (Rectangle @@ scale[#, {gapl, gapr}] &)]

用法:

要绘制没有间隙的原始条形图

To plot the original bar chart with no gaps

barplot[dists, arList, {0, 0}]

这将绘制一个条形图,其两侧的边距为0.2,这导致条形图的间隙为条形总宽度的0.4倍.请注意,这些条的位置与第一个图中的位置相匹配.

This would plot a bar chart with a margin of 0.2 on both sides which results in a bar chart with gaps of 0.4 times the total width of the bars. Note that the positions of the bars matches with those in the first figure.

barplot[dists, arList, {0.2, 0.2}]

您可以通过执行类似的操作来绘制多个序列

You can plot multiple series by doing something like

Show[barplot[dists, arList 0.9, {0, 0.5}],
 barplot[dists, arList 0.8, {0.5, 0}, ChartStyle -> LightGreen]] 

这篇关于在Mathematica中如何真正实现BarSpacing选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆