[PYTHON] Run Matplotlib on a Docker container

Premise

Method

Source

Dockerfile


FROM python:3.6.2

RUN apt-get update \
 && apt-get install -y \
      git \
      unzip \
 && rm -rf /var/lib/apt/lists/*

#Install Ricty Diminished as a font.
WORKDIR /usr/share/fonts
ENV RICTY_DIMINISHED_VERSION 3.2.4
ADD https://github.com/mzyy94/RictyDiminished-for-Powerline/archive/$RICTY_DIMINISHED_VERSION-powerline-early-2016.zip .
RUN unzip -jo $RICTY_DIMINISHED_VERSION-powerline-early-2016.zip \
 && fc-cache -fv

#Prepare a configuration file for Matplotlib.
WORKDIR /etc
RUN echo "backend : Agg" >> matplotlibrc \
 && echo "font.family : Ricty Diminished" >> matplotlibrc

#Install Matplotlib.
WORKDIR /opt/app
ENV MATPLOTLIB_VERSION 2.0.2
RUN pip install matplotlib==$MATPLOTLIB_VERSION

#Copy the sample script using Matplotlib.
COPY plot.py .

docker-compose.yml


version: '3.3'
services:
  plot:
    build: .
    environment:
      MPLCONFIGDIR: /etc
    volumes:
      - .:/opt/data-volume
    working_dir: /opt/data-volume
    entrypoint:
      - python
      - /opt/app/plot.py

plot.py


from matplotlib import pyplot as plt


if __name__ == '__main__':
  labels = ['sardine', 'mackerel', 'pike', 'Oarfish']
  x = list(range(1, len(labels) + 1))
  y = [1, 3, 5, 15]

  plt.bar(x, y, align='center')
  plt.xticks(x, labels, rotation='vertical')
  plt.xlabel('Types of fish')
  plt.ylabel('strength(pt)')
  plt.tight_layout()
  plt.savefig('plot.png')

Execution result

plot.png

reference

Recommended Posts

Run Matplotlib on a Docker container
Run matplotlib on a Windows Docker container
Carry a Docker container
Run a Python file inside a Docker container on a remote Raspbian via PyCharm
Run IPython Notebook on Docker
Launch Django on a Docker container with docker-compose up
How to run a Django application on a Docker container (development and production environment)
Run a Linux server on GCP
Run TensorFlow Docker Image on Python3
Run headless-chrome on a Debian-based image
How to delete a Docker container
Run TensorFlow2 on a VPS server
Create a Docker container image with JRE8 / JDK8 on Amazon Linux
Run Keycloak on Amazon Linux 2 without Docker
Run Python code on A2019 Community Edition
Run a Python web application with Docker
Run Jupyter notebook on a remote server
pykintone on Docker
Build a LAMP environment on your local Docker
Periodically run a python program on AWS Lambda
Use the Kaggle API inside a Docker container
Try Python interactive mode inside a Docker container
Visualize grib2 on a map with python (matplotlib)
Install docker on Fedora31
A memorandum about matplotlib
Run mysqlclient on Lambda
How to build a Django (python) environment on docker
Run OpenMVG on Mac
Install Docker on Arch Linux and run it remotely
Install Docker on AWS
Solution for inaccessible Gin server running on Docker container
Build a Docker container and save png from altair
How to run Django on IIS on a Windows server
Install Python 3.6 on Docker
How to run a trained transformer model locally on CloudTPU
Create a VS Code + Docker development environment on a Linux VM
[Django] Build a Django container (Docker) development environment quickly with PyCharm
Run Jupyter on Ubuntu on Windows
Run Openpose on Python (Windows)
Asynchronously run matplotlib interactive mode
Use matplotlib on Ubuntu 12 & Python
A comment on Boruta algorithm
Set matplotlib font on ubuntu
Run Tensorflow 2.x on Python 3.7
Run Python CGI on CORESERVER
Run unix command on python
Create a classroom on Jupyterhub
Install Docker on WSL Ubuntu 18.04
Run YOLO v3 on AWS v2
View matplotlib plots on Tensorboard
Run CircuitPython on Seeeduino XIAO
Run Jupyter Notebook on windows
Run FreeBSD on Linux + qemu
Run OpenVino on macOS Catalina
docker run --help Japanese translation
Run YOLO v3 on AWS
Run a Java app that resides on AWS EC2 as a daemon
Resolve garbled Japanese characters in matplotlib of Jupyter Notebook on Docker
Building an environment to run ChainerMN on a GPU instance on AWS
Prepare a machine learning project format and run it on SageMaker