RussAbbott
RussAbbott

Reputation: 2738

How do you generate a histogram in 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?

Upvotes: 7

Views: 3596

Answers (2)

Terry Weymouth
Terry Weymouth

Reputation: 51

I know it is a very old question, but in case anyone lands here on a search. The current version of NetLogo comes with example code for histogram usage: look in the file menu under 'Models Library'; see 'Code Examples'/'Histogram Example'.

Upvotes: 1

Nicolas Payette
Nicolas Payette

Reputation: 14972

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.)

Upvotes: 10

Related Questions