简体   繁体   中英

Plotting scatter points with matplotlib from a CSV file question

I have extracted some data from a website into a CSV file and I need to plot a scatterplot in matplotlib from that CSV file. I only need column 2 and 3 data from the CSV file.

I'm trying to use a for loop to gather CSV data into a list and then use that to plot the scatterplot but I'm getting a "ValueError: x and y must be the same size" error.

import matplotlib.pyplot as plt
import csv

with open(cache_path + distance_csv) as csv_file:
reader = csv.reader(csv_file)

for column in reader:
    city_distance = [x[1] for x in csv.reader(csv_file)]
    crime_rate = [x[2] for x in csv.reader(csv_file)]

    plt.scatter(city_distance, crime_rate)
    plt.show()

Both columns 2 and 3 in my CSV file are the same length - 83 cells yet I am getting a ValueError. What am I missing here?

You have some bugs in your code, I can't know which of them is causing your behaviour but after you fix them all you could progress:

  1. First, look how you read the columns. Notice that you iterate over column in reader but never use column (you create two new csv.readers in every iteration in the for loop). Look at a possible solution afterwards in this answer.
  2. Regarding to that, you're reading csv_file outside the scope of the 'with' statement so the file would be already closed. If you'll use the for loop and column you won't have to fix this issue anyway.
  3. You're plotting in every iteration (thus you'll create 83 plots and I guess you don't want that).

So a possible solution would be:

import matplotlib.pyplot as plt
import csv

with open(cache_path + distance_csv) as csv_file:
  reader = csv.reader(csv_file)
city_distance, crime_rate  = [], []
for column in reader:
  city_distance.append(float(column[1]))
  crime_rate.append(float(column[2]))
plt.scatter(city_distance, crime_rate)
plt.show()

For future, I'll recommend that you try to verify that len(city_distance)==len(crime_rate) . I mean, check your data not in the csv but rather in the code, after reading the values and right before the error - to have the most usable information to proceed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM