Leosar
Leosar

Reputation: 2072

Netlogo how to make an ordered histogram

I would like to make a histogram of a turtle's variable but with the bars ordered from bigger to lower, I made this as an example

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

Then if I plot the histogram it is not ordered by frequencies, I would like to see the most frequent species on the left and descending.

enter image description here

Upvotes: 0

Views: 346

Answers (1)

Nicolas Payette
Nicolas Payette

Reputation: 14972

You won't be able to do that directly with the histogram command, but the table extension offers the handy table:group-agents reporter that will allow you to do that relatively easily.

Just put this in you plot pen update commands:

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

(And don't forget to change the pen mode to "Bar".)

Here is more or less how it works:

  • table:group-agents will give you a table where the keys are species id and the values are agentsets of birds with the corresponding species id.
  • table:values discards the keys and leaves you with just a list of agentsets.
  • map count extracts the number of birds in each agentset, leaving you with a list of counts.
  • reverse sort counts sorts that list from the biggest to the smallest number.
  • foreach (reverse sort counts) plot draw a bar for each of those numbers.

Upvotes: 2

Related Questions