Help in plotting histogram

25 views
Skip to first unread message

Ronald Ng

unread,
Nov 8, 2022, 2:37:43 AM11/8/22
to netlogo-users
Dear All, 

I need help in plotting histogram from two variables. 

My turtles have two "turtles own"  variables. They are both numeric variables, AAA and BBB. 

How does one write the codes for plotting BBB against AAA?

Thanks,

Ronald Ng

Wade Schuette

unread,
Nov 8, 2022, 5:43:03 AM11/8/22
to Ronald Ng, netlogo-users
One way to do that kind of "scatterplot" ( not "histogram") is to use Python's MatPlotLib which gives you complete
control over color, size,shape, alpha, of the plot.  Of course you have to have python installed and you need to
point your pathway to the python executable, once,  as described in the NetLogo Documentation.

Documentation on scatterplot in MatPlotLib

If you do that, the following Netlogo code below will produce this if you run setup, then "testplot"
Figure_1.png

and this if you run setup then "scatterplot" which reads the AAA and BBB values from turtles
and plots it.


Figure_2.png
Here's the total Netlogo code that runs correctly for me. I think you need
Netlogo 6.3, as 6.2x had some issue with python.

;;============= Netlogo 6.3 code =======
extensions [py]
turtles-own [ AAA BBB ]
to setup
  clear-all
  create-turtles 10 [ set AAA who set BBB (who ^ 1.3) ]
  reset-ticks
end
to  scatterplot
  print "Setting up python now"
  py:setup py:python
  let xlist [ ]
  let ylist [ ]
  ask turtles [ set xlist lput AAA xlist   set ylist lput precision BBB 3 ylist]
  print xlist
  print ylist
  py:set "x" xlist
  py:set "y" ylist
  carefully [
  (py:run
  "import matplotlib"
  "matplotlib.use('TkAgg')"
  "import numpy as np"
  "import matplotlib.pyplot as plt"
  "plt.scatter(x, y )"
  "plt.show()"
)
  ] [ print "ran into some problem running python." ]
 py:run "print('python finished')"
 print "End of plot routine"
end
to  testplot
   print "Setting up python now"
  py:setup py:python
  show py:runresult "1 + 1"
  carefully [
  (py:run
  "import matplotlib"
  "matplotlib.use('TkAgg')"
  "import numpy as np"
  "import matplotlib.pyplot as plt"
  "N = 50"
  "x = np.random.rand(N)"
  "y = np.random.rand(N)"
  "colors = np.random.rand(N)"
  "area = (30 * np.random.rand(N))**2  "
  "plt.scatter(x, y, s=area, c=colors, alpha=0.5)"
  "plt.show()"
)
  ] [ print "ran into some problem running python." ]
 py:run "print('python finished')"
 print "End of plot routine"
end
;;====================

Wade

--
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/CAE1erW7n%3DECbcJbFEwhDd_ZBw_yQd3EhTMuNnjhk_x9m1bEzbg%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages