bhjghjh bhjghjh - 1 month ago 4
Python Question

TypeError: unsupported operand type(s) for /: 'float' and 'list', can't resolve

I am trying to generate a plot in python to determine velocity of planets, V = sqrt(GM/r). My approach is this:

I can generate few hundred points for 'r' :

r = range (57909227, 5906440628, 10000000)

using the largest and smallest values of 'r', which have already been given. the values of G and M are also given. my code looks like this :

import sys
import os
import numpy
import matplotlib.pyplot as plt
from pylab import *


radial_distance = range (57909227, 5906440629, 10000000)

for distance in radial_distance:
plt.plot(radial_distance, ((G*M)/distance)**0.5, '*')

which returns me the following error:

File "", line 20, in <module>
plt.plot(radial_distance, ((G*M)/distance)**0.5, '*')
File "/home/trina/anaconda2/lib/python2.7/site-packages/matplotlib/", line 3154, in plot
ret = ax.plot(*args, **kwargs)
File "/home/trina/anaconda2/lib/python2.7/site-packages/matplotlib/", line 1812, in inner
return func(ax, *args, **kwargs)
File "/home/trina/anaconda2/lib/python2.7/site-packages/matplotlib/axes/", line 1424, in plot
for line in self._get_lines(*args, **kwargs):
File "/home/trina/anaconda2/lib/python2.7/site-packages/matplotlib/axes/", line 386, in _grab_next_args
for seg in self._plot_args(remaining, kwargs):
File "/home/trina/anaconda2/lib/python2.7/site-packages/matplotlib/axes/", line 364, in _plot_args
x, y = self._xy_from_xy(x, y)
File "/home/trina/anaconda2/lib/python2.7/site-packages/matplotlib/axes/", line 223, in _xy_from_xy
raise ValueError("x and y must have same first dimension")
ValueError: x and y must have same first dimension

please help me fix the error.


What you need is to apply an expression element-wise on the list and then pass the resultant list to plot.

y = [((G*M)/x)**0.5 for x in radial_distance]
plt.plot(radial_distance, y)