完全隐藏x轴

时间:2019-01-27 21:39:48

标签: python matplotlib

我正在使用此代码进行绘图

def animate(i, xs, ys):
    global a_depth
    global b_depth

    print(a_depth,b_depth)

    if a_depth!=0 and b_depth!=0:

        # Add x and y to lists
        xs.append(dt.datetime.now().strftime('%H:%M:%S.%f'))
        ys.append([a_depth, b_depth])

        # Limit x and y lists to 20 items
        # xs = xs[-20:]
        # ys = ys[-20:]

        # Draw x and y lists
        ax.clear()
        ax.plot(xs, ys)
        ax.xaxis.set_major_formatter(plt.NullFormatter())       
        # Format plot
        plt.xticks(rotation=45, ha='right')
        plt.subplots_adjust(bottom=0.30)
        plt.title('title')
        plt.ylabel('ylabel')

# Set up plot to call animate() function periodically
ani = animation.FuncAnimation(fig, animate, fargs=(xs, ys), interval=1000)
plt.show()

我明白了:

enter image description here

如何隐藏红色圆圈内标记的黑线?由于这是一个动画情节,因此黑线不断堆积,使该情节在拖曳时滞后。

1 个答案:

答案 0 :(得分:3)

由于您将字符串追加到数组中,所以滴答声堆积了起来。为了以日期时间为单位绘制图,只需不要将其转换为字符串。

xs.append(dt.datetime.now())

如果您想保持数据点之间的距离相等,则只需附加动画索引

xs.append(i)

在两种情况下,刻度不会堆积,而是由相应的自动定位器选择。然后,您可以选择是否也隐藏刻度线,例如通过

ax.tick_params(axis="x", bottom=False)

(请注意,如果您仅隐藏刻度线,但仍使用字符串,则可能无法避免动画中滞后现象的增加。)