使用这段代码,我得到了温度和日期时间,然后使用numpy(np)将它们插入到matplotlib(plt)
# 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$
答案 0 :(得分:1)
也许这可以帮助
#first import
from datetime import datetime
a = datetime.now()
#converting into int
a = int(a.strftime('%Y-%m-%d %H:%M:%S')) #using strtime to convert datetime into int
答案 1 :(得分:1)
是的,您可以使用字符串制作图形,但matplot版本应为> 2.1或2.2
import matplotlib.pyplot as plt
x = ["ABCD", "EEEEE", "LLLL"]
y = [5,2,3]
plt.plot(x, y)
plt.show()
答案 2 :(得分:1)
我认为这可以复制部分情节:
In [347]: date_times = ['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 = [29, 31, 30, 25, 23, 22, 20, 23, 30, 33, 31, 27, 24, 23, 21, 23, 31]
In [348]: len(date_times)
Out[348]: 4
In [349]: len(temperatures)
Out[349]: 17
In [350]: x = np.arange(len(date_times))
In [351]: y = np.array(temperatures[:4])
In [359]: plt.xticks(x, date_times);
In [360]: plt.plot(x,y);
我的arange
是构建x
的一种比您更快捷的方法:
In [361]: n = []
...: for i in range(0,4):
...: n.append(i)
...: np.array(n)
请注意,我使用的是date_times
,而不是[date_times]
;稍后会添加一个额外的列表层。我无法重现您的错误,但是不必要的[]可能会引起问题。 ticks
的{{1}}和labels
参数应具有相同的长度。
该错误看起来像是在创建轴(xticks)时发生的。它使用数组(xticks
?)作为字典键。该错误在x
代码的深处发生,因此很难将其追溯到您的输入。因此,仅检查输入(plt
,x
,y
)并确保它们看起来合理(期望的数据和匹配的长度)就容易得多。
这里有同样的错误:
TypeError: unhashable type: 'numpy.ndarray' when trying to plot a DataFrame
尽管没有,但我看不出有什么相似或不同之处。
===
这可以确定:
date_times
但这会产生错误:
In [364]: plt.plot(date_times,y);
(与您的In [365]: plt.plot([date_times],y);
一样,它有不必要的括号)。