python安装pygal_如何在pygal python中创建直方图

, a Python SVG graph-plotting library. You’ll also learn how to draw histograms using the Pygal library.

可伸缩矢量图形(SVG)是一种图像格式,以XML格式定义了基于矢量的图形。 在本教程中,您将了解如何开始使用 (Python SVG图形绘图库)。 您还将学习如何使用Pygal库绘制直方图。

Pygal简介 (Introduction to Pygal)

There are many libraries in Python available for charts ( and being a couple of examples). Even though most of these libraries aren’t that complex, most Python-charting developers require a simple, easy-to-use library that can quickly build Python applications.

Python中有许多可用于图表的库( 和是两个示例)。 尽管大多数这些库并不是那么复杂,但是大多数使用Python绘制图表的开发人员都需要一个简单易用的库,该库可以快速构建Python应用程序。

The Pygal library offers multiple charting options beyond what’s available in other charting libraries. It includes a world map, funnel charts, radar charts, and box plots. It also includes prebuilt themes and styles, which you don’t have to customize unless you want to. Pygal also integrates well with web development, especially with Flask and Django.

除了其他图表库提供的功能外,Pygal库还提供了多种图表选项。 它包括世界地图,漏斗图,雷达图和箱形图。 它还包括预建的主题和样式,除非您愿意,否则无需自定义。 Pygal还与Web开发很好地集成在一起,尤其是与Flask和Django。

Pygal, as a library for data science, enables the creation of graphics in SVG format. The SVG format is prevalent in digital projects because it makes it possible to create interactive displays and to download graphics in image format, specifically in PNG.

Pygal作为数据科学的库,可以创建SVG格式的图形。 SVG格式在数字项目中很普遍,因为它可以创建交互式显示并下载图像格式的图形,尤其是PNG。

安装Pygal (Install Pygal)

Pygal is available in Python, and installing it is as simple as:


pip install pygal

Ensure that you have pip installed on your machine.


To test if the installation has been successful, open a Python interpreter, and issue the following command.


import pygal

If no errors occur, then the installation has been successful.


You can also clone it from the official GitHub repository if you want to use the latest version of Pygal.


pip install git+https://github.com/Kozea/pygal

为什么是Pygal? (Why Pygal?)

Let’s look at some of the reasons why you might prefer to use Pygal:


1.容易理解 (1. Easy to understand)

The Pygal library is very easy to understand because it has less imports than Matplotlib and other plotting modules. Let’s compare this by writing the code required to draw a chart in Pygal and in Matplotlib.

Pygal库非常易于理解,因为它的导入量少于Matplotlib和其他绘图模块。 让我们通过在Pygal和Matplotlib中编写绘制图表所需的代码进行比较。

Draw a chart in Pygal.


# First import pygal
import pygal
# Then create a bar graph object
bar_chart = pygal.Bar()
# Add some values
bar_chart.add('Fibonacci', [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55])
# Save the svg to a file

Draw a chart in Matplotlib.


# Import the necessary packages and modules
import matplotlib.pyplot as plt
import numpy as np

# Prepare the data
x = np.linspace(0, 10, 100)

# Plot the data
plt.plot(x, x, label='linear')

# Add a legend

# Show the plot

As you can see above, it only takes four lines of code and one import to draw a chart in Pygal, compared to Matplotlib, which takes considerably more code and more imports.


2.更少的依赖 (2. Fewer dependencies)

The Pygal library also doesn’t need any dependencies, which might be confusing for new developers.


3.灵活的输出 (3. Flexible output)

The charts-library output is SVG, a highly flexible output type for HTML5 and other print media. The SVG format is also scalable and can be resized without losing its quality.

图表库输出为SVG,这是HTML5和其他印刷媒体的高度灵活的输出类型。 SVG格式也是可伸缩的,可以调整大小而不会损失其质量。

入门 (Getting Started)

A histogram is a bar chart that gives a quantitative analysis of the data.


Histograms deal more with data distribution and tell us more about how many of an item are in each category. It’s also important to note that histograms represent a continuous data set, and, as such, there are no gaps in the data defined by a histogram. An example of a histogram and the raw data it was constructed from is shown below:

直方图更多地处理数据分布,并告诉我们更多有关每个类别中多少个项目的信息。 同样重要的是要注意,直方图表示一个连续的数据集,因此,直方图定义的数据中没有空白。 直方图及其原始数据的示例如下所示:

Image for post
An example of a histogram

创建直方图(Creating a Histogram)

Data visualization explains the information in the form of charts or graphs. In this tutorial, you’ll see how to create a histogram using the Pygal library. Let’s start by creating a simple histogram with predefined values.

数据可视化以图表或图形的形式解释信息。 在本教程中,您将看到如何使用Pygal库创建直方图。 让我们从创建具有预定义值的简单直方图开始。

Create a file called histogram.py, and add the following code:


