1
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import matplotlib.animation as animation

data = pd.read_csv('simulation_data_7.csv', index_col = 0, header=0)

frames, bins = data.shape
HIST_BINS = np.arange(bins - 3)

fig, axs = plt.subplots(ncols=2, nrows=2)
gs = axs[0,0].get_gridspec()

for ax in axs[0,0:]:
    ax.remove()
trades = axs[1,0]
entropy = axs[1,1]
pareto = fig.add_subplot(gs[0, 0:])

pareto.set_ylim(top=100)
pareto.margins(x=0)

p_annotation = pareto.annotate("Iterations: 0", (0.75, 0.75), xycoords='axes fraction', va='center')
t_annotation = trades.annotate("Trades: 0", (0.75, 0.75), xycoords='axes fraction', va='center')
e_annotation = entropy.annotate("Entropy: 0", (0.75, 0.75), xycoords='axes fraction', va='center')

fig.tight_layout()

def update(bar_container):
    
    def animate(frame_number):

        p_annotation.set_text("Iterations: {}".format(frame_number))

        n = np.array(data.iloc[frame_number, HIST_BINS])
        for count, rect in zip(n, bar_container.patches):
            rect.set_height(count)
        return bar_container.patches

    return animate

_, _, bar_container = pareto.hist(data.iloc[0,0:bins-2], HIST_BINS, lw=1, ec="#000000", fc="#FF00FF", alpha=0.5)


ani = animation.FuncAnimation(fig, update(bar_container), frames, repeat=False, blit=True, cache_frame_data = False, interval = 0.04)
plt.show()

I don't think bar_container.patches is compatible with updating text.

I have two questions that are bothering me:

  1. I can't update annotations in my plot when blit=True, but it's too slow and clunky when blit=False. How to update annotations while blit=True
  2. I grabbed the code from an example, but for the life of me I don't know how frame_number is updated. I read that funcanimation uses the next argument in frames, but how the embedded definition track the frame_number value?

0 Answers0