如何在 NetLogo 中创建条形图,其中条形并排

use*_*791 2 netlogo

我有两个列表,我想将它们绘制为侧栏:

[44.44 28.57 50 22.72 37.52]
[64.10 75 76.19 55.55 72.22]
Run Code Online (Sandbox Code Playgroud)

我希望得到如下图所示的结果,但考虑到可用的绘图原语,我不明白如何在 netlogo 中实现此目的:“直方图”在这里绝对没有用,而“绘图”需要数据来是单个值而不是列表。

在此输入图像描述

小智 5

这确实不是一项简单的任务。首先,您不会在 x 轴右侧获得 x 标签,因为 NetLogo 图无法处理分类轴尺度(无法跳过组之间的间隙)。但是,可以使用 NetLogo 创建填充条形图。我前段时间写了一个程序并根据您的需要进行了调整:

to go

  ca
  ;; Define plot variables:
  let y.a [44.44 28.57 50 22.72 37.52]
  let y.b [64.10 75 76.19 55.55 72.22]
  let plotname "plot 1"
  let ydata (list y.a y.b)
  let pencols (list 16 56)
  let barwidth 2
  let step 0.01
  ;; Call the plotting procedure
  groupedbarplot plotname ydata pencols barwidth step

end

to groupedbarplot [plotname ydata pencols barwidth step]

  ;; Get n from ydata -> number of groups (colors)
  let n length ydata
  let i 0
  ;; Loop over ydata groups (colors)
  while [i < n]
  [
    ;; Select the current ydata list and compute x-values depending on number of groups (n), current group (i) and bardwith
    let y item i ydata
    let x n-values (length y) [? -> (i * barwidth) + (? * (((n + 1) * barwidth)))]
    print y
    print x

    ;; Initialize the plot (create a pen, set the color, set to bar mode and set plot-pen interval)
    set-current-plot plotname
    create-temporary-plot-pen (word i)
    set-plot-pen-color item i pencols
    set-plot-pen-mode 1
    set-plot-pen-interval step

    ;; Loop over xy values from the two lists:
    let j 0
    while [j < length x]
    [
      ;; Select current item from x and y and set x.max for current bar, depending on barwidth
      let x.temp item j x
      let x.max x.temp + (barwidth * 0.97)
      let y.temp item j y

      ;; Loop over x-> xmax and plot repeatedly with increasing x.temp to create a filled barplot
      while [x.temp < x.max]
      [
        plotxy x.temp y.temp
        set x.temp (x.temp + step)
      ] ;; End of x->xmax loop
      set j (j + 1)
    ] ;; End of dataloop for current group (i)
    set i (i + 1)
  ] ;; End of loop over all groups

end
Run Code Online (Sandbox Code Playgroud)

您可能必须在go过程中调整绘图名称,然后执行该go过程才能进行绘图。此方法的优点之一是,对于要绘制的彼此相邻的组的数量而言,它非常灵活。您只需提交 y 坐标列表(嵌套在一个大列表中的每个组一个列表)以及每个组相应的画笔颜色列表。您只需添加更多 y 列表和颜色即可绘制更多组。groupedbarplot 过程循环遍历这些组并计算匹配的 x 坐标(基于组数和条形宽度)。绘图本身嵌套在另一个循环中,以随着 x 坐标的增加(增量步骤)重复绘制 y 坐标。这会创建许多彼此相邻的小条形,以产生填充条形图的错觉。