flask推送matplotlib绘图到html

1. matplotlib绘图

import matplotlib.pyplot as plt
import numpy as np
import base64
from io import BytesIO
    #############################################绘图测试
    X = np.linspace(-np.pi, np.pi, 256, endpoint=True)  # -π to+π的256个值
    C, S = np.cos(X), np.sin(X)
    #plt.rcParams['figure.dpi'] = 100  # 分辨率
    #plt.rcParams['savefig.dpi'] = 100  # 图片像素
    plt.rcParams['figure.figsize'] = (8.0, 4.0)  # 设置figure_size尺寸800x400
    plt.plot(X, C)
    plt.plot(X, S)

 

2. 转二进制

    # figure 保存为二进制文件
    buffer = BytesIO()
    plt.savefig(buffer)
    plot_data = buffer.getvalue()
    # 将matplotlib图片转换为HTML
    imb = base64.b64encode(plot_data)  # 对plot_data进行编码
    ims = imb.decode()
    imd = "data:image/png;base64," + ims

3.推送

return render_template('test.html',  img=imd)

4. html



    
        
        
        
    
    
    

查询 > 热点概念

 

你可能感兴趣的:(web开发,量化交易系统,python,量化交易系统)