0

I am writting a program in python to plot planet's distances to the Sun vs orbital velocity. No problem with that. Then I have to plot in the same graph, a line with the function: v = GMsun/r, where v is orbital speed, G is Newton's Gravitational constant, Msun the mass of the Sun and r is distance. I use quantities library to keep dimensional consistency between units. When I try to plot that function, I get this error. Could you tell me where is the error?

import numpy as np 
import matplotlib.pyplot as plt 
import quantities as pq

#orbital velocity (km/s)
velocity= np.array([47.8725, 35.021, 29.7859, 24.130, 13.0697, 9.6724, 6.8352, 5.4778])

#Distance (UA)
distance = np.array([0.38709893, 0.72333199, 1., 1.52366231, 5.20336301, 9.53707032, 19.19126393, 30.06896348])

#Normalize so distance to Earth = 1
velocity2 = velocity / (29.7859)

#Calculate the equation:
Msun = 1.9891e30 * pq.kg #Sun mass
G = 1 * pq.constants.G #Gravitational constant

r = np.arange(0.1,34,0.1) * pq.au #Distance to Sun in Astronomical Units
Vc = np.sqrt(G*Msun/ r) #equation
Vc2 = Vc.rescale(pq.km / pq.s) / 29.7859 #convert to km/s and normalize to Earth's speed.
#Plotting:

fig = plt.figure() 
plt.plot(distance, velocity2, 'ro')
plt.plot(r, Vc2, 'b-')
plt.ylabel(r'Orbital Velocity (km/s)', fontsize = 18)
plt.xlabel(r'Distance to Sun (AU)', fontsize = 18)


plt.show()

The error doesn't show if I coment the line plt.plot(r, Vc2, 'b-'). the error is this, the first line repeats a lot, so I only show irt once:

File "/usr/lib64/python2.7/site-packages/matplotlib/units.py", line 148,  in get_converter
converter = self.get_converter(xravel[0])
File "/usr/lib/python2.7/site-packages/quantities/quantity.py", line 352, in __getitem__
return Quantity(ret, self._dimensionality)
File "/usr/lib/python2.7/site-packages/quantities/quantity.py", line 126, in __new__
ret = np.array(data, dtype=dtype, copy=copy).view(cls)
File "/usr/lib/python2.7/site-packages/quantities/quantity.py", line 215, in __array_finalize__
self._dimensionality = getattr(obj, 'dimensionality', Dimensionality())
RuntimeError: maximum recursion depth exceeded while calling a Python object
[Finished in 8.6s with exit code 1]

Thanks so much.

Gargoloso
  • 1
  • 3
  • Runs correctly on my machine (Ubuntu 14.04, stock python and stock packages) -- have you tried to update your python libraries with pip? – Ed King Jan 26 '15 at 05:04
  • Your code is working fine for me. You could try to increase the stack limit. See. http://stackoverflow.com/questions/8177073/python-maximum-recursion-depth-exceeded. – Kasisnu Jan 26 '15 at 05:10
  • Related, http://stackoverflow.com/questions/2917210/python-what-is-the-hard-recursion-limit-for-linux-mac-and-windows – Kasisnu Jan 26 '15 at 05:11
  • @EdKing I did it, it didn't work. – Gargoloso Jan 26 '15 at 17:29
  • @Kasisnu I did as in the link you provided, increasing the stackdepth allowed with: import sys sys.setrecursionlimit(10000) The error is no more, but the graph is not plotted at all and appears this: [Finished in 55.5s with exit code -11] – Gargoloso Jan 26 '15 at 17:38
  • Which operating system are you on? Do you see a plot if you comment the offending line out? You should still be seeing one. – Kasisnu Jan 26 '15 at 18:49
  • I am using Fedora 21 workstation. Yes, when I comment that line I get the plot with the red dots and the labels. – Gargoloso Jan 26 '15 at 23:28

1 Answers1

0

OK SOLVED: just removed and reinstalled pyplot, and everything runs fine now. Thanks.

Gargoloso
  • 1
  • 3