bhjghjh - 1 year ago 46

Python Question

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 *

G=6.67*10**(-11)

M=2.0*10**30

radial_distance = range (57909227, 5906440629, 10000000)

for distance in radial_distance:

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

plt.show()

which returns me the following error:

`File "generate_list.py", line 20, in <module>`

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

File "/home/trina/anaconda2/lib/python2.7/site-packages/matplotlib/pyplot.py", line 3154, in plot

ret = ax.plot(*args, **kwargs)

File "/home/trina/anaconda2/lib/python2.7/site-packages/matplotlib/__init__.py", line 1812, in inner

return func(ax, *args, **kwargs)

File "/home/trina/anaconda2/lib/python2.7/site-packages/matplotlib/axes/_axes.py", line 1424, in plot

for line in self._get_lines(*args, **kwargs):

File "/home/trina/anaconda2/lib/python2.7/site-packages/matplotlib/axes/_base.py", 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/_base.py", line 364, in _plot_args

x, y = self._xy_from_xy(x, y)

File "/home/trina/anaconda2/lib/python2.7/site-packages/matplotlib/axes/_base.py", 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.

Answer Source

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)
```