安搭
软件安装搭建教程网是专门为软件开发、IT运维人员提供Apache、Centos、dotNET、Eclipse、Ftp、Git、java、mssqlserver、MySQL、Nginx、php、python、Redis、SVN、Vscode、VUE等服务器服务系统的安装教程、搭建教程、配置教程信息网。

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:

Pygal在Python中可用,安装过程非常简单:

pip install pygal

Ensure that you have pip installed on your machine.

确保您的机器上已安装pip。

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

要测试安装是否成功,请打开Python解释器,然后发出以下命令。

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.

如果您想使用最新版本的Pygal,也可以从GitHub官方存储库中克隆它。

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:

让我们看一下为什么您更喜欢使用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.

在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
bar_chart.render_to_file('bar_chart.svg')

Draw a chart in Matplotlib.

在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
plt.legend()


# Show the plot
plt.show()

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.

从上面可以看到,与Matplotlib相比,在Pygal中只需要四行代码和一次导入即可绘制图表,而Matplotlib则需要更多的代码和更多的导入。

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

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

Pygal库也不需要任何依赖关系,这可能会使新开发人员感到困惑。

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:

创建一个名为histogram.py的文件,并添加以下代码:

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:

在此示例中,您具有一些测试中的学生分数数据,如下所示:

46,78,51,52,83,87,69,71,65,67,76,98

46,78,51,52,83,87,69,71,65,67,76,98

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.

从Pygal库创建Histogram对象。

hist = pygal.Histogram()

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

拥有Histogram对象后,需要将X轴和Y轴值分配给Histogram对象。

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图像渲染到文件的方法:

hist.render_to_file(histogram1.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
hist.render_to_file('histogram2.svg')

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
hist.render_to_file('histogram2.svg')

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

然后,您像上一个示例一样继续表示数据,如下所示。

Image for post
Histogram
直方图

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:

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

96,54,76,92,63,64,66,59,77,56,87,42

96,54,76,92,63,64,66,59,77,56,87,42

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
hist.render_to_file('histogram.svg')

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.

由于您将从JSON文件加载数据,因此需要导入JSON库。

import json

Load the JSON data from the .json file.

.json文件加载JSON数据。

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

Create a Histogram object from the Pygal library.

从Pygal库创建Histogram对象。

histogram = pygal.Histogram()

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

通过从JSON数据对象中以列表形式读取频率来设置X轴和Y轴的值。

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

Assign the labels data to the Histogram object.

labels数据分配给Histogram对象。

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

Add the data to the Histogram object.

将数据添加到Histogram对象。

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.

最后,将直方图SVG图像渲染到文件中。

hist.render_to_file(‘histo.svg’)

结论 (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])
line_chart.render()

翻译自:

python安装pygal

文章来源:https://blog.csdn.net/weixin_26720549/article/details/108958676

相关推荐