【问题标题】:Matplotlib wont join data points when the x axis isn't equally spaced当 x 轴不等间距时,Matplotlib 不会连接数据点
【发布时间】:2015-08-13 01:25:26
【问题描述】:

我的真实数据有数千个数据点,所以我目前正在使用一个小数组来测试。

我正在尝试去除 y 轴数据中的零点,并根据 x 轴数据中的相关索引绘制非零值。我可以看到我在新数组中得到了正确的数字,但我的图表是空白的。

我的数组打印输出:

[array([ 1,  6, 54,  4,  2,  3,  8])]

[array([ 1,  3,  4,  5,  6,  9, 11])]

如果我绘制原始数据,它会起作用(工作是点都与线相连)。另外,如果我添加“。”对于绘图命令,我得到了点应该在的点,但我无法将数据点连接起来。我认为这可能是因为我的新 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()

【问题讨论】:

    标签: python matplotlib plot


    【解决方案1】:

    基本上你需要做的:

    plt.plot(x2[0],y2[0])
    

    因为

    >>> x2
    [array([ 1,  3,  4,  5,  6,  9, 11])]
    >>> y2
    [array([ 1,  6, 54,  4,  2,  3,  8])]
    

    当您看到x2y2 中的Type 时,您会看到:

    >>> 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'>
    >>>
    

    【讨论】:

    • 谢谢,因为非零返回元组,所以需要吗?
    • 检查编辑后的答案。如果可行,请接受它作为答案
    • 它有效,我没有注意到数组周围的方括号。
    猜你喜欢
    • 2020-07-11
    • 1970-01-01
    • 2019-03-02
    • 1970-01-01
    • 2021-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-28
    相关资源
    最近更新 更多