import pygal
# create histogram object
hist = pygal.Histogram(fill=True)
# add values to histogram object
hist.add('bars', [(10, 1, 2), (12, 4, 4.5), (8, 11, 13)])
# render histogram as SVG
hist.render_to_file('histogram.svg') # set filename.

In the code above, you first import the Pygal library, create a histogram object, and then add values to the histogram object. The final step is to render the created histogram to an SVG file.

在上面的代码中,您首先导入Pygal库,创建一个直方图对象,然后将值添加到直方图对象。 最后一步是将创建的直方图呈现为SVG文件。

The resulting graph is stored in the same directory as the file histogram.py. So go ahead: Open it with your browser, and you should see something like the following.

生成的图形与文件histogram.py存储在同一目录中。 因此,继续:用浏览器打开它,您应该看到类似以下的内容。

Image for post
Create histogram

加载动态数据(Loading Dynamic Data)

The values above are predefined; let’s assume you already have some data you want to present through a histogram, as follows.

上面的值是预定义的; 假设您已经有一些要通过直方图显示的数据,如下所示。

In this example, you have some data of scores of students in a test as follows:




The first thing to do is analyze this data by breaking the range of scores into intervals. From the data above, the highest value is 98, while the lowest value is 46.

首先要做的是通过将分数范围划分为间隔来分析此数据。 根据上面的数据,最大值是98 ,而最小值是46

The easiest way is to have intervals of 10 to represent the data points; hence, the resulting analysis will be:

最简单的方法是使用10个间隔来表示数据点。 因此,结果分析将是:

Image for post
Table of data

Now that you’re done with the analysis, you’ll represent the data in a histogram. You generally want to display scores along the X-axis and the frequency along the Y-axis. First, you’ll need to import the Pygal library.

现在您已经完成了分析,您将以直方图表示数据。 通常,您希望沿X轴显示得分,并沿Y轴显示频率。 首先,您需要导入Pygal库。

import pygal

Create a Histogram object from the Pygal library.


hist = pygal.Histogram()

Once you have the Histogram object, you need to assign the X-axis and Y-axis values to the Histogram object.


hist.add(‘students’, [(1, 40, 50), (2, 50, 60),(3, 60, 70), (3, 70, 80), (2, 80, 90), (1, 90, 100)])

Now you need to render the Histogram to an SVG file. This is how to render the SVG image to a file:

现在,您需要将Histogram呈现为SVG文件。 这是将SVG图像渲染到文件的方法:


The final code should look like this:


import pygal

# create histogram object
hist = pygal.Histogram()

# add data
hist.add('students', [(1, 40, 50), (2, 50, 60),
(3, 60, 70), (3, 70, 80), (2, 80, 90), (1, 90, 100)])

# render to a svg file

You’ve created your first histogram. Navigate to the directory where the main script is, and you should see the graph saved as histogram.svg. Open it with your browser, and it should be rendered as shown:

您已经创建了第一个直方图。 导航到主脚本所在的目录,您应该看到该图另存为histogram.svg 。 用您的浏览器打开它,它应该显示如下:

Image for post
Histogram screenshot

调整直方图(Tweaking Our Histogram)

Our histogram looks a bit thick; let’s change the interval frequency to at least a 5 instead. So our data will look like this:

我们的直方图看起来有点粗; 让我们将间隔频率更改为至少5 。 因此我们的数据将如下所示:

Image for post
Histogram data analysis

The full code will look like this:


import pygal

# create histogram object
hist = pygal.Histogram()

# add data
hist.add('students', [(0, 40, 45), (1, 45, 50),
(2, 50, 55), (0, 55, 60), (0, 60, 65), (3, 65, 70),
(1, 70, 75), (2, 75, 80), (1, 80, 85),
(1, 85, 90), (0, 90, 95), (1, 95, 100)])

# render to a svg file

You then go ahead and represent the data, as you did in the previous example, as shown below.


Image for post

Alternatively, you can draw two histograms on the same graph. Let’s assume you want to compare the performance of two classes in the same exam, and the data is as follows:

或者,您可以在同一张图上绘制两个直方图。 假设您要比较同一考试中两个班级的表现,数据如下:



Our analysis, according to how many students scored between defined scores, will be as shown below:


scores |frequency
[40,50] | 1
[50,60] | 3
[60,70] | 3
[70,80] | 2
[80,90] | 1
[90,100] | 2

The full code after filling in the data is shown below:


import pygal

# create histogram object
hist = pygal.Histogram(fill=True)

# set values for x axis
hist.x_labels = [40, 50, 60, 70, 80, 90, 100]

