0

What am I doing wrong? Can anyone help me? Or give me specific keywords for google search (I'm sure I'm not the first)? Have been dealing with this problem for over 8h now, cant find something on the internet.

Full Notebook Link (problem at the end): Kaggle Notebook

My code:

dict_data = data.copy()
dict_data.drop(["Date"], axis=1, inplace=True)
series_data = dict_data.to_dict()

bar_label = []
for key in dict_data:
    bar_label.append(key)
    

bar_color = generate_color_series(28)

fig = plt.figure(figsize=(7, 5))
axes = fig.add_subplot(1, 1, 1)
axes.set_xlim(0, 35)
axes.set_xlabel("Popularity in %")

def animate(i):
    i_value = []
    for key in dict_data:
        i_value.append(dict_data[key][i])
    i_value = tuple(i_value)
    plt.barh(bar_label, i_value, color=bar_color)
        
ani = FuncAnimation(fig, animate, interval=30)
%time ani.save('myAnimation1.gif', writer='imagemagick', fps=15)
plt.close()

Output: [Picture]

2

eshirvana
  • 23,227
  • 3
  • 22
  • 38
  • 1
    The problem is that in each animation frame you are drawing a bar plot with new data on top of the plots created for previous frames. Bars have thin white boundaries, so when a shorter bar gets plotted on top of a longer one a white line across the longer bar shows up. What you should do instead, is to create an initial plot and then at each frame modify the plot data and redisplay the modified plot. See e.g. [here](https://www.tutorialspoint.com/dynamically-updating-a-bar-plot-in-matplotlib) for an example how to do it. – bb1 Jan 15 '22 at 03:46

1 Answers1

0

The reason is that the new graph is being drawn with the previous drawing still intact, as described in the comments. So, the easiest way to deal with this is to put the action to clear the current graph in the loop process. Clearing the graph removes the x-axis limit and changes the height of the bar graph, so the x-axis limit is added again.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.animation import FuncAnimation
from IPython.display import HTML

# set global variable for color palette (plots) and grid style
PALETTE = "magma_r" # my favourite palettes: flare, CMRmap_r, magma_r
sns.set(style="darkgrid")

# function that generates n color values out of defined PALETTE
def generate_color_series(n):
    segments = cm.get_cmap(PALETTE, n)
    return segments(range(n))

data = pd.read_csv('./data/Most Popular Programming Languages from 2004 to 2022.csv', sep=',')
data["Date"] = pd.to_datetime(data["Date"])
dict_data = data.copy()
dict_data.drop(["Date"], axis=1, inplace=True)
series_data = dict_data.to_dict()

bar_label = []
for key in dict_data:
    bar_label.append(key)

bar_color = generate_color_series(28)

fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(1, 1, 1)
ax.set_xlim(0, 35)
ax.set_xlabel("Popularity in %")

def animate(i):
    i_value = []
    for key in dict_data:
        i_value.append(dict_data[key][i])
    i_value = tuple(i_value)
    ax.cla()
    ax.set_xlim(0, 35)
    ax.barh(bar_label, i_value, color=bar_color)
        
ani = FuncAnimation(fig, animate, interval=30)

from IPython.display import HTML

plt.close()
HTML(ani.to_html5_video())

enter image description here

r-beginners
  • 31,170
  • 3
  • 14
  • 32