Skip to content

Instantly share code, notes, and snippets.

@iwatake2222
Created October 18, 2017 12:59
Show Gist options
  • Save iwatake2222/43b1d8bc1dcfd2f0bf19a81486985d59 to your computer and use it in GitHub Desktop.
Save iwatake2222/43b1d8bc1dcfd2f0bf19a81486985d59 to your computer and use it in GitHub Desktop.
Several ways for matplotlib heatmap animation
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
main.py
"""
__author__ = "take-iwiw"
__copyright__ = "Copyright 2017, take-iwiw"
__date__ = "18 Oct 2017"
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import pandas as pd
import time
import matplotlib.animation as animation
NUMBER_X: int = 10
NUMBER_Y: int = 10
CANVAS_WIDTH: int = 10
CANVAS_HEIGHT: int = 10
def heatmap_animation1():
fig, ax_lst = plt.subplots(NUMBER_X, NUMBER_Y)
ax_lst = ax_lst.ravel()
def plot(data):
data = np.random.rand(CANVAS_WIDTH, CANVAS_HEIGHT)
heatmap = ax_lst[0].pcolor(data)
ani = animation.FuncAnimation(fig, plot, interval=1)
plt.show()
def heatmap_animation2():
fig, ax_lst = plt.subplots(NUMBER_X, NUMBER_Y)
ax_lst = ax_lst.ravel()
data = np.random.rand(CANVAS_WIDTH, CANVAS_HEIGHT)
im = ax_lst[0].imshow(data)
while True:
t_start = time.time()
data = np.random.rand(CANVAS_WIDTH, CANVAS_HEIGHT)
im.set_data(data)
plt.pause(0.001)
t_end = time.time()
print("fps = {0}".format(999 if t_end == t_start else 1/(t_end-t_start)))
def heatmap_animation3():
fig, ax_lst = plt.subplots(NUMBER_X, NUMBER_Y)
ax_lst = ax_lst.ravel()
data = np.random.rand(CANVAS_WIDTH, CANVAS_HEIGHT)
heatmap = ax_lst[0].pcolor(data)
fig.canvas.draw()
fig.show()
while True:
data = np.random.rand(CANVAS_WIDTH, CANVAS_HEIGHT)
t_start = time.time()
heatmap = ax_lst[0].pcolor(data)
ax_lst[0].draw_artist(ax_lst[0].patch)
ax_lst[0].draw_artist(heatmap)
fig.canvas.blit(ax_lst[0].bbox)
fig.canvas.flush_events()
t_end = time.time()
print("fps = {0}".format(999 if t_end == t_start else 1/(t_end-t_start)))
def main():
"""
Entry function
:called when: the program starts
:param none: no parameter
:return: none
:rtype: none
"""
heatmap_animation3()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment