How is the BarSpacing option really implemented in Mathematica?

笑着哭i 提交于 2019-11-30 20:41:24
Heike

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}] &)]

Usage:

To plot the original bar chart with no gaps

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

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]] 

You may relieve your complaint about FillingStyle by using CapForm["Butt"].

list = {0.01, -0.81, 0.12, 0.81, 1.79, 1.1, 0.41, 1., 1.33, 1.08, 
  2.16, 1.13, 1.92, 1.64, 1.31, 1.94, 1.71, 0.91, 2.32, 0.95, 1.29, 
  1.28, 2.97, 4.45, 5.11}

DateListPlot[list, {2000, 8}, 
 PlotStyle -> {AbsolutePointSize[6], Yellow}, Filling -> {1 -> 0}, 
 FillingStyle -> {1 -> {{CapForm["Butt"], AbsoluteThickness[14], 
      Darker[Red, 0.25]}}}, PlotRange -> {0, 6}, ImageSize -> 400]

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!