zoukankan      html  css  js  c++  java
  • Python3使用plotly模块保存图片

    目的

    使用Python3的Plotly模块,实现对数据库数据的读取,然后形成Table表格,并通过钉钉机器人定时发送到钉钉群组。
     

    难点

    Plotly本身是通过html页面展示的,先要把图片保存下来需要安装一些其他的包;通过网上的无数文章都是错误的,弄了将近两天才把环境搞得,不得不感慨一下,下面就自己总结成文,飞快的就能搞定。
     

    步骤

    1、环境
    CentOS Linux release 7.6.1810 (Core)
    2、Python环境
     
     
    3、基本库安装psutil
    pip3 install psutil requests
    4、使用plotly模块必须得安装orca
    wget https://github.com/plotly/orca/releases/download/v1.2.1/orca-1.2.1-x86_64.AppImage
    chmod +x orca-1.2.1-x86_64.AppImage
     
    # 绝对路径
    ln -s /root/orca-1.2.1-x86_64.AppImage /usr/bin/orca

    # 看orca命令是否可以显示正确,如果报依赖包的错误,可以看下一个步骤,是主要的一些依赖包 orca
    --help
    5、orca安装相关得系统依赖包 
    yum install fuse-libs-2.9.2-11.el7.x86_64
    yum install gtk2-2.24.31-1.el7.x86_64
    yum install desktop-file-utils 
    yum install Xvfb
    yum install xdg-utils-1.1.0-0.17.20120809git.el7.noarch

    Tips:

    这里报依赖包没找到的话,你又不知道安装什么样的依赖包,那就通过这个命令去获取 yum provides 

    yum provides libfuse.so.2
    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
     * base: ftp.heanet.ie
     * extras: ftp.heanet.ie
     * updates: ftp.heanet.ie
    fuse-libs-2.9.2-11.el7.i686 : File System in Userspace (FUSE) libraries
    Repo : base
    Matched from:
    Provides : libfuse.so.2

    6、添加脚本executable.sh

    orca 执行不报错后,执行脚本executable.sh,里面的文件路径为第4步下载的文件

    #!/bin/bash
    xvfb-run -a /root/orca-X.Y.Z-x86_64.AppImage "$@"

    7、测试

    import plotly.graph_objects as go
    import plotly.io as pio
    
    
    fig = go.Figure(data=[go.Table(header=dict(values=['A Scores', 'B Scores']),
                     cells=dict(values=[[100, 90, 80, 90], [95, 85, 75, 95]]))
                         ])
    
    pio.write_image(fig, '1.png')

    执行完这个脚本后,就会发现当前目录下有一个1.png,就是我们保存的图片,后续的话就可以通过钉钉的告警脚本把图片发送到钉钉群组里。

    相关链接:
    https://plot.ly/python/basic-charts
     
  • 相关阅读:
    Populating Next Right Pointers in Each Node II
    Populating Next Right Pointers in Each Node
    Construct Binary Tree from Preorder and Inorder Traversal
    Construct Binary Tree from Inorder and Postorder Traversal
    Path Sum
    Symmetric Tree
    Solve Tree Problems Recursively
    632. Smallest Range(priority_queue)
    609. Find Duplicate File in System
    poj3159最短路spfa+邻接表
  • 原文地址:https://www.cnblogs.com/lemon-le/p/12092415.html
Copyright © 2011-2022 走看看