Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 17447

Whitespace artifacts with pyplot bar graph animation

$
0
0

I am trying to animate a quicksort sorting algorithm with pyplot bar graphs, but when the input list is long (500+ items), there seem to be these whitespace artifacts when rendering the animation that stay even after saving as a gif.

screenshot of animation

This is the animation function I am using

def animate(_list, generator, title, save, save_frames=1000):    n = len(_list)    fig, ax = plt.subplots()    ax.set_title(title)    bar_rects = ax.bar(range(len(_list)), _list, align="edge",antialiased=False)      ax.set_xlim(0, n)    ax.set_ylim(0, int(1.1*n))    text = ax.text(0.01, 0.95, "", transform = ax.transAxes)    iteration = [0]    def _animate(array, rects, iteration):        for rect, val in zip(rects, array):            rect.set_height(val)        iteration[0] += 1        text.set_text(f"iterations: {iteration[0]}")    anim = FuncAnimation(fig, func=_animate,        fargs=(bar_rects, iteration), frames=generator, interval=5,        repeat=False, save_count=save_frames)    if save:        writer = PillowWriter(fps=30)        anim.save(title +".gif", writer=writer)    plt.show()  def quicksortP(numberList: list, end: int = None, start: int = 0):    yield numberList    if end is None:        end = len(numberList) - 1    if start < end:        pivotPos = yield from partition(numberList, start=start, end=end)        yield from quicksortP(numberList, start=start, end=pivotPos - 1)        yield from quicksortP(numberList, start=pivotPos + 1, end=end)def partition(numberlist: list, start: int, end: int):    pivot = numberlist[end]    biggestpos = start - 1    for i in range(start, end):        if numberlist[i] <= pivot:            biggestpos += 1            numberlist[i], numberlist[biggestpos] = numberlist[biggestpos], numberlist[i]            yield numberlist    biggestpos += 1    numberlist[biggestpos], numberlist[end] = numberlist[end], numberlist[biggestpos]    yield numberlist    return biggestposif __name__=="__main__":    nums=list(np.random.randint(0,500,500))    animate(nums,quicksortP(nums),"test",save=True)

Viewing all articles
Browse latest Browse all 17447

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>