I just need help with debugging what I have. I followed an example from our book as best to my understanding, but I am still having problems! I want to plot the time of sunrise and sunset throughout one year (2022) by importing the data from a file. I was able to figure out the importing stuff correctly (I hope), but when I run this in Pycharm (IDE I am using) I am still getting errors.
Specifically these ones:
Traceback (most recent call last): File "C:\Users\sosar\PycharmProjects\pythonProject1\main.py", line 27, in left_axis.plot(date, sunrise, label = "Sunrise") File "C:\Users\sosar\PycharmProjects\pythonProject1\venv\Lib\site-packages\matplotlib\axes_axes.py", line 1688, in plot lines = [*self._get_lines(*args, data=data, **kwargs)] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\sosar\PycharmProjects\pythonProject1\venv\Lib\site-packages\matplotlib\axes_base.py", line 311, in call yield from self._plot_args( ^^^^^^^^^^^^^^^^ File "C:\Users\sosar\PycharmProjects\pythonProject1\venv\Lib\site-packages\matplotlib\axes_base.py", line 504, in _plot_args raise ValueError(f"x and y must have same first dimension, but " ValueError: x and y must have same first dimension, but have shapes (365,) and (364,)
I am honestly not good at coding and debugging. This took a long time to figure out, and I am unsure if I did right! But I will post my code below!
ALL HELP IS APPREFCIATED AND THANK YOU IN ADVANCE!!!!
import csv
import datetime
import matplotlib.pyplot as plt
with open('sr_ss_clean.csv') as temp_file:
sunrise = []
sunset = []
reader = csv.reader(temp_file)
next(reader)
for row in reader:
sunrise.append(float(row[1]))
sunset.append(float(row[2]))
start_date = datetime.date(2022, 1, 1)
end_date = datetime.date(2022, 12, 31)
delta = datetime.timedelta(days=1)
date = []
while start_date <= end_date:
date.append(start_date)
start_date += delta
figure= plt.figure()
left_axis = figure.add_subplot(1, 1, 1)
right_axis = left_axis.twinx()
left_axis.plot(date, sunrise, label = "Sunrise")
left_axis.plot(date, sunset, label = "Sunset")
left_axis.set_xlabel('Date')
left_axis.legend(loc= 'upper left')
right_axis.set_ylabel('Time of Day')
plt.show()
1条答案
按热度按时间ubbxdtey1#
对于
left_axis.plot(date, sunrise, label = "Sunrise")
,您的错误如下:这意味着包含日期的列表有365行长,但包含日出的列表只有364行长。这会引发错误,因为您绘制的第365个x值找不到第365个对应的y值。
如果您用途:
这可能会起作用。这里我们将日期限制在364个值之内。您的日出列表中的某个地方缺少了一天。