{
"上海": 15260,
"杭州": 12586,
"北京": 13305,
"天津": 9000,
"深圳": 16060,
"广州": 14010,
"西安": 8000,
"重庆": 7800,
"成都": 8500
}
import json
def json_to_dict(filename):
json_file = open(filename, 'r', encoding='utf-8')
return json.loads(json_file.read())
from . import matlot
from matplotlib.pylab import *
from flask import render_template
from io import BytesIO
from .func import json_to_dict, ax
import base64
def json_img(data):
font = {'family': 'SimHei',
'weight': 'bold',
'size': '14'}
rc('font', **font) # 设置支持中文
rc('axes', unicode_minus=False)
rcParams['savefig.dpi'] = 100 # 设置像素大小
for x, y in data.items():
bar(x, y, align='center') # 生成柱状图
text(x, y + 100, '%.2f' % y, ha='center', va='bottom', size=10, color="#909299") # 柱状添加标签数据
# 写入内存,py3 BytesIO()
img = BytesIO()
savefig(img, format='png')
img.seek(0)
# img转义base64编码
img_base64 = base64.b64encode(img.getvalue()).decode('utf8')
# 边框,边框坐标处理
ax = gca()
ax.spines['right'].set_color('none') # 隐藏右边框线
ax.spines['top'].set_color('none') # 隐藏上边框线
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data',0)) # 定位下边框线0位置
return img_base64
@matlot.route('/')
def fig():
data = json_to_dict('./file/matlot.json')
# print(data)
# return send_file(json_img(data), mimetype='image/png')
return render_template('fig.html',img_base64=json_img(data))