Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you generate a histogram in NetLogo?

Tags:

netlogo

I have a breed called players. Each player-owns a player-strategy. There are 6 possible player-strategies. When I write

histogram [player-strategy] of players

nothing appears on my plot.

I have one plot with one pen -- and don't set either the plot or penname. plot-name returns the plot. pen-mode is bar. interval is 1.0

I don't know how to ask for the current penname. But since the plot has only one pen it shouldn't matter.

The histogram command is in both plot-update and pen-update. I've tried it in both together or one at a time. I also tried in code. Makes no difference.

What do I have to do to get a histogram to appear?

like image 419
RussAbbott Avatar asked Mar 06 '18 03:03

RussAbbott


1 Answers

OK, here is what I think you are after.

If you create the following plot:

plot setup

And write the following code:

extensions [table]
breed [players player]
players-own [player-strategy]

to setup
  clear-all
  create-players 100 [ set player-strategy one-of ["a" "b" "c" "d" "e"] ]
  reset-ticks
end

to update-strategy-plot
  set-current-plot "Player strategies"
  clear-plot
  let counts table:counts [ player-strategy ] of players
  let strategies sort table:keys counts
  let n length strategies
  set-plot-x-range 0 n
  let step 0.05 ; tweak this to leave no gaps
  (foreach strategies range n [ [s i] ->
    let y table:get counts s
    let c hsb (i * 360 / n) 50 75
    create-temporary-plot-pen s
    set-plot-pen-mode 1 ; bar mode
    set-plot-pen-color c
    foreach (range 0 y step) [ _y -> plotxy i _y ]
    set-plot-pen-color black
    plotxy i y
    set-plot-pen-color c ; to get the right color in the legend
  ])
end

You should get the following plot:

plot result

I believe this should answer your previous question as well.

(Note that I've put the plotting code in a code tab procedure, but you could just as well stick it in the "Plot update commands" field.)

like image 76
Nicolas Payette Avatar answered Oct 02 '22 06:10

Nicolas Payette