Reputation: 157
screen_width = 45
screen_height = 45
plt.rcParams["figure.figsize"] = (screen_width, screen_height)
plt.style.use('fivethirtyeight')
plt.tight_layout()
fig, (ax1, ax2) = plt.subplots(2,1)
ax1.set_title("Voltage vs samples")
ax1.grid()
ax1.set_ylabel("Voltage (V)")
ax1.set_xlabel("Samples")
ax2.set_title("Current vs samples")
ax2.set_ylabel("Current (A)")
ax2.set_xlabel("Samples")
ax2.grid()
# animation def
def animate(i):
plt.cla()
ax1.plot(x_vals, y_vals, 'ro')
ax2.plot(x_vals_1, y_vals_1, 'bo')
plt.tight_layout
ani = FuncAnimation(fig, animate, interval=1000)
The code works somewhat. It does animate and update the data how I want it. However, it creates two figures, the first figure is empty and the second figure is the one being updated. Why is this the case?
Upvotes: 1
Views: 184
Reputation: 10320
When plt.tight_layout()
is called prior to the creation of a figure, one is created on-the-fly. To avoid the creation of this figure you can simply call plt.tight_layout()
after creating a Figure
instance, i.e.
# ...
fig, (ax1, ax2) = plt.subplots(2, 1)
plt.tight_layout()
# ...
Also note that the line
def animate(i):
# ...
plt.tight_layout
Does nothing because the function is not called without the trailing parentheses. If you want to call tight_layout
in your animate function it should be
def animate(i):
# ...
plt.tight_layout()
Upvotes: 3