Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 类型错误:不可损坏的类型:';numpy.ndarray和#x27;尝试使用numpy进行绘图时_Python_Numpy_Matplotlib - Fatal编程技术网

Python 类型错误:不可损坏的类型:';numpy.ndarray和#x27;尝试使用numpy进行绘图时

Python 类型错误:不可损坏的类型:';numpy.ndarray和#x27;尝试使用numpy进行绘图时,python,numpy,matplotlib,Python,Numpy,Matplotlib,使用这段代码,我得到了温度和日期时间,然后使用numpy(np)将它们插入matplotlib(plt)中 然而,我不断地得到这个错误: for val in OrderedDict.fromkeys(data): TypeError: unhashable type: 'numpy.ndarray' 我做了一些研究,发现这意味着我认为形状出了问题。 是因为它们是弦吗?如果是这样的话,你能建议一种将我的日期时间转换成整数的方法吗 谢谢大家! 完全回溯: Traceback (most

使用这段代码,我得到了温度和日期时间,然后使用numpy(np)将它们插入matplotlib(plt)中

然而,我不断地得到这个错误:

    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$ 
回溯(最近一次呼叫最后一次):
文件“class test.py”,第77行,在
天气数据('gurgaon')
weatherData中第55行的文件“class test.py”
平面图(x,y)
文件“/Users/Ronnie/.local/share/virtualenvs/weather-d3bb5uZO/lib/python3.8/site packages/matplotlib/pyplot.py”,绘图中第2761行
返回gca().plot(
文件“/Users/Ronnie/.local/share/virtualenvs/weather-d3bb5uZO/lib/python3.8/site packages/matplotlib/axes/_axes.py”,绘图中第1646行
行=[*self.\u获取行(*args,data=data,**kwargs)]
文件“/Users/Ronnie/.local/share/virtualenvs/weather-d3bb5uZO/lib/python3.8/site packages/matplotlib/axes/_base.py”,第216行,在调用中__
从自身获得的收益。_plot_args(本,kwargs)
文件“/Users/Ronnie/.local/share/virtualenvs/weather-d3bb5uZO/lib/python3.8/site packages/matplotlib/axes/_base.py”,第339行,在绘图参数中
自.轴.y轴.更新单位(y)
文件“/Users/Ronnie/.local/share/virtualenvs/weather-d3bb5uZO/lib/python3.8/site packages/matplotlib/axis.py”,第1516行,以更新单位表示
默认值=self.converter.default\u单位(数据,self)
文件“/Users/Ronnie/.local/share/virtualenvs/weather-d3bb5uZO/lib/python3.8/site packages/matplotlib/category.py”,第107行,默认单位
轴。设置单位(单位数据(数据))
文件“/Users/Ronnie/.local/share/virtualenvs/weather-d3bb5uZO/lib/python3.8/site packages/matplotlib/category.py”,第175行,在__
自我更新(数据)
文件“/Users/Ronnie/.local/share/virtualenvs/weather-d3bb5uZO/lib/python3.8/site packages/matplotlib/category.py”,第210行,更新中
对于OrderedDict.fromkeys中的val(数据):
TypeError:不可损坏的类型:“numpy.ndarray”
(天气)bash-3.2美元
也许这会有所帮助

#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

是的,您可以使用字符串生成图形,但matplot版本应>2.1或2.2

import matplotlib.pyplot as plt
x = ["ABCD", "EEEEE", "LLLL"]
y = [5,2,3]

plt.plot(x, y)

plt.show()

我认为这复制了你的部分情节:

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);  
My
arange
是一种比您的:

In [361]: n = [] 
     ...: for i in range(0,4): 
     ...:     n.append(i) 
     ...: np.array(n) 
请注意,我使用的是
date\u times
,而不是
[date\u times]
;后者添加了一个额外的列表层。我无法重现您的错误,但不必要的[]可能会导致问题。
标记
标签
参数的长度应该相同

错误看起来像是在创建轴(XTICK)时发生的。它使用数组(
x
?)作为字典键。错误发生在
plt
代码的深处,因此很难将其追溯到输入。因此,只需检查输入(
x
y
date\u times
),并确保它们看起来合理(预期数据和匹配长度)

此处出现相同错误:

虽然我不知道有什么相似或不同

===

这将绘制ok:

In [364]: plt.plot(date_times,y);  
但这会产生错误:

In [365]: plt.plot([date_times],y);  

(与您的
xticks
一样,这有不必要的括号)。

您可以添加完整的回溯吗?告诉我们
x
y
-形状和数据类型(假设它们是
ndarray
)。如果跳过
plt.xticks
行,或者将括号放到
[date\u times]
(为什么要使用[]?)。所以根据你的说法,我必须这样做:
date\u times=int(date\u times.strftime(“%Y-%m-%d%H:%m:%S”))print(date\u times)
?“a”从哪里得到它的值?@beepbeep boop你应该使用
date\u times=datetime.now()
而不是
today=datetime.todate().date()
。好的,谢谢,但是等等,@Cb9867,我们可以使用字符串在x轴上使用matplot,在y轴上使用int来绘制图形吗?@beep boop或者如果你不想在程序中使用秒和分钟,只想使用今天的日期,那么你应该使用你正在使用的
today=datetime.today().date()
但也需要将这一行作为
%Y-%m-%d'
非常感谢!顺便说一下,对于for循环,我使用了这个:
对于范围内的i(0,日期时间长度):
可以吗?你不需要循环。使用
np.arange(日期时间长度)
来获得
[0,1,2…]的数组,甚至
np.array(范围(日期时间长度))
In [365]: plt.plot([date_times],y);