Rex Yeigh Rex Yeigh - 1 year ago 132
Python Question

Generating Plots from .csv file

I'm trying to make several plots of Exoplanet data from NASA's Exoplanet Archive. The problem is nothing I do will return the columns of the csv file with the headers on the first line of the csv file.

The Error I get is

NameError: name 'pl_orbper' is not defined


The DATA I need to use.

The code I have currently has not worked though I'm sure I'm close.

import matplotlib.pyplot as plt
import numpy as np

data = np.genfromtxt("planets.csv",delimiter=',',names=True, unpack=True)

plt.plot(pl_orbper,pl_bmassj)
plt.title('Mass vs Period')
plt.ylabel('Period')
plt.xlabel('Mass')

plt.show()


If anyone has a better solution with csv.reader or any other ways to read csv files I'd be open to it.

Answer Source

Change the following line

plt.plot(pl_orbper,pl_bmassj)

to

plt.plot(data['pl_orbper'],data['pl_bmassj'])

With the following data:

rowid,pl_orbper,pl_bmassj
1, 326.03, 0.320
2, 327.03, 0.420
3, 328.03, 0.520
4, 329.03, 0.620
5, 330.03, 0.720
6, 331.03, 0.820

this is the result

Mass vs Period Plot

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download