# set values for y axis
hist.y_labels = [0, 1, 2, 3, 4, 5
# add data
hist.add('A', [(1, 40, 50), (2, 50, 60),
               (3, 60, 70), (3, 70, 80), (2, 80, 90), (1, 90, 100)])
hist.add('B', [(1, 40, 50), (3, 50, 60),
               (3, 60, 70), (2, 70, 80), (1, 80, 90), (2, 90, 100)])
# render to a svg file

In the code above, you have gone the extra mile of adding values on the X-axis and the Y-axis so the histogram doesn’t define the values to be shown on both scales. The result is as shown below:

在上面的代码中,您花了更多精力在X轴和Y轴上添加值,因此直方图不会定义要在两个刻度上显示的值。 结果如下图所示:

Image for post
Final histogram

自定义样式(Custom Styles)

You can also add custom styles to your histogram. For example, when adding colors, you need to import the Pygal style attribute.

您还可以将自定义样式添加到直方图中。 例如,添加颜色时,您需要导入Pygal样式属性。

from pygal.style import Style

Next, define the custom style to specify the bar’s colors, and specify a background color for the chart.


style = Style(colors=(‘#991593’, ‘#201599’))

Apply the custom style to the histogram:


chart = pygal.Bar(style = custom_style)

Save the above changes and refresh the file. You should be able to view the histogram with the custom styles rendered, as shown below.

保存以上更改并刷新文件。 您应该能够查看呈现了自定义样式的直方图,如下所示。

Image for post
Styled histogram

Additionally, you can add other styling options, including transparency, font family, legend, and background, as shown below:


style = Style(colors=(‘#991593’, ‘#201599’),label_font_size=39,opacity=’.7',legend_font_size=54)

Refresh the file, and you should see your changes in effect.


Image for post
More styles

汇入资料(Importing Data)

JSON is a popular format for storing data. Let’s assume you have some data in JSON format that has been scrapped or obtained from another source, and you’d like to plot a histogram with this kind of data. The data is as stored in a file as data.json.

JSON是用于存储数据的流行格式。 假设您有一些JSON格式的数据已从其他来源中删除或获取,并且您希望使用此类数据绘制直方图。 数据作为data.json存储在文件中。

[{“frequency”: 1,“min”: 40,“max”: 50},
{“frequency”: 2,“min”: 50,“max”: 60},
{“frequency”: 3,“min”: 60,“max”: 70},
{“frequency”: 3,“min”: 70,“max”: 80},
{“frequency”: 2,“min”: 80,“max”: 90},
{“frequency”: 1,“min”: 90,“max”: 100}

Since you’ll be loading data from a JSON file, you’ll need to import the JSON library.


import json

Load the JSON data from the .json file.


with open(‘data.json’,’r’) as file:
data = json.load(file)

Create a Histogram object from the Pygal library.


histogram = pygal.Histogram()

Set values for the X- and Y-axes by reading the frequency as a list from the JSON data object.


x_labels_ = [x[‘frequency’] for x in data]
y_labels_ = [x[‘min’] for x in data]

Assign the labels data to the Histogram object.


# x axishist.x_labels = x_labels
# y axishist.y_labels = y_labels

Add the data to the Histogram object.


def convert(data):
    data2 = []
    for i in data:
        data.append((tuple([i["frequency"], i["min"], i["max"]])))
    return data2
hist.add('A', convert(data))

Finally, render the histogram SVG image to a file.



结论 (Conclusion)

In this tutorial, you saw how to use Pygal, a Python SVG graph-plotting library. You saw how to add multiple histograms to the same graph and also how to customize the bar-chart style.

在本教程中,您了解了如何使用Pygal(Python SVG图形绘图库)。 您已经了解了如何向同一图形添加多个直方图,以及如何自定义条形图样式。

With Pygal, you can draw different types of charts, such as line charts, bar charts, histograms, XY charts, pie charts, radar charts, box charts, dot charts, funnel charts, solid-gauge charts, gauge charts, pyramid charts, treemaps, and maps.

使用Pygal,您可以绘制不同类型的图表,例如折线图,条形图,直方图, XY图,饼图,雷达图,箱形图,点图,漏斗图,实心图,量规图,金字塔形图,树状图和地图。

The procedure and commands for creating charts are also similar to creating a histogram. For example, this is the code for creating a simple line chart with Pygal.

创建图表的过程和命令也类似于创建直方图。 例如,这是用于使用Pygal创建简单折线图的代码。

line_chart = pygal.Line()
line_chart.title = 'Browser usage evolution (in %)'
line_chart.x_labels = map(str, range(2002, 2013))
line_chart.add('Firefox', [None, None,    0, 16.6,   25,   31, 36.4, 45.5, 46.3, 42.8, 37.1])
line_chart.add('Chrome',  [None, None, None, None, None, None,    0,  3.9, 10.8, 23.8, 35.3])
line_chart.add('IE',      [85.8, 84.6, 84.7, 74.5,   66, 58.6, 54.7, 44.8, 36.2, 26.6, 20.1])
line_chart.add('Others',  [14.2, 15.4, 15.3,  8.9,    9, 10.4,  8.9,  5.8,  6.7,  6.8,  7.5])