Pygal 中的输出

发布日期:2026-07-10 00:07:03   来源 : 杭州电子商务研究院    浏览量 :5
杭州电子商务研究院 发布日期:2026-07-10 00:07:03  
5

介绍

Pygal是一个非常通用的库。它以 SVG(标量矢量图形)提供动态动画图形或绘图。它还提供多种输出格式,可在不同情况下派上用场。在本指南中,我们将通过示例学习如何获取每种类型的输出。我们还将研究如何将 pygal 嵌入 Flask 应用程序中。

如果您是 pygal 新手,请查看我之前有关Pygal 中的图表的指南。

输出类型

我假设您具有 Python 的基本知识,已经在系统上安装了 Python 3 和 pygal 打包库,并且拥有 Web 浏览器。在本指南中,我们将学习如何生成以下格式的输出:

  • 输出为字符串
    • 以字节为单位输出
    • 以 Unicode 输出
  • 输出到文件中
  • 输出为 png
  • 输出为 etree
  • 输出为 base 64 数据 URI
  • 浏览器中的输出
  • PyQuery 中的输出
  • Flask 应用程序中的输出

让我们详细讨论一下每一个问题。

输出为字符串

输出(以字节为单位)

这种输出格式将为我们提供 SVG 格式的矢量输出。

      # Importing pygal
import pygal
# creating line chart object
line_chart = pygal.Line()
line_chart.title = 'A vs B'
line_chart.x_labels = map(str, range(0, 20))
line_chart.add('A', [None, None, 0, 1, 1, 2, 3, 5, None, 13, 21, 34, 55])
line_chart.add('B',  [9.2, 19.4, 15.3,  8.9, None, 9, 10.4, 10, 5.8, 6.7, 6.8, 7.5])
# this will render SVG as bytes
line_chart.render()
    

以 Unicode 输出

这种输出格式还将为我们提供 Unicode 中的 SVG 矢量输出。

      # Importing pygal
import pygal
# creating line chart object
line_chart = pygal.Line()
line_chart.title = 'A vs B'
line_chart.x_labels = map(str, range(0, 20))
line_chart.add('A', [None, None, 0, 1, 1, 2, 3, 5, None, 13, 21, 34, 55])
line_chart.add('B',  [9.2, 19.4, 15.3,  8.9,None, 9, 10.4, 10, 5.8, 6.7, 6.8, 7.5])
# this will render ouput as unicode
line_chart.render(is_unicode=True)
    

输出到文件中

在这种输出格式中,如果您未指定位置,您的文件将保存在当前目录中。

      # Importing pygal
import pygal
# creating line chart object
line_chart = pygal.Line()
line_chart.title = 'A vs B'
line_chart.x_labels = map(str, range(0, 20))
line_chart.add('A', [None, None, 0, 1, 1, 2, 3, 5, None, 13, 21, 34, 55])
line_chart.add('B',  [9.2, 19.4, 15.3,  8.9, None, 9, 10.4,  10, 5.8, 6.7, 6.8, 7.5])
# this will render output file in current directory
line_chart.render_to_file('line_chart.svg')
    

输出为 Png

在开始之前,如果我们想以 png 格式输出,我们必须安装依赖项。

xml文件

  • pip install:要使用pip安装,请打开终端并运行以下命令:

    pip install lxml
    
    • conda Install: To install using conda, open the terminal and run the following command:

      conda install -c anaconda lxml 
      

cairosvg

    • pip install: To install using pip, open the terminal and run the following command:

      pip install cairosvg
      
      • conda Install: To install using conda, open the terminal and run the following command:

        conda install -c conda-forge/label/cf201901 cairosvg
        

cssselect

    • pip install: To install using pip, open the terminal and run the following command:

      pip install cssselect
      
      • conda Install: To install using conda, open the terminal and run the following command:

        conda install -c anaconda cssselect 
        

tinycss

    • pip install: To install using pip, open the terminal and run the following command:

      pip install tinycss
      
      • conda Install: To install using conda, open the terminal and run the following command:

        conda install -c conda-forge/label/cf201901 tinycss
        
      # Importing pygal
import pygal
# creating line chart object
line_chart = pygal.Line()
line_chart.title = 'A vs B vs C'
line_chart.x_labels = map(str, range(0, 20))
line_chart.add('A', [None, None, 0, 1, 1, 2, 3, 5, None, 13, 21, 34, 55])
line_chart.add('B',  [9.2, 19.4, 15.3,  8.9, None, 9,  10.4,  10, 5.8, 6.7, 6.8, 7.5])
line_chart.add('C',  [1, 1, 1, 2, 2, 3, 4, 5, 7, 9, 12, 5.7, 4.8, 6.2, 66, 36])
# this will render output file in current directory
line_chart.render_to_png('line_chart.png')
    

注意:渲染的 png 中不会有动画。

输出为 Etree

此输出格式将返回 SVG 根 etree 节点。

      # Importing pygal library
import pygal
# creating line chart object
line_chart = pygal.Line()
line_chart.title = 'A vs B'
line_chart.x_labels = map(str, range(0, 20))
line_chart.add('A', [None, None, 0, 1, 1, 2, 3, 5, None, 13, 21, 34, 55])
line_chart.add('B',  [9.2, 19.4, 15.3, 8.9, None, 9, 10.4, 10, 5.8, 6.7, 6.8, 7.5])
# this will render output
line_chart.render_tree()
    

输出为 Base 64 数据 URI

      import pygal
# creating line chart object
line_chart = pygal.Line()
line_chart.title = 'A vs B'
line_chart.x_labels = map(str, range(0, 20))
line_chart.add('A', [None, None, 0, 1, 1, 2, 3, 5, None, 13, 21, 34, 55])
line_chart.add('B',  [9.2, 19.4, 15.3, 8.9, None, 9, 10.4, 10, 5.8, 6.7, 6.8, 7.5])
# this will render output
line_chart.render_data_uri()
    

浏览器中的输出

Pygal 还可以使用render_in_browser命令直接在浏览器中渲染 SVG 。如果你使用 Jupyter 笔记本,这将非常有用。

      # Importing pygal library
import pygal
# creating object
xy_chart = pygal.XY(stroke=False)
# adding title
xy_chart.title = 'Correlation'
# adding random data
xy_chart.add('A', [(0, 0), (.1, .25), (.3, .1), (.5, .1), (.8, .6), (1, 1.08), (1.3, 1.1), (2, 3.23), (2.43, 2)])
xy_chart.add('B', [(.1, .15), (.14, .3), (.5, .3), (.3, .4), (.21, .21), (.5, .3), (.6, .8), (.7, .8)])
xy_chart.add('C', [(.5, .01), (.13, .02), (1.5, 1.7), (1.02, 1.6), (1.8, 1.63), (1.5, 1.82), (1.7, 1.23), (2.1, 2.23), (2.3, 1.98)])
# render file in browser
xy_chart.render_in_browser()
    

PyQuery 中的输出

对于 PyQuery,您首先必须在系统上安装 pyquery 库。

  • pip install:要使用pip安装,请打开终端并运行以下命令:
      pip install pyquery
    
  • conda 安装:要使用conda安装,请打开终端并运行以下命令:
      conda install -c anaconda pyquery
    

安装 pyquery 后,您可以通过调用render_pyquery获取 pyquery 对象来包装图表

      # Importing the pygal
import pygal
# creating line chart object
line_chart = pygal.Line()
line_chart.title = 'A vs B'
line_chart.x_labels = map(str, range(0, 20))
line_chart.add('A', [None, None, 0, 1, 1, 2, 3, 5, None, 13, 21, 34, 55])
line_chart.add('B',  [9.2, 19.4, 15.3,  8.9,None, 9, 10.4, 10, 5.8, 6.7, 6.8, 7.5])
# this will render output
line_chart.render_pyquery()
    

Flask 中的输出

为了在 Flask 应用程序中输出,您应该在系统上安装 Flask 和 virtualenv 库。

  • Flask 安装

    • pip install:要使用pip安装,请打开终端并运行以下命令:
    pip install Flask
    
    • conda Install: To install using conda, open the terminal and run the following command:
    conda install -c anaconda flask 
    
    • virtualenv library installation

      • pip install: To install using pip, open the terminal and run the following command:
      pip install virtualenv
      
      • conda Install: To install using conda, open the terminal and run the following command:
      conda install -c anaconda virtualenv 
      

In the given code, we are using pygal in flask app.

      # Importing flask module in the project is mandatory 
# An object of Flask class is our WSGI application.
# We also have to import pygal library
import pygal
from flask import Flask 
  
# current module (__name__) as argument. 
app = Flask(__name__) 
    
# the associated function. 
@app.route('/')
def line_route(): 
    line_chart = pygal.Line()
    line_chart =
以上内容来自杭州电子商务研究院推送
关注
关于我们
热门推荐
合作伙伴
免责声明:本站部分资讯来源于网络,如有侵权请及时联系客服,我们将尽快处理
Copyright © 2025-2027 ToB产业网址导航 公安备案 浙公网安备33010602013138号 浙ICP备16025413号-9
支持 反馈 关注 数据