如何处理 numpy 警告
how to tackle numpy warnings
我正在尝试生成一个带有注释的图,问题是我有多个 y 值对应于每个 x 值,所以为了保持 x 和 y 的大小相似,我写了以下内容:
x = [-2,-1,0,1,2]
y = [(22.8,19.6),(8.9,13.7,14.7),(1.9,-1.8),(-3,-5.9,-13.4),(-5.7,-6.8)]
然而,我不断收到以下警告:
VisibleDeprecationWarning:不推荐使用参差不齐的嵌套序列(它是具有不同长度或形状的列表或元组或 ndarray 的列表或元组)创建 ndarray。如果你打算这样做,你必须在创建 ndarray 时指定 'dtype=object'。
_data = np.array(data, dtype=dtype, copy=copy,
我的完整代码:
import matplotlib.pyplot as plt
import numpy as np
x = [-2,-1,0,1,2]
y = [(22.8,19.6),(8.9,13.7,14.7),(1.9,-1.8),(-3,-5.9,-13.4),(-5.7,-6.8)]
custom_annotations = ["K464E", "K472E", "K464", "M155E", "K472", "M155A", "Q539A", "M155R", "D244A", "E247A", "E247R", "D244K"]
plt.scatter(x, y, marker="o")
for i, txt in enumerate(custom_annotations):
plt.annotate(txt, (x[i], y[i]))
plt.savefig("1.png")
您想要构建所有 x-to-y 对数据。然后您可以轻松地用这些数据填充一个数组,绘制它并对其进行注释。这意味着您必须多次重复出现 x-values。
import matplotlib.pyplot as plt
import numpy as np
plt.close('all')
# We define the data as a 12 x 2 array
# data[:,0] are the x values
# data[:,1] are the y values
data = np.array([
[-2, 22.8],
[-2, 19.6],
[-1, 8.9],
[-1, 13.7],
[-1, 14.7,],
[ 0, 1.9],
[ 0, -1.8],
[ 1, -3],
[ 1, -5.9],
[ 1, -13.4],
[ 2, -5.7],
[ 2, -6.8],
])
custom_annotations = ["K464E", "K472E", "K464", "M155E", "K472", "M155A", "Q539A", "M155R", "D244A", "E247A", "E247R", "D244K"]
plt.scatter(data[:,0], data[:,1], marker="o")
for i, txt in enumerate(custom_annotations):
plt.annotate(txt, (data[i,0], data[i,1]))
据我了解,np 数组必须由 matplotlib 在您的代码中隐式声明。
要解决这个问题并移除警告,您只需将 x 和 y 声明为 numpy 数组,同时确保 y 的 dtype 是一个对象(这里的对象可以有不同的大小)。
像这样:
x = np.array([-2,-1,0,1,2])
y = np.array(
[(22.8,19.6),(8.9,13.7,14.7),(1.9,-1.8),(-3,-5.9,-13.4),(-5.7,-6.8)],
dtype = object)
In [196]: x = [-2,-1,0,1,2]
...: y = [(22.8,19.6),(8.9,13.7,14.7),(1.9,-1.8),(-3,-5.9,-13.4),(-5.7,-
...: 6.8)]
...: custom_annotations = ["K464E", "K472E", "K464", "M155E", "K472", "M
...: 155A", "Q539A", "M155R", "D244A", "E247A", "E247R", "D244K"]
matplotlib
将从输入中生成数组。
In [197]: np.array(x)
Out[197]: array([-2, -1, 0, 1, 2])
In [198]: np.array(y)
<ipython-input-198-f87824be910b>:1: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.
np.array(y)
Out[198]:
array([(22.8, 19.6), (8.9, 13.7, 14.7), (1.9, -1.8), (-3, -5.9, -13.4),
(-5.7, -6.8)], dtype=object)
In [199]: len(_)
Out[199]: 5
从 y
生成数组时出现参差不齐的警告。 y
的元组大小不同,因此无法从中创建二维数组。
In [200]: len(custom_annotations)
Out[200]: 12
看起来 y
与 annotations
的元素数量相同:
In [201]: np.hstack(y)
Out[201]:
array([ 22.8, 19.6, 8.9, 13.7, 14.7, 1.9, -1.8, -3. , -5.9,
-13.4, -5.7, -6.8])
In [202]: len(_)
Out[202]: 12
André 可能有 x
和 y
的正确配对方式。根据您当前的列表
,这是一种方法
复制 x
个值以匹配 y
模式:
In [204]: lens = [len(i) for i in y]
In [205]: lens
Out[205]: [2, 3, 2, 3, 2]
In [207]: x1 = np.array(x).repeat(lens)
In [208]: x1
Out[208]: array([-2, -2, -1, -1, -1, 0, 0, 1, 1, 1, 2, 2])
In [209]: y1 = np.hstack(y)
In [210]: x1.shape
Out[210]: (12,)
In [211]: y1.shape
Out[211]: (12,)
现在 scatter
,具有 2 (12,) 个形状数组。
In [212]: plt.scatter(x1,y1)
Out[212]: <matplotlib.collections.PathCollection at 0x7fba38be5b80>
注意 scatter
文档:
Parameters
----------
x, y : float or array-like, shape (n, )
The data positions
带有注释(尽管可能有更好的东西):
In [214]: plt.scatter(x1, y1, marker="o")
...: for i, text in enumerate(custom_annotations):
...: plt.annotate(text, (x1[i], y1[i]))
或者,因为:
In [224]: for t,i,j in zip(custom_annotations, x1, y1):
...: plt.annotate(t,(i,j))
我正在尝试生成一个带有注释的图,问题是我有多个 y 值对应于每个 x 值,所以为了保持 x 和 y 的大小相似,我写了以下内容:
x = [-2,-1,0,1,2]
y = [(22.8,19.6),(8.9,13.7,14.7),(1.9,-1.8),(-3,-5.9,-13.4),(-5.7,-6.8)]
然而,我不断收到以下警告:
VisibleDeprecationWarning:不推荐使用参差不齐的嵌套序列(它是具有不同长度或形状的列表或元组或 ndarray 的列表或元组)创建 ndarray。如果你打算这样做,你必须在创建 ndarray 时指定 'dtype=object'。 _data = np.array(data, dtype=dtype, copy=copy,
我的完整代码:
import matplotlib.pyplot as plt
import numpy as np
x = [-2,-1,0,1,2]
y = [(22.8,19.6),(8.9,13.7,14.7),(1.9,-1.8),(-3,-5.9,-13.4),(-5.7,-6.8)]
custom_annotations = ["K464E", "K472E", "K464", "M155E", "K472", "M155A", "Q539A", "M155R", "D244A", "E247A", "E247R", "D244K"]
plt.scatter(x, y, marker="o")
for i, txt in enumerate(custom_annotations):
plt.annotate(txt, (x[i], y[i]))
plt.savefig("1.png")
您想要构建所有 x-to-y 对数据。然后您可以轻松地用这些数据填充一个数组,绘制它并对其进行注释。这意味着您必须多次重复出现 x-values。
import matplotlib.pyplot as plt
import numpy as np
plt.close('all')
# We define the data as a 12 x 2 array
# data[:,0] are the x values
# data[:,1] are the y values
data = np.array([
[-2, 22.8],
[-2, 19.6],
[-1, 8.9],
[-1, 13.7],
[-1, 14.7,],
[ 0, 1.9],
[ 0, -1.8],
[ 1, -3],
[ 1, -5.9],
[ 1, -13.4],
[ 2, -5.7],
[ 2, -6.8],
])
custom_annotations = ["K464E", "K472E", "K464", "M155E", "K472", "M155A", "Q539A", "M155R", "D244A", "E247A", "E247R", "D244K"]
plt.scatter(data[:,0], data[:,1], marker="o")
for i, txt in enumerate(custom_annotations):
plt.annotate(txt, (data[i,0], data[i,1]))
据我了解,np 数组必须由 matplotlib 在您的代码中隐式声明。
要解决这个问题并移除警告,您只需将 x 和 y 声明为 numpy 数组,同时确保 y 的 dtype 是一个对象(这里的对象可以有不同的大小)。
像这样:
x = np.array([-2,-1,0,1,2])
y = np.array(
[(22.8,19.6),(8.9,13.7,14.7),(1.9,-1.8),(-3,-5.9,-13.4),(-5.7,-6.8)],
dtype = object)
In [196]: x = [-2,-1,0,1,2]
...: y = [(22.8,19.6),(8.9,13.7,14.7),(1.9,-1.8),(-3,-5.9,-13.4),(-5.7,-
...: 6.8)]
...: custom_annotations = ["K464E", "K472E", "K464", "M155E", "K472", "M
...: 155A", "Q539A", "M155R", "D244A", "E247A", "E247R", "D244K"]
matplotlib
将从输入中生成数组。
In [197]: np.array(x)
Out[197]: array([-2, -1, 0, 1, 2])
In [198]: np.array(y)
<ipython-input-198-f87824be910b>:1: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.
np.array(y)
Out[198]:
array([(22.8, 19.6), (8.9, 13.7, 14.7), (1.9, -1.8), (-3, -5.9, -13.4),
(-5.7, -6.8)], dtype=object)
In [199]: len(_)
Out[199]: 5
从 y
生成数组时出现参差不齐的警告。 y
的元组大小不同,因此无法从中创建二维数组。
In [200]: len(custom_annotations)
Out[200]: 12
看起来 y
与 annotations
的元素数量相同:
In [201]: np.hstack(y)
Out[201]:
array([ 22.8, 19.6, 8.9, 13.7, 14.7, 1.9, -1.8, -3. , -5.9,
-13.4, -5.7, -6.8])
In [202]: len(_)
Out[202]: 12
André 可能有 x
和 y
的正确配对方式。根据您当前的列表
复制 x
个值以匹配 y
模式:
In [204]: lens = [len(i) for i in y]
In [205]: lens
Out[205]: [2, 3, 2, 3, 2]
In [207]: x1 = np.array(x).repeat(lens)
In [208]: x1
Out[208]: array([-2, -2, -1, -1, -1, 0, 0, 1, 1, 1, 2, 2])
In [209]: y1 = np.hstack(y)
In [210]: x1.shape
Out[210]: (12,)
In [211]: y1.shape
Out[211]: (12,)
现在 scatter
,具有 2 (12,) 个形状数组。
In [212]: plt.scatter(x1,y1)
Out[212]: <matplotlib.collections.PathCollection at 0x7fba38be5b80>
注意 scatter
文档:
Parameters
----------
x, y : float or array-like, shape (n, )
The data positions
带有注释(尽管可能有更好的东西):
In [214]: plt.scatter(x1, y1, marker="o")
...: for i, text in enumerate(custom_annotations):
...: plt.annotate(text, (x1[i], y1[i]))
或者,因为:
In [224]: for t,i,j in zip(custom_annotations, x1, y1):
...: plt.annotate(t,(i,j))