【问题标题】:Hide x axis completely完全隐藏 x 轴
【发布时间】:2019-01-27 21:39:48
【问题描述】:

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

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()

我明白了:

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

【问题讨论】:

标签: python matplotlib


【解决方案1】:

刻度堆积起来是因为您将字符串附加到数组中。为了以日期时间为单位绘制图表,请不要转换为字符串。

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

如果你想保持数据点之间的距离相等,你可以附加动画索引,

xs.append(i)

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

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

(请注意,如果您只隐藏刻度,但仍使用字符串,您可能无法解决动画中增加的滞后问题。)

【讨论】:

  • 我使用了xs.append(i)ax.tick_params(axis="x", bottom=False),但小黑线仍然出现
  • 只有在clear轴之前设置不可见的刻度才可以解释。可能是这样吗?另请注意,如果您想让刻度不可见,则无需旋转刻度,因此您可以删除 plt.xticks
  • 我可能在测试时犯了一个错误,现在它可以工作了。我是在清除后设置的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多