Используя этот фрагмент кода, я получаю температуры и дату, а затем вставляю их в matplotlib (plt), используя numpy (np)
# get date times and the temperatures of a certain city, info pulled from request
raw_date_times = [item['dt_txt'] for item in s['list']]
temperature_kelvins = [item['main']['temp'] for item in s['list']]
# Apply calculation on each item to make celsius from kelvins
temperatures = [round(item - 273.15) for item in temperature_kelvins]
# Filter out today's date from list of dates into date_times
today = datetime.today().date()
date_times = []
for i in raw_date_times:
date = datetime.strptime(i, '%Y-%m-%d %H:%M:%S').date()
if date == today:
date_times.append(i)
# Convert the array with integers of temperatures to strings to make both of same dimension
for i in range(0, len(temperatures)):
temperatures[i] = str(temperatures[i])
# get len of date_times and convert it into an array (i.e 6 becomes [0,1,2,3,4,5])
date_times_len = len(date_times)
n = []
for i in range(0,date_times_len):
n.append(i)
print (n)
# Plot out map using values
x = np.array(n)
y = np.array([temperatures])
my_xticks = [date_times]
plt.xticks(x, my_xticks)
plt.plot(x, y)
plt.show()
# date_times example = ['2020-03-17 12:00:00', '2020-03-17 15:00:00', '2020-03-17 18:00:00', '2020-03-17 21:00:00']
# temperatures example (before string)= [29, 31, 30, 25, 23, 22, 20, 23, 30, 33, 31, 27, 24, 23, 21, 23, 31]
Однако я продолжаю получать эту ошибку:
for val in OrderedDict.fromkeys(data):
TypeError: unhashable type: 'numpy.ndarray'
Я немного исследовал и обнаружил, что это означает, что что-то пошло не так с формой, я думаю. Это потому что они струны? Если да, то не могли бы вы предложить способ преобразования моих дат в целые числа?
Спасибо!
Полный возврат:
Traceback (most recent call last):
File "class-test.py", line 77, in <module>
weatherData('gurgaon')
File "class-test.py", line 55, in weatherData
plt.plot(x, y)
File "/Users/Ronnie/.local/share/virtualenvs/weather-d3bb5uZO/lib/python3.8/site-packages/matplotlib/pyplot.py", line 2761, in plot
return gca().plot(
File "/Users/Ronnie/.local/share/virtualenvs/weather-d3bb5uZO/lib/python3.8/site-packages/matplotlib/axes/_axes.py", line 1646, in plot
lines = [*self._get_lines(*args, data=data, **kwargs)]
File "/Users/Ronnie/.local/share/virtualenvs/weather-d3bb5uZO/lib/python3.8/site-packages/matplotlib/axes/_base.py", line 216, in __call__
yield from self._plot_args(this, kwargs)
File "/Users/Ronnie/.local/share/virtualenvs/weather-d3bb5uZO/lib/python3.8/site-packages/matplotlib/axes/_base.py", line 339, in _plot_args
self.axes.yaxis.update_units(y)
File "/Users/Ronnie/.local/share/virtualenvs/weather-d3bb5uZO/lib/python3.8/site-packages/matplotlib/axis.py", line 1516, in update_units
default = self.converter.default_units(data, self)
File "/Users/Ronnie/.local/share/virtualenvs/weather-d3bb5uZO/lib/python3.8/site-packages/matplotlib/category.py", line 107, in default_units
axis.set_units(UnitData(data))
File "/Users/Ronnie/.local/share/virtualenvs/weather-d3bb5uZO/lib/python3.8/site-packages/matplotlib/category.py", line 175, in __init__
self.update(data)
File "/Users/Ronnie/.local/share/virtualenvs/weather-d3bb5uZO/lib/python3.8/site-packages/matplotlib/category.py", line 210, in update
for val in OrderedDict.fromkeys(data):
TypeError: unhashable type: 'numpy.ndarray'
(weather) bash-3.2$