我想出了如何使用与stackoverflow上显示的技术略有不同的技术将我的日期时间列表转换为字符串,但现在我想绘制我将字符串转换为日期时间时附加到的数组datetimes_final final_times数组,但我一直得到错误ValueError:x和y必须具有相同的第一个维度,当我的final_times和temp数组确实具有相同数量的元素以便绘制时,所以我不明白为什么我收到此错误,并需要帮助修复它。我也发布了我得到的错误,但我没有发布整个错误,因为它已超过200行所以我刚刚发布了重要部分。
def values_equal_np_array3(temp, time_created_index, time_modified_index, times_from_text_file):
if (len(np.unique(temp)) == 1):
return True
else:
#tells user the temps are not equal for a specified interval of time
print ('False')
#specifying the beginning to end interval of temperature collection
final_times = times_from_text_file[time_created_index: time_modified_index]
#create a new empty list to hold datetimes
datetimes_final = []
#iterate over the empyty list
for element in final_times:
datetime_conversion = datetime.datetime.strptime(element, "%H:%M")
datetimes_final.append(datetime_conversion)
plt.xlabel('Time')
plt.ylabel('Temperature')
plt.title('Time vs Temperature')
#http://stackoverflow.com/questions/1574088/plotting-time-in-python-with-matplotlib
#I am trying to plot two arrays-one in datetimes and another in ints- using the link above
final_times = matplotlib.dates.date2num(datetimes_final)
matplotlib.pyplot.plot_date(final_times, temp)
#the values that appear in the non-uniform temperature array on display as an array
return np.unique(temp)
#test case for the values_equal_np_array3 function
#the first 6 times from the times_from_text_file array should be plotted against the temp array
values_equal_np_array3(np.array([1, 1, 1, 2, 3, 4]), 0, 5, np.array(['10:15','12:31','12:32','1:33', '12:34', '1:35', '2:36', '2:37', '3:38', '1:39']))
False
ValueError Traceback (most recent call last)
<ipython-input-79-c16b47f0901d> in <module>()
----> 1 values_equal_np_array3(np.array([1, 1, 1, 2, 3, 4]), 0, 5, np.array(['10:15','12:31','12:32','1:33', '12:34', '1:35', '2:36', '2:37', '3:38', '1:39']))
<ipython-input-78-67671c2f9e11> in values_equal_np_array3(temp, time_created_index, time_modified_index, times_from_text_file)
23
24 final_times = matplotlib.dates.date2num(datetimes_final)
---> 25 matplotlib.pyplot.plot_date(final_times, temp)
26
27
C:\Users\mintw\Anaconda3\lib\site-packages\matplotlib\axes\_base.py in _xy_from_xy(self, x, y)
217 y = _check_1d(y)
218 if x.shape[0] != y.shape[0]:
--> 219 raise ValueError("x and y must have same first dimension")
220 if x.ndim > 2 or y.ndim > 2:
221 raise ValueError("x and y can be no greater than 2-D")
ValueError: x and y must have same first dimension