1

I am plotting with this code

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

And I get this:

enter image description here

How do I hide the black lines marked inside the red circle? Since this is an animated plot, the black lines keep piling up making the plot to lag when dragging it around.

Tasos
  • 1,575
  • 5
  • 18
  • 44
  • Possible duplicate of [Remove xticks in a matplotlib plot?](https://stackoverflow.com/questions/12998430/remove-xticks-in-a-matplotlib-plot) – SiHa Jan 27 '19 at 21:49
  • @SiHa, when I call plt.tick_params() as shown in that answer, it doesn't work, black lines still show – Tasos Jan 27 '19 at 22:16
  • Possible duplicate of [How can I remove the top and right axis in matplotlib?](https://stackoverflow.com/questions/925024/how-can-i-remove-the-top-and-right-axis-in-matplotlib) – Thomas Kühn Jan 29 '19 at 09:36

1 Answers1

3

The ticks pile up because you append strings to your array. In order to have a plot in datetime units, simply don't convert to strings.

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

If instead you want to keep equal distances between datapoints, you can just append the animating index,

xs.append(i)

In both cases, ticks won't pile up, but be selected by the respective automatic locator. You may then optionally decide to hide the ticks as well, e.g. via

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

(Note that if you only hide the ticks, but still use strings, you will likely not get around the increased lagging in the animation.)

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • I used `xs.append(i)` and also `ax.tick_params(axis="x", bottom=False)` but the little black lines still show up – Tasos Jan 27 '19 at 22:18
  • 1
    That would only be explainable if you set the ticks invisible before `clear`ing the axes. Might that be the case? Also note that if you want ticks invisible, there wouldn't be any need to rotate the ticks, so you could remove `plt.xticks`. – ImportanceOfBeingErnest Jan 27 '19 at 22:22
  • I probably made a mistake when I tested it, now it works. I set it after clearing. – Tasos Jan 27 '19 at 22:27