[PYTHON] A memo that made a graph animated with plotly

A memo that made a graph to animate with Plotly

Overview

Source code

import pandas as pd
import numpy as np
import plotly.express as px

def create_animation_graph(input_array: np.array, start: str = '1900-01-01 0:00', g_type: str = 'line', y_label: str = 'value',
                           x_label: str = 'time') -> object:
    """
    np.Entering an array returns a plotly animated graph
    :param input_array:Data np.array
    :param start:Start time
    :param g_type:Graph type'line','bar','area','scatter'
    :param y_label:y-axis name
    :param x_label:x-axis name
    :return:
    """
    df = pd.DataFrame()
    times = pd.date_range(start=start, periods=len(input_array), freq='T').strftime('%H:%M')

    #Create a data frame for animation
    for idx, time in enumerate(list(times)):
        add_df = pd.DataFrame({x_label: [time for time in times [0:idx]],
                               y_label: input_array [0:idx],
                               'tick': idx})
        df = pd.concat([df, add_df])

    range_x_max = len(input_array) - 2
    
    # plotly.Draw with express
    if g_type == 'scatter':
        fig = px.scatter(df, 
                         x=x_label, 
                         y=y_label,
                         animation_frame="tick",
                         range_y=[0, input_array.max()],
                         range_x=[0, range_x_max])
    elif g_type == 'line':
        fig = px.line(df, 
                      x=x_label, 
                      y=y_label, 
                      animation_frame="tick", 
                      range_y=[0, input_array.max()],
                      range_x=[0, range_x_max])
    elif g_type == 'bar':
        fig = px.bar(df, 
                     x=x_label, 
                     y=y_label, 
                     animation_frame="tick", 
                     range_y=[0, input_array.max()],
                     range_x=[0, range_x_max])
    elif g_type == 'area':
        fig = px.area(df, 
                      x=x_label, 
                      y=y_label, 
                      animation_frame="tick", 
                      range_y=[0, input_array.max()],
                      range_x=[0, range_x_max])
    else:
        raise KeyError('g_type is different')

    return fig

Run

input_array = np.array([_*np.random.rand() for _ in  np.arange(0,30)*2])
create_animation_graph(input_array,g_type='area',start='00:00',x_label='Times of Day',y_label='Electric energy')

animation.gif

Recommended Posts

A memo that made a graph animated with plotly
Make a nice graph with plotly
[Python] Make a graph that can be moved around with Plotly
I made a random number graph with Numpy
A memo that I touched the Datastore with python
Draw a graph with NetworkX
Create graph with plotly button
Draw a graph with networkx
A story that stumbled when I made a chatbot with Transformer
I made a LINE BOT that returns parrots with Go
I made a rigid Pomodoro timer that works with CUI
[Visualization] I want to draw a beautiful graph with Plotly
I made a plug-in that can "Daruma-san fell" with Minecraft
Draw a graph with Julia + PyQtGraph (2)
Draw a loose graph with matplotlib
I made a fortune with Python.
Draw a graph with Julia + PyQtGraph (1)
Draw a graph with Julia + PyQtGraph (3)
Draw a graph with pandas + XlsxWriter
Let's make a graph with python! !!
Draw a graph with PySimple GUI
I made a daemon with Python
I made a package that can compare morphological analyzers with Python
[Python] A memo that I tried to get started with asyncio
I made a shuffle that can be reset (reverted) with Python
Make a currency chart that can be moved around with Plotly (2)
Make a currency chart that can be moved around with Plotly (1)
I made a program that automatically calculates the zodiac with tkinter
A simple RSS reader made with Django
[PyQt] Display a multi-axis graph with QtChart
I made a character counter with Python
Create a web app that can be easily visualized with Plotly Dash
I made a plug-in "EZPrinter" that easily outputs map PDF with QGIS.
A memo that allows you to change Pineapple's Python environment with pyenv
A memo with Python2.7 and Python3 on CentOS
How to draw a bar graph that summarizes multiple series with matplotlib
Draw a graph that can be moved around with HoloViews and Bokeh
I made a Chrome extension that displays a graph on an AMeDAS page
I made a Hex map with Python
I made a life game with Numpy
A typed world that begins with Python
I made a tool that makes decompression a little easier with CLI (Python3)
I made a stamp generator with GAN
A memo packed with RADEX environment construction
I made a roguelike game with Python
Draw a graph with PyQtGraph Part 1-Drawing
I made a simple blackjack with Python
I made a configuration file with Python
I made a WEB application with Django
I made a neuron simulator with Python
Create a graph with borders removed with matplotlib
I made a module PyNanaco that can charge nanaco credit with python
A memo for making a figure that can be posted to a journal with matplotlib
I made a familiar function that can be used in statistics with Python
A story that I was addicted to when I made SFTP communication with python
I made a stamp substitute bot with line
Draw a flat surface with a matplotlib 3d graph
I made a competitive programming glossary with Python
I made a weather forecast bot-like with Python.
Draw a graph with Japanese labels in Jupyter
A memo that I wrote a quicksort in Python