I started to use netlogo and I would like to add a legend to the model.
I didn't found any way to do it.
Is there some standard way to do it?
I thought about adding image to the interface but I didn't find any way to do it.
For example I want that user will know is the green particle means and what the read means
You can use the bitmap extension.
Here is a result example:
the code:
extensions [bitmap]
....
let legend bitmap:import "symbology.png"
bitmap:copy-to-drawing legend 40 275
"symbology.png" is the name of the graphics file, and 40 275 are the location coordinates. You will have to test several image sizes and coordinates until it suits your needs.
I don't know a great way to do this, the closest I've ever been able to get is to use a combination of specific plot set-ups and note widgets. As an example, start out with a plot called "Legend" with this setup to start:
Now size it to look something like this:
Now make a procedure to 'draw' legend guides (some detail in comments):
to setup-legend-plot
; Choose correct plot
set-current-plot "Legend"
clear-plot
; Define starting y and color
let starts [ [ 10 green ] [ 7 red ] [ 4 blue ] ]
; for each value in starts
foreach starts [ start ->
; make a range of values starting at the initial
; y value from 'starts'
let s first start
let f s - 2.5
let ran ( range s f -0.01 )
create-temporary-plot-pen "temp"
set-plot-pen-color last start
; draw lines at each y value to make it
; look like a solid drawing
foreach ran [ y ->
plot-pen-up
plotxy 1 y
plot-pen-down
plotxy 2 y
]
]
end
Calling setup-legend-plot should now make your "Legend" plot look like
Now you can make some note widgets and layer them over the blank space in the plot:
Not exactly straightforward, but definitely the closest I've found.
Edit:
"Closest I've found for building a legend within NetLogo." Javier's answer is a far better approach if you can swing it.
Related
I am building a model that tries to simulate a network-based market. In this model the turtles/nodes get a reward called points, which is a turtles-own variable.
I am now trying to plot a graph of the degree of the nodes against the average number of points that nodes with a given degree have. I have attempted to do this by creating a plot from the interface tab but I cannot manage to make this work.
Here are images of the windows of the plot settings.
Anybody know how can I make this work?
Also, I keep getting these "Runtime error: Can't find the maximum of an empty list" in all the plots/histograms I create. It is not a big deal at the moment as they seem to work fine, however if you know why these appear please let me know!
Thanks beforehand,
Carlos
For simplicity and to avoid overloading your plot setup, I like to use to-report procedures for things like this. As a quick example try this setup:
turtles-own [ points degree ]
to setup
ca
crt 50 [
set degree 5 + random 5
set points random 10
setxy random-xcor random-ycor
]
reset-ticks
end
Make a to-report each for a list of existing degrees, the average points of turtles that have each degree, and the maximum of those average point values:
to-report degrees-list
report sort remove-duplicates [degree] of turtles
end
to-report avg-points-list
let avg-list map [ i ->
mean [points] of turtles with [ degree = i ]
] degrees-list
report avg-list
end
to-report max-avg
report precision ( max avg-points-list + 1 ) 2
end
In this example, degrees-list reports [ 1 2 3 4 5 ], avg-points-list reports something like [6.5 3.9285714285714284 6 3.75 4.2], and max-avg reports something like 7.5- note that of course the exact values will vary since the setup is random.
Now, you can set up your plot window:
The actual plotting is handled by the foreach primitive in the plot pen, which uses plotxy to plot the point value in avg-points-list against the corresponding value in degrees-list. Should give a plot that looks something like:
Hope that's sort of what you're after!
Is it possible to have plots that are updated at different time points?
My model looks like that (it is replicator dynamics):
to go
repeat 10 [do-something]
replicate
tick
end
Then I would like to have one plot that is updated on tick, just like usual, and the second one that is updated at do-something and then reset at tick.
I tried to find a solution in documentation but update-plots command updates ALL plots which is not what i want.
You can use the various manual plotting commands (eg plotxy, plot-pen-down, plot-pen-up) to explicitly plot things, see the plotting section of the user manual, but is often simpler to have the plot commands in the plot rather than the code.
To do it in the plot instead of code, you could change to a structure like this (if your full code is suitable):
to go
do-something
if ticks mod 10 = 0 [ replicate ]
tick
end
and use the automatic update of plots with tick. You would also use the if ticks mod 10 = 0 trick in the update section of your plot.
I wish to draw an arrow at the top turtle representing a vector denoted by
[x1,y1] . The vector is of unit magnitude and the size of the arrow should not exceed that of the turtle.
The vector is stored in a list with two elements.
I don't wish to use the shape editor in netlogo to shape as arrow and then point the turtle in the heading denoted by the vector. The reason being I could draw one than 1 arrows for each turtle.
Edit:
Desired:
Bryan's answer gives the following:
Edit 2:
Video link : https://www.youtube.com/watch?v=9SVcLg4Oyoc&t=23 for better explanation.
Here's how I'd do it:
Make sure your turtles are all of one breed, say, particles, or whatever they represent. Create another turtles breed called vectors or something. These turtles are going to be the tip of your vectors but you'll use links to actually visualize the vectors. Now, you can create the vectors like so:
ask particles [
hatch-vectors 1 [
create-link-from myself
hide-turtle
]
]
To update the position of the vectors (given that the vector itself is stored in a turtle variable vec), you can do:
ask particles [
let abs-x xcor + first vec
let abs-y ycor + last vec
;; Since the particle is linked to the vector by a directed link, it's an out-link-neighbor
ask out-link-neighbors [ setxy abs-x abs-y ]
]
Edit in response to update:
That's tougher, since link shape editing is more limited than turtle shape editing. One possibility would be to set the shape of the vector turtles to an arrow head (you could either create a new such shape, or the default turtle shape could suffice). Rather than hiding the vectors, you'd then point them in the right direction. This can easily be done by having them face their link-partner and then turn around.
You may also want to switch from directed to undirected links to get rid of the arrow in the link itself. This should only involve minor code changes.
Is there a way to change the background color of a plot window in NetLogo?
(I want turtles to be black and white, and I'd like plot pens for statistics on the two kinds of turtles to match their colors. A white pen is not visible on a white background, obviously.)
Thanks.
NetLogo has not built-in way to change the background color of a plot. Arguably, this is something it should have. If you feel strongly enough about it, I would suggest sending a feature request to feedback#ccl.northwestern.edu or even opening an issue directly on GitHub.
Now, in the meanwhile, is there a way around it? Well, I feel almost dirty for even suggesting it, but you could do something like this:
Create a new plot pen of the color you wish your background to be, and set it to "line mode". This pen has to be the first one in your plot pen list so it's drawn before the other pens. (This might require deleting your other pens and recreating them, as NetLogo doesn't have an easy way to reorder plot pens, I think.)
Now put the following in your pen's update commands:
plot-pen-reset
let y plot-y-min
while [ y <= plot-y-max ] [
plotxy plot-x-min y
plotxy plot-x-max y
set y y + 0.05
]
This will draw lines, one by one, to fill your background. Depending on the size of your plot on the screen, you might want to play with the "interval" (0.05 here) to find the biggest value that doesn't leave white lines.
Be warned: this will slow down your model. If your plot axis are never rescaled, however, maybe you can get away with putting the code in your pen's setup commands so it's only executed once.
I have a matrix of fft data over time, 8192 rows of data x 600 columns of time. The first column is a frequency label, the first row is shown below but doesn't actually exist in the data file, neither do the spaces, they are shown just for ease of reading.
Frequency, Sec1, Sec2, Sec3...Sec600
1e8, -95, -90, -92
1.1e8, -100, -101, -103
...
It is plotted in matlab with the following code (Apologies to other posters, I grabbed the wrong matlab code)
x is a matrix of 8192 rows by 600 columns, f is an array of frequency labels, FrameLength = 1, figN = 3
function [] = TimeFreq(x,f,FrameLength,figN)
[t,fftSize] = size(x);
t = (1:1:t) * FrameLength;
figure(figN);
mesh(f,t,x)
xlabel('Frequency, Hz')
ylabel('time, sec')
zlabel('Power, dBm')
title('Time-Freq Representation')
I cant quite figure out how to make it work in gnuplot. Here is a sample image of what it looks like in Matlab: http://imagebin.org/253633
To make this work in gnuplot, you'll want to take a look at the splot (for "surface plot") command. You can probably figure out quite a lot about it just by running the following commands in your terminal:
$ gnuplot
gnuplot> help splot
Specifically, you want to read the help page shown by running (after the above, when the prompt asks for a subtopic): datafile. That should tell you enough to get you started.
Also, the answers to this question might be helpful.
so here is the gnuplot command script that I ended up using. It has some additional elements in it that weren't in the original matlab plot but all the essentials are there.
set term png size 1900,1080
set datafile separator ","
set pm3d
# reverse our records so that time moves away from our perspective of the chart
set xrange[*:*] reverse
# hide parts of the chart that would make the 3d view look funny
set hidden3d
# slightly roate our perspective and compress the z axis
set view 45,75,,0.85
set palette defined (-120 "yellow", -70 "red", -30 "blue")
set grid x y z
set xlabel "time (secs)"
set ylabel "frequency"
set zlabel "dBm"
# plot all the data
set output waterfall.png
splot 'waterfall.csv' nonuniform matrix using 1:2:3 with pm3d lc palette