from matplotlib import pyplot as plt
import matplotlib.animation as animation
import matplotlib.dates as mdates
import csv
import datetime
import locale

locale.setlocale(locale.LC_TIME, 'ru_RU.UTF-8')

csv_file_name = 'roi-63007.csv'
date_format = '%Y-%m-%d'
time_format = '%Y-%m-%d %H:%M'

x = []
yes = []
no = []
row_index = 0
with open(csv_file_name, 'r') as csvfile:
    plots = csv.reader(csvfile, delimiter=',')
    for row in plots:
        row_index = row_index + 1
        if (row_index % 48) == 0:
            datetime_obj = datetime.datetime.strptime(str(row[0]), time_format)
            x.append(datetime_obj)
            yes.append(int(row[1]))
            no.append(int(row[2]))

fig = plt.figure()
ax1 = fig.add_subplot()


def animate(i):
    if i >= len(x):
        i = len(x) - 1
    ax1.clear()
    ax1.set_title('Ход голосования по инициативе 63007')
    ax1.plot(x[:i], yes[:i], 'g', label='За')
    ax1.plot(x[:i], no[:i], 'r', label='Против')
    ax1.legend(loc='upper left')
    ax1.grid(True)
    locator = mdates.AutoDateLocator()
    ax1.xaxis.set_major_locator(locator)
    fmt = mdates.ConciseDateFormatter(locator)
    ax1.xaxis.set_major_formatter(fmt)


additional_frames = 60  # show last frame few more seconds
ani = animation.FuncAnimation(fig, animate, interval=100, save_count=len(x) + additional_frames)

#plt.show()

#writergif = animation.PillowWriter(fps=15)
#ani.save('roi-63007.gif', writer=writergif)

Writer = animation.writers['ffmpeg']
writer = Writer(fps=15, metadata=dict(artist='Me'), bitrate=1800)
ani.save('roi-63007.mp4', writer=writer)


from matplotlib import pyplot as plt
import matplotlib.animation as animation
import matplotlib.dates as mdates
import csv
import datetime
import locale

locale.setlocale(locale.LC_TIME, 'ru_RU.UTF-8')

csv_file_name = 'roi-63007.csv'
date_format = '%Y-%m-%d'
time_format = '%Y-%m-%d %H:%M'

x = []
yes = []
no = []
row_index = 0
with open(csv_file_name, 'r') as csvfile:
    plots = csv.reader(csvfile, delimiter=',')
    for row in plots:
        row_index = row_index + 1
        if (row_index % 48) == 0:
            datetime_obj = datetime.datetime.strptime(str(row[0]), time_format)
            x.append(datetime_obj)
            yes.append(int(row[1]))
            no.append(int(row[2]))

fig = plt.figure()
ax1 = fig.add_subplot()


def animate(i):
    if i >= len(x):
        i = len(x) - 1
    ax1.clear()
    ax1.set_title('Ход голосования по инициативе 63007')
    ax1.plot(x[:i], yes[:i], 'g', label='За')
    ax1.plot(x[:i], no[:i], 'r', label='Против')
    ax1.legend(loc='upper left')
    ax1.grid(True)
    locator = mdates.AutoDateLocator()
    ax1.xaxis.set_major_locator(locator)
    fmt = mdates.ConciseDateFormatter(locator)
    ax1.xaxis.set_major_formatter(fmt)


additional_frames = 60  # show last frame few more seconds
ani = animation.FuncAnimation(fig, animate, interval=100, save_count=len(x) + additional_frames)

#plt.show()

#writergif = animation.PillowWriter(fps=15)
#ani.save('roi-63007.gif', writer=writergif)

Writer = animation.writers['ffmpeg']
writer = Writer(fps=15, metadata=dict(artist='Me'), bitrate=1800)
ani.save('roi-63007.mp4', writer=writer)


# show last frame few more seconds #plt.show() #writergif = animation.PillowWriter(fps=15) #ani.save('roi-63007.gif', writer=writergif)