Search code examples
netlogo

How can I plot several distributions in the same window?


I am trying to plot the age distributions of different types of turtles. I use the Plot interface and add a pen to plot the age distribution for the entire population of turtles. I set the Plot update commands for that pen to histogram [ ticks - birth-tick ] of turtles.

Also, I set the Plot setup commands to set-histogram-num-bars 50.

Now, when I add a pen for a sub-population of turtles, e.g., histogram [ ticks - birth-tick ] of turtles with [ shape = "circle" ], the distribution gives a flat line at y = 0.

If I reverse the order of both pens, only the sub-population is correctly shown, while the entire population isn't. As such, it seems like only the first pen is correctly evaluated.

Here is one possible clue: I do manage to get multiple distributions for different sub-populations, when evaluating a different distribution where I use count, for instance, histogram [count link-neighbors] of turtles and histogram [count link-neighbors] of turtles with [ shape = "circle" ]. Also, here it seems I do not need to set set-histogram-num-bars 50.


Solution

  • If you get a line, I guess you just picked the wrong mode for that pen. In my opinion, it is easier to write the plotting for more comprehensive plots as an own procedure in the code tab.

    Here is an example assuming, that you created a plot called "plot 1" with two pens called "first" and "second":

    to setup 
      clear-all
      create-turtles 10 [
        set shape "circle"
        setxy random-xcor random-xcor
      ]
      create-turtles 10 [
        setxy random-xcor random-xcor
      ]
    end
    
    to plot-hist
      set-current-plot "plot 1" ; choose plot by its name
      clear-plot
      
      set-current-plot-pen "first" ; choose pen by its name
      set-plot-pen-mode 1 ; for bar
      set-histogram-num-bars 2 ; defin number of bars
      histogram [ xcor ] of turtles ; define what to plot
    
      set-current-plot-pen "second"
      set-plot-pen-mode 1 ; for bar
      set-histogram-num-bars 5
      histogram [ xcor ] of turtles with [shape = "circle"]
    end