Netlogo如何制作有序直方图

Netlogo how to make an ordered histogram

我想制作海龟变量的直方图,但是条形从大到小排列,我将其作为示例

breed [birds bird]        ; living birds

birds-own [ species]

to setup

  ask n-of 2000 patches [
    sprout-birds 1 [ set shape "circle"
                     set species random max-birds-species
                     set color scale-color white species ( max-birds-species + 1 ) 0]
  ]
  reset-ticks
end


to go
  ask birds [
  ifelse random-float 1 > gr-birds
    [ die ]
    [
      let target one-of neighbors4 with [not any? birds-here]
      if target != nobody [
        hatch-birds 1 [ move-to target ]
      ]
    ]
  ]
  tick
end

然后,如果我绘制直方图,它不是按频率排序的,我想在左侧和降序中看到最常见的物种。

您无法直接使用 histogram 命令执行此操作,但 table 扩展程序提供了方便的 table:group-agents 报告器,可让您相对轻松地执行此操作.

只需将其放入绘图笔更新命令中即可:

plot-pen-reset
let counts map count table:values table:group-agents birds [ species ]
foreach (reverse sort counts) plot

(别忘了将笔模式更改为 "Bar"。)

这里大致介绍了它的工作原理:

  • table:group-agents 会给你一个 table,其中键是物种 ID,值是具有相应物种 ID 的鸟类的代理集。
  • table:values 丢弃密钥,只留下一个代理集列表。
  • map count 提取每个代理集中的鸟类数量,为您留下一个计数列表。
  • reverse sort counts 将该列表从大到小排序。
  • foreach (reverse sort counts) plot 为每个数字画一个柱形。