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:
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.