944,051 Members | Top Members by Rank

Ad:
  • Python Code Snippet
  • Views: 7955
  • Python RSS
0

Plotting math functions (Python)

by on Sep 1st, 2005
VPython's fame is with 3D animated modeling, but it's plotting abilities, while not flashy, are easy to understand and very useful. This program uses VPython to plot math functions y = sin(x) and y = cos(x) and y = sin(x)*cos(x).
Python Code Snippet (Toggle Plain Text)
  1. # simple plotting with VPython from http://vpython.org
  2. # for the Windows version and Python24 download: VPython-Win-Py2.4-3.2.3b.exe
  3. # tested with Python24 vegaseat 01sep2005
  4.  
  5. str1 = "blue is sin(x), red is cos(x), green is sin(x)*cos(x)"
  6.  
  7. import visual.graph as vg
  8.  
  9. # set up the plot display, if xmax,xmin,ymax,ymin are not specified then default is autoscale
  10. graph1 = vg.gdisplay(x=0, y=0, width=600, height=350, title = str1, xtitle='x', ytitle='y',
  11. xmax=5.0, xmin=-5.0, ymax=1.1, ymin=-1.1, foreground=vg.color.black, background=vg.color.white)
  12.  
  13. # gcurve() draws a line
  14. y1 = vg.gcurve(color=vg.color.blue)
  15. y2 = vg.gcurve(color=vg.color.red)
  16. y3 = vg.gcurve(color=vg.color.green)
  17.  
  18. # arange() is like range() with floating point values
  19. for x in vg.arange(-5, 5, 0.1):
  20. y1.plot( pos=(x, vg.sin(x)) )
  21. y2.plot( pos=(x, vg.cos(x)) )
  22. y3.plot( pos=(x, vg.sin(x)*vg.cos(x)) )
Message:
Previous Thread in Python Forum Timeline: Reading from a file question
Next Thread in Python Forum Timeline: weird exceptions:(





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC