我的真实数据有成千上万个数据点,因此我目前正在使用一个小型阵列进行测试。
我试图摆脱y轴数据中的零,并针对x轴数据中的相关索引绘制非零值。我可以看到我在新数组中得到了正确的数字,但是我的图形是空白的。
我的阵列打印输出:
[array([ 1, 6, 54, 4, 2, 3, 8])]
[array([ 1, 3, 4, 5, 6, 9, 11])]
如果我绘制原始数据,它会起作用(工作点都是用线连接起来的)。另外,如果我添加“。”到plot命令,我得到了点的本意,但是我无法将数据点合并。我认为这可能是因为我的新x轴数据不是连续的,但是我不确定。
y1 = np.array([1,0,6,54,4,2,0,0,3,0,8])
x1 = np.array([1,2,3,4,5,6,7,8,9,10,11])
yind = y1.nonzero()
y2 = []
x2 = []
for el in yind:
y2.append(y1[el])
x2.append(x1[el])
print y2
print x2
pl.plot(x2,y2)
pl.show()
最佳答案
基本上,您需要执行以下操作:
plt.plot(x2[0],y2[0])
因为
>>> x2
[array([ 1, 3, 4, 5, 6, 9, 11])]
>>> y2
[array([ 1, 6, 54, 4, 2, 3, 8])]
当您看到
Type
和x2
的y2
时,您会看到:>>> type(y2)
<class 'list'>
但是
x2[0]
和y2[0]
是要绘制的数组:>>> x2[0]
array([ 1, 3, 4, 5, 6, 9, 11])
>>> y2[0]
array([ 1, 6, 54, 4, 2, 3, 8])
>>> type(y2[0])
<class 'numpy.ndarray'>
>>>
关于python - 当x轴间距不相等时,Matplotlib不会连接数据点,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31971243/