python 图表转图片

原创
2023/08/25 17:45
阅读数 59

机器人报警 图表转pic 的方式如下

import plotly.graph_objects as go
import plotly.io as pio
from plotly.subplots import make_subplots

def get_table_png(header,cell,file_name):
    layout = go.Layout(autosize=True,margin={'l': 0, 'r': 0, 't': 50, 'b': 0},height=240)
    fig = go.Figure(data=[go.Table(header = header,
                              cells = cell,
                              )
                     ],layout=layout,name="test_data"
                )
    pio.write_image(fig, file_name)

def get_table_and_pie_png(data,file_name, title):
    fig = make_subplots(rows=2,cols=1, subplot_titles=["proportion","statistics"],specs=[[{"type": "pie"}],[{"type":"table"}]])
    pie_sub=go.Pie(
        values=[data["equal"],data["deleted"],data["not_equal"],data["abnormal"],data["state!=1"]],
        labels=["equal", "deleted", "not_equal", "abnormal","state!=1"],
        domain=dict(x=[0.5, 1.0]))
    header=dict(values=["all","equal", "deleted", "not_equal", "abnormal","state!=1"])
    cell=dict(values=[[data["all"]],[data["equal"]],[data["deleted"]],[data["not_equal"]],[data["abnormal"]],[data["state!=1"]]])
    table_sub = go.Table(header = header,cells=cell)
    fig.add_trace(pie_sub, 1,1)
    fig.add_trace(table_sub, 2,1)
    fig.update_layout(title_text=title)
    pio.write_image(fig, file_name)

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部