#!/usr/bin/python
#-*- encoding: utf-8 -*-
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.patches as pat
from matplotlib.backends.backend_agg import
FigureCanvasAgg
import cStringIO
from flask import Flask, render_template, make_response
app = Flask(__name__)
# https://python.atelierkobato.com/rectangle/
@app.route("/pat1")
def pat1():
fig, ax = matplotlib.pyplot.subplots()
# 左下の座標(0.3, 0.3), 横幅0.5, 高さ0.25
# 回転角0°, 塗り潰し色blue, 透明度0.5
rec1 = pat.Rectangle(xy = (0.3, 0.3), width =
0.5, height = 0.25,
angle = 0, color = "blue", alpha = 0.5)
# 左下の座標(0.3, 0.3), 横幅0.5, 高さ0.25
# 回転角30°, 塗り潰し色red, 透明度0.5
rec2 = pat.Rectangle(xy = (0.3, 0.3), width =
0.5, height = 0.25,
angle = 30, color = "red", alpha = 0.5)
# 左下の座標(0.3, 0.3), 横幅0.5, 高さ0.25
# 回転角60°, 塗り潰し色red, 透明度0.5
rec3 = pat.Rectangle(xy = (0.3, 0.3), width =
0.5, height = 0.25,
angle = 60, color = "green", alpha = 0.5)
# Axesに長方形を追加
ax.add_patch(rec1)
ax.add_patch(rec2)
ax.add_patch(rec3)
canvas = FigureCanvasAgg(fig)
buf = cStringIO.StringIO()
canvas.print_png(buf)
data = buf.getvalue()
response = make_response(data)
response.headers['Content-Type'] =
'image/png'
response.headers['Content-Length'] =
len(data)
return response
@app.route("/pat2")
def pat2():
fig, ax =
matplotlib.pyplot.subplots(figsize=(6,6))
# fc = face color, ec = edge color
plt.xlim(-1, 1)
plt.ylim(-1, 1)
c = pat.Circle(xy=(0, 0), radius=0.5, fc='g',
ec='r')
e = pat.Ellipse(xy=(-0.25, 0), width=0.5,
height=0.25, fc='b', ec='y')
r = pat.Rectangle(xy=(0, 0), width=0.25,
height=0.5, ec='#000000', fill=False)
# Axesに図形を追加
ax.add_patch(c)
ax.add_patch(e)
ax.add_patch(r)
canvas = FigureCanvasAgg(fig)
buf = cStringIO.StringIO()
canvas.print_png(buf)
data = buf.getvalue()
response = make_response(data)
response.headers['Content-Type'] =
'image/png'
response.headers['Content-Length'] =
len(data)
return response
if __name__ == "__main__":
app.run(host='0.0.0.0', port=80, debug=True)
|