[Python] Create animated gif with imageio library python

Discussão em 'Python' iniciado por Stack, Março 9, 2025.

  1. Stack

    Stack Membro Participativo

    Trying to create an animated GIF with static images obtained from the seaborn library. The code is as follows:

    import pandas as pd
    import matplotlib.pyplot as plt
    import seaborn as sns
    import os
    import imageio.v3 as iio

    image = []
    folder = 'C:/Users/admin/PycharmProjects/pythonProject2/Project/Seaborn'



    if not os.path.exists(folder):
    os.mkdir(folder)

    for month in range(1, 4):
    porosity_cutoff = 250
    highlight_colour = 8
    non_highlight_colour = 10

    df = pd.read_csv('../Список.csv')
    df = df.loc[df['Месяц'] == month].sort_values(by='Баллы', ascending=False)

    df['colours'] = df["Баллы"].apply(lambda x: highlight_colour if x >= porosity_cutoff else non_highlight_colour)

    ax = sns.barplot(data=df.loc[df['Месяц'] == month], x="Баллы", y="Участники", palette="Set1", hue=df['colours'],
    legend=False)

    plt.axvline(x=250, zorder=0, color='grey', ls='--', lw=1.5)

    plt.text(x=283, y=8, s='Порог допустимых баллов', ha='center', fontsize=11, bbox=dict(facecolor='white',
    edgecolor="grey", ls='--'))

    ax.set_title(f"Рейтинг за {month} месяц", size=30)
    ax.set_ylabel("Участники", size=20)
    ax.set_xlabel("Баллы", size=20)

    for i in ax.containers:
    ax.bar_label(i, color="white", padding=-30, fontsize=12)

    filename = f'Рейтинг_{month}.png'

    plt.savefig(filename)

    image.append(iio.imread(filename))

    plt.close()

    iio.imwrite('barplot.gif', image, duration=20)


    In the end I get three images in png format with graphics, all the images are correct, but the giftcard itself is a static first graph. What can be changed or does not work in general?

    Guess it’s about the list image and filling it out wrong

    Continue reading...

Compartilhe esta Página