Create SVG graph with matplotlib on heroku (displayed in Japanese)

Introduction

Please refer to Past Article for graph creation without Japanese.

Preparation of Japanese font

Prepare a suitable Japanese font file and add it to git. Google's Noto.

$ cd qiita-sin
$ mkdir fonts
$ cp .../NotoSansCJKjp-Medium.otf fonts/

App fix

app.py


#!/bin/env python
# coding: utf-8

import os
import StringIO

from flask import Flask, render_template
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
import numpy as np

fontprop = FontProperties(fname='./fonts/NotoSansCJKjp-Medium.otf', size=10)

app = Flask(__name__)
app.debug = True

@app.route('/')
def do_sin():
    x = np.arange(-np.pi, np.pi, 0.1)
    y = np.sin(x)

    fig, ax = plt.subplots(1)
    plt.plot(x, y, label="sin")
    plt.legend(loc="best")

    strio = StringIO.StringIO()
    fig.savefig(strio, format="svg")
    plt.close(fig)

    strio.seek(0)
    svgstr = strio.buf[strio.buf.find("<svg"):]

    return render_template("sin.html", svgstr=svgstr.decode("utf-8"))

@app.route('/sin-jp')
def do_sin_jp():
    x = np.arange(-np.pi, np.pi, 0.1)
    y = np.sin(x)

    fig, ax = plt.subplots(1)
    plt.plot(x, y, label=u"sin curve")
    plt.legend(loc="best", prop=fontprop)
    ax.set_title(u"test", font_properties=fontprop)
    ax.set_xlabel(u"x axis", font_properties=fontprop)
    ax.set_ylabel(u"y axis", font_properties=fontprop)

    strio = StringIO.StringIO()
    fig.savefig(strio, format="svg")
    plt.close(fig)

    strio.seek(0)
    svgstr = strio.buf[strio.buf.find("<svg"):]

    return render_template("sin.html", svgstr=svgstr.decode("utf-8"))

if __name__ == "__main__":
    port = int(os.environ.get("PORT", 5000))
    app.run(port=port)

See https://limitless-garden-3527.herokuapp.com/sin-jp for the completed app

point

Prepare a font file by yourself and create a fontprop, and specify the fontprop when drawing.

Recommended Posts

Create SVG graph with matplotlib on heroku (displayed in Japanese)
Write SVG graphs with matplotlib on heroku
Draw Japanese with matplotlib on Ubuntu
Create a graph with borders removed with matplotlib
Japanese with matplotlib
Create an image with characters in python (Japanese)
Band graph with matplotlib
Put Scipy + Matplotlib in Ubuntu on Vagrant and display the graph with X11 Forwarding
Graph Excel data with matplotlib (1)
What to do when Japanese is not displayed on matplotlib
Create a list in Python with all followers on twitter
Create graph with plotly button
Graph drawing method with matplotlib
Union Find on networkX
Use directional graphs with networkx
Real-time graphs on Plotly (Python)
Drawing graphs on Windows10 + Julia + PyPlot
Visualize graphs with Japanese labeled edges in NetworkX and PyGraphviz / Gephi
Write SVG graphs with matplotlib on heroku
Multiple file processing with Kivy + Matplotlib + Draw Graph on GUI
Graph Excel data with matplotlib (2)
Write charts in real time with Matplotlib on Jupyter notebook
I want to create a graph with wavy lines omitted in the middle with matplotlib (I want to manipulate the impression)
Resolve garbled Japanese characters in matplotlib of Jupyter Notebook on Docker
matplotlib: Insert comment on timeline graph
Draw a loose graph with matplotlib
How to run matplotlib on heroku
Draw retention rate graph in Matplotlib
Heatmap with Dendrogram in Python + matplotlib
Easy Japanese font setting with matplotlib
Make matplotlib Japanese compatible in 3 minutes
(Matplotlib) I want to draw a graph with a size specified in pixels
Create and edit spreadsheets in any folder on Google Drive with python
Create a Python3 environment with pyenv on Mac and display a NetworkX graph
You can do it in 5 minutes !? Create a face detection API with FastAPI and OpenCV and publish it on Heroku
[Python] Set the graph range with matplotlib
Put Japanese fonts in images with Colaboratory
Notes on doing Japanese OCR with Python
Display Matplotlib xy graph in PySimple GUI.
Display Japanese graphs with VS Code + matplotlib
[Python] Let's make matplotlib compatible with Japanese
Graph trigonometric functions with numpy and matplotlib
Launch Flask application with Docker on Heroku
[Python] Japanese localization of matplotlib on Ubuntu
The date is displayed incorrectly in matplotlib.