0
import pandas as pd
import matplotlib.pyplot as plt

# Plotting Avg. Area Income vs Price

plt.plot(usa_housing['Avg. Area Income'], usa_housing['Price'], color = 'red', marker = 'o')

# Giving title and label names
plt.title('Avg. Area Income Vs Price', fontsize = 14)
plt.xlabel('Avg. Area Income', fontsize = 14)
plt.ylabel('Price', fontsize = 14)
plt.grid(True)
plt.show()

Output return:

NameError                                 Traceback (most recent call last)

~\AppData\Local\Temp\ipykernel_12188\450110948.py in <module>

      3 # Plotting Avg. Area Income vs Price

      4 

----> 5 plt.plot(usa_housing['Avg. Area Income'], usa_housing['Price'], color = 'red', marker = 'o')

      6 

      7 # Giving title and label names

NameError: name 'usa_housing' is not defined

I want to get a plot of Avg. Area Income against Price

JohanC
  • 71,591
  • 8
  • 33
  • 66
Niyioxy
  • 1
  • 1
  • Well, you first need to read your data into a dataframe. Somehting like `usa_housing = pd.read_csv(.....filename....)`. – JohanC Dec 01 '22 at 14:49
  • I am still getting the same error JohanC. – Niyioxy Dec 01 '22 at 15:24
  • File name is USA_Hosing.csv right? I ahve been stucked here for days now. I need help – Niyioxy Dec 01 '22 at 15:25
  • If the file isn't in the "current" directory, you would need to give the full path. You can't get the *same* error if you assign `usa_housing = pd.read_csv('............../USA_Hosing.csv')` before calling `plt.plot`. Note that I have no way to know how your file is named. – JohanC Dec 01 '22 at 15:34
  • "current folder?" please clarify. in the tutorial i am using a different name is used. – Niyioxy Dec 01 '22 at 15:41
  • 1. # Plotting Avg. Area Income vs Price 2. plt.plot(housing_price[‘Avg. Area Income’], housing_price[‘Price’], color=’red’, marker=’o’) 3. 4. # Giving title and label names 5. plt.title(‘Avg. Area Income Vs Price’, fontsize=14) 6. plt.xlabel(‘Avg. Area Income’, fontsize=14) 7. plt.ylabel(‘Price’, fontsize=14) 8. plt.grid(True) 9. 10. plt.show() this is the code the tutor used. – Niyioxy Dec 01 '22 at 15:45
  • Well, you didn't copy the part where your tutor read the csv file and assigned it to a dataframe named `housing_price`. Maybe your tutor put that code somewhere earlier in the chapter. The concept of a "current folder" (or directory) is both elementary, and at the same time hard to explain if you don't know it already. See e.g. https://stackoverflow.com/questions/45591428/what-exactly-is-current-working-directory – JohanC Dec 01 '22 at 15:55
  • I am still getting the same error after doing the needful: import pandas as pd import matplotlib.pyplot as plt spreadsheet = pd.read_csv(r'C:\Users\Hamaboi Care Limited\Documents\CSV\USA_Housing.csv') I got the same error message: NameError: name 'USA_Housing' is not defined The path is correct. Please help. – Niyioxy Dec 02 '22 at 15:30
  • Instead of `spreadsheet = pd.read_csv(...)` you could write `USA_Housing = pd.read_csv(...)` – JohanC Dec 02 '22 at 15:44
  • Thanks JohanC it worked perfectly. Can you take me n as a student or protégé? – Niyioxy Dec 03 '22 at 12:59

0 Answers0