我的代码中不断收到此错误“ValueError:x 和 y 必须大小相同”,不确定我哪里出错/如何修复?

问题描述 投票:0回答:0

plt.scatter(year_lst, max_temp_year_lst, color = "black") 是我发现“ValueError: x and y must be the same size”错误信息出现的地方 这是我的最新尝试,我尝试在代码本身中绘制 max_year 和 max_temp,例如: plt.scatter(year_lst, max_temp, max_year, color = "black") 但这也给了我同样的错误信息。

NOLA_WEATHER_FILE = "nola_weather_feb.csv"
MAX_TEMP_POSITION = 1
MIN_TEMP_POSITION = 2
YEAR_POSITION = 0
import matplotlib.pyplot as plt
def main():
    year_lst = []
    high_temp_lst = []
    low_temp_lst = []
    max_temp = 0
    max_year = 0
    # gather data - read the file and create and append lists 
    # for year, high temp, and low temp
    with open(NOLA_WEATHER_FILE, "r") as infile:
        infile.readline()
        for line in infile:
            lst = line.split(",")
            high_temp = int(lst[MAX_TEMP_POSITION])
            low_temp = lst[MIN_TEMP_POSITION]
            year = int(lst[YEAR_POSITION])
            high_temp_lst.append(int(high_temp))
            low_temp_lst.append(int(low_temp))
            year_lst.append(int(year))
            # computation find the year with the highest 
            # temp and keep record of both max temp and what year it occured
            if high_temp > max_temp:
                max_temp = high_temp
                max_year = year
    # sanity checking if correct max year and temp is printed
    max_temp_year_lst = (max_temp, max_year)
    print(max_temp_year_lst)
    # communication - creating a labled scatter plot
    plt.scatter(year_lst, high_temp_lst, color = "red")
    plt.scatter(year_lst, low_temp_lst, color = "blue")
    plt.scatter(year_lst, max_temp_year_lst, color = "black")
    plt.xlabel("Years")
    plt.ylabel("Temperature in Degrees")
    plt.title("Weather of Mardi Gras over the Years!")
    plt.show()
main()
valueerror
© www.soinside.com 2019 - 2024. All rights reserved.