Re: [netlogo-users] Netlogo - How to give patches different colors

281 views
Skip to first unread message

Jacob Kelter

unread,
Jan 11, 2020, 5:52:25 PM1/11/20
to Tobias Koch, netlogo-users
Could you specify which part of your current code attempts to do what you are saying? Also is it just random which patches will have each color or is it linked to some data?

On Sat, Jan 11, 2020 at 4:17 AM Tobias Koch <tobik...@gmail.com> wrote:
Hello everyone,

I'm working on a model in Netlogo and I'm trying to give different patches different colors. For example, I want to give around 50% of the patches in a certain area the color green, 25% the color gray and 25% the color blue.
Here is my code:

extensions [ gis ]

globals [
  wien-grenzen
  temperature
]

patches-own [
  random-n
  centroid
  ID
  patch-type
]

breed [rays ray]
breed [IRs IR]
breed [heats heat]
breed [CO2s CO2]
breed [suns sun]
breed [trees tree]
breed [peoples people]
breed [industries industry]
breed [cars car]
suns-own [sun-speed]

to setup
  clear-all
  set-default-shape rays "ray"
  set-default-shape IRs "ray"
  set-default-shape heats "dot"
  set-default-shape CO2s "CO2-molecule"
  set-default-shape suns "sun"
  set-default-shape trees "tree"
  set-default-shape industries "industry"
  setup-map
  set-surface
  set temperature 0
  reset-ticks
  plot temperature
end

to setup-map
  clear-all
  set wien-grenzen gis:load-dataset "Bezirksgrenzen.shp"
  gis:set-world-envelope (gis:envelope-of wien-grenzen)
  let i 1
  foreach gis:feature-list-of wien-grenzen [ feature ->
    ask patches gis:intersecting feature [
;      set centroid gis:location-of gis:centroid-of feature
;      ask patch item 0 centroid item 1 centroid [
;        set ID i
;      ]
      set ID i
    ]
    set i i + 1
  ]
  gis:set-drawing-color white
  gis:draw wien-grenzen 1.5
;  create-suns 1
; [
;   set color yellow
;   set size 6
;   setxy (min-pxcor + 2) 22.6
;   set heading 90
;   set sun-speed 0.025
; ]
end

to set-surface
  ask patches with [ID > 0] [
    set random-n random-float 15
    ifelse random-n >= 7
    [
      gis:set-drawing-color green + 1.5
      set pcolor green + 1.5
    ]
    [
      gis:set-drawing-color gray + 2.5
      set pcolor gray + 2.5
    ]
  gis:set-drawing-color white
  gis:draw wien-grenzen 1.5
end

to go
  run-sunshine
  run-CO2
;  run-heat
;  run-IR
  tick
  plot temperature
  ask suns [
    run-sun
  ]
  ask trees [
    catch-CO2s
  ]
  ask industries [
    emmit-CO2s
  ]
end
I also added a picture of the world, where I'm trying to set different colored patches. Thank you for any help.

--
You received this message because you are subscribed to the Google Groups "netlogo-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netlogo-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/netlogo-users/edcc8408-e963-4e64-b7ea-20dc22baa29c%40googlegroups.com.


--

Paolo Gaudiano (Aleria)

unread,
Jan 11, 2020, 8:02:49 PM1/11/20
to netlogo-users, Tobias Koch
if you just want random coloring, here is a trick I have used: 

* Create an empty list
* Add to it a pretty large set of numbers (1000 should be good) containing the color values, represented by your desired frequency. In your case you would add 500 greens, 250 greys and 250 blues. Note that the exact numbers really don’t matter, and you can use color names or numbers (the list will show the color numbers even if you specified the names)
* Ask each patch that you want randomly colored to choose a random element of that list

Here is some simple code that does that (also attached). I used the anonymous procedure foreach - could probably also have done it with map or with a more pedestrian set of repeats.

to setup ; Do this each time the setup button is clicked
  let color-distributions (list [500 green] [250 grey] [250 blue]) ; Here you specify whatever distribution of colors you want. You can add more pairs, change totals, ...
  let color-list [] ; Make this an empty list
  let num 0 ; These are just the index of the number and color in the sublists for clarity
  let col 1
  
  ; Now fill the the list with the total colors specified
  foreach color-distributions [
    ; This uses the anonymous procedure "foreach" to fill the list with the right color values
    x -> repeat ( item num x ) [ set color-list lput (item col x )  color-list ]
  ]
  
  ; Now ask each patch to choose a random color value from the list
  ask patches [
    set pcolor one-of color-list
  ]
end

Of course instead of asking all patches, you could ask any subset of patches that you want to have colors.

Paolo



Aleria   |  QSDI   |  ARC  |  Forbes   | TEDx   | @icopaolo   | LinkedIn   |   +1-646-862-2934  |    Pronouns:  He/Him/His
Subscribe to our  weekly D&I Newsletter
Random-Color-Patches.nlogo

Dale Frakes

unread,
Jan 11, 2020, 9:23:24 PM1/11/20
to netlog...@googlegroups.com
Just for fun, this particular problem could be solved in an even simpler way:

to setup
 
  let num-patches count patches  ;; get the total number of patches

  ask patches [ set pcolor black ]  ;; start out with all black to keep track of which haven't been assigned

  ask n-of ( 0.5 * num-patches ) patches [set pcolor green ]  ;; randomly ask half to be green
  ask n-of ( 0.25 * num-patches ) patches with [pcolor = black ] [set pcolor blue] ;; randomly ask 1/2 of remaining (1/4) to be blue
  ask patches with [pcolor = black ] [set pcolor grey ]       ;; ask the rest to be grey
 
  ;; show what we got
  show count patches with [ pcolor = green ]
  show count patches with [ pcolor = blue ]
  show count patches with [ pcolor = grey ]
 
 
end

The last 3 lines are just to show the counts and not necessary.
To view this discussion on the web visit https://groups.google.com/d/msgid/netlogo-users/etPan.5e1a7032.75b5864.4482%40aleria.tech.

-- 
Dale Frakes
Adjunct Instructor, PhD Candidate
PSU Systems Science
dfr...@pdx.edu - http://web.pdx.edu/~dfrakes/
Reply all
Reply to author
Forward
0 new messages