I tried using matplotlib

I had the opportunity to use matplotlib at work, so I will summarize the usage I learned.

environment

Official page of matplotlib

Basic usage

Module import

import matplotlib.pyplot as plt

Graph display

plt.plot([1, 2, 3, 4, 5])
plt.show()

The following graph is displayed. If there is only one piece of information to pass to the argument, it seems that the coordinates of the argument will be on the y-axis. plot.png

When drawing multiple lines

line1 = [1, 2, 3, 4, 5]
line2 = [5, 4, 3, 2, 1]
plt.plot(line1, color='red')
plt.plot(line2, color='green')
plt.show()

You can draw one line with plt.plot (). Specify color to change the line color

plot.png

animation

You can also animate to draw the line.

Import of modules required for animation

from matplotlib.animation import ArtistAnimation
#Or
from matplotlib.animation import FuncAnimation

There are two classes for implementing animation.

ArtistAnimation

Animation of a graph in which the value increases by 1

fig = plt.figure()
artist = []
line = []

for value in range(1, 10):
    line.append(value)
    im = plt.plot(line, color='red', marker='o')
    artist.append(im)

anim = ArtistAnimation(fig, artist, interval=300)    
plt.show()

FuncAnimation

Implementation content is the same as Artist Animation Note that FuncAnimation defaults to repeat when creating an object, and if the initialization function is not implemented, it will be an unintended animation after the second week.

fig = plt.figure()
line = []

def init():
    global line
    print("Implement initialization process")
    plt.gca().cla()
    line = []

def draw(i):
    line.append(i)
    im = plt.plot(line, color='red', marker='o')

anim = FuncAnimation(fig, func=draw, frames=10, init_func=init, interval=100)
plt.show()

Recommended Posts

I tried using matplotlib
I tried using argparse
I tried using anytree
I tried using aiomysql
I tried using Summpy
I tried using coturn
I tried using "Anvil".
I tried using Hubot
I tried using ESPCN
I tried using openpyxl
I tried using Ipython
I tried using PyCaret
I tried using cron
I tried using ngrok
I tried using face_recognition
I tried using Jupyter
I tried using PyCaret
I tried using Heapq
I tried using doctest
I tried using folium
I tried using jinja2
I tried using folium
I tried using time-window
[I tried using Pythonista 3] Introduction
I tried using easydict (memo).
I tried face recognition using Face ++
I tried using Random Forest
I tried using BigQuery ML
I tried using Amazon Glacier
I tried using git inspector
[Python] I tried using OpenPose
I tried using magenta / TensorFlow
I tried using AWS Chalice
I tried using Slack emojinator
I tried using Rotrics Dex Arm # 2
I tried using Rotrics Dex Arm
I tried using GrabCut of OpenCV
I tried using Thonny (Python / IDE)
I tried server-client communication using tmux
I tried reinforcement learning using PyBrain
I tried deep learning using Theano
Somehow I tried using jupyter notebook
[Kaggle] I tried undersampling using imbalanced-learn
I tried shooting Kamehameha using OpenPose
I tried using the checkio API
[Python] I tried using YOLO v3
I tried asynchronous processing using asyncio
I tried PyQ
I tried AutoKeras
I tried papermill
Try using matplotlib
I tried django-slack
I tried Django
I tried spleeter
I tried cgo
I tried using Amazon SQS with django-celery
I tried using Azure Speech to Text.
I tried using Twitter api and Line api
I tried playing a ○ ✕ game using TensorFlow
I tried using YOUTUBE Data API V3
I tried using Selenium with Headless chrome