Python 有不同的可视化包,可用于制作不同类型的图表、图形和绘图。Pygal 是一个开源的 Python 包,它不仅可以生成高度交互的绘图,还可以生成图形和绘图的 SVG 图片,允许我们根据需要使用和修改它们。Pygal 非常灵活,只需要几行代码即可生成图表。
另请阅读:4 个简单的 Python 绘图库及示例
在本教程中,我们将探索可以使用创建的折线图可视化PyGal
以及如何下载相同SVG
格式的折线图可视化。
在我们开始检查 pygal 之前,必须先安装它pip install pygal
。安装后,我们将导入 pygal 进行可视化。
我们将使用下面的代码从简单的折线图开始可视化。我们将首先使用该函数创建一个空折线图Line
,并使用该属性设置图表的标题title
。
接下来,我们将随机设置x_labels
,然后我们将为四种不同的编程语言制作折线图,并为每个折线图添加折线标签和值。最后,我们将使用该方法以 SVG 格式保存并渲染绘图render_to_file
。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 号
18
19 号
20
21
22
23
24
25
26
27
28
|
# Create an empty Line Chart Line_Chart = pygal.Line() # Set title of the Line Chart Line_Chart.title = 'A demonstration of Line Chart using Pygal' # Set x labels/values Line_Chart.x_labels = map ( str , range ( 2000 , 2022 )) # Adding Line Chart for each language popularity over the years Line_Chart.add( 'Python' , [ None , None , 0 , 16.1 , 22 , 30 , 36.9 , 45.1 , 46.1 , 42.33 , 37.11 , 38.5 , 50.78 , 53.62 , 55.37 , 60.22 , 60.5 , 61.68 , 68.65 , 79.83 , 80.25 , 85.44 ]) Line_Chart.add( 'Kotlin' , [ None , None , None , None , None , None , 0 , 3.5 , 10.1 , 23.1 , 35.2 , 24.03 , 46.53 , 46.74 , 47.31 , 59.93 , 16.11 , 22.94 , 23.56 , 1.86 , 16.07 ]) Line_Chart.add( 'C++' , [ 0.99 , 7.15 , 8.39 , 9.78 , 14.0 , 25.66 , 33.3 , 35.8 , 40.11 , 54.92 , 57.17 , 60.14 , 68.63 , 73.11 , 75.99 , 79.37 , 89.73 , 90.82 , 95.71 , 60.66 , 61.43 , 64.94 ]) Line_Chart.add( 'Java' , [ 14.1 , 15.8 , 15.1 , 8.8 , 8 , 10.1 , 18.4 , 15.1 , 16.6 , 16.1 , 17.4 , 19.27 , 21.24 , 29.29 , 38.04 , 43.24 , 54.0 , 62.55 , 11.28 , 12.08 , 13.19 , 29.35 ]) # Save the plot in svg format Line_Chart.render_to_file( 'Line_Chart_Demo.svg' ) |
我们还可以Horizontal Line Chart
借助HorizontalLine
以下代码所示的方法进行可视化。其余代码保持完全相同。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 号
18
19 号
20
21
22
23
24
25
26
27
28
|
# Create an empty Line Chart Line_Chart = pygal.HorizontalLine() # Set title of the Line Chart Line_Chart.title = 'A Horizontal Line Chart using Pygal' # Set x labels/values Line_Chart.x_labels = map ( str , range ( 2000 , 2022 )) # Adding Line Chart for each language popularity over the years Line_Chart.add( 'Python' , [ None , None , 0 , 16.1 , 22 , 30 , 36.9 , 45.1 , 46.1 , 42.33 , 37.11 , 38.5 , 50.78 , 53.62 , 55.37 , 60.22 , 60.5 , 61.68 , 68.65 , 79.83 , 80.25 , 85.44 ]) Line_Chart.add( 'Kotlin' , [ None , None , None , None , None , None , 0 , 3.5 , 10.1 , 23.1 , 35.2 , 24.03 , 46.53 , 46.74 , 47.31 , 59.93 , 16.11 , 22.94 , 23.56 , 1.86 , 16.07 ]) Line_Chart.add( 'C++' , [ 0.99 , 7.15 , 8.39 , 9.78 , 14.0 , 25.66 , 33.3 , 35.8 , 40.11 , 54.92 , 57.17 , 60.14 , 68.63 , 73.11 , 75.99 , 79.37 , 89.73 , 90.82 , 95.71 , 60.66 , 61.43 , 64.94 ]) Line_Chart.add( 'Java' , [ 14.1 , 15.8 , 15.1 , 8.8 , 8 , 10.1 , 18.4 , 15.1 , 16.6 , 16.1 , 17.4 , 19.27 , 21.24 , 29.29 , 38.04 , 43.24 , 54.0 , 62.55 , 11.28 , 12.08 , 13.19 , 29.35 ]) # Save the plot in svg format Line_Chart.render_to_file( 'Horizontal_Line_Chart_Demo.svg' ) |
我们还可以Stacked Line Chart
借助StackedLine
以下代码所示的方法进行可视化。我们可以传递一个属性fill
来True
填充线图并发挥创意!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 号
18
19 号
20
21
22
23
24
25
26
27
28
|
# Create an empty Line Chart Line_Chart = pygal.StackedLine(fill = True ) # Set title of the Line Chart Line_Chart.title = 'A Stacked Line Chart (Filled) using Pygal' # Set x labels/values Line_Chart.x_labels = map ( str , range ( 2000 , 2022 )) # Adding Line Chart for each language popularity over the years Line_Chart.add( 'Python' , [ None , None , 0 , 16.1 , 22 , 30 , 36.9 , 45.1 , 46.1 , 42.33 , 37.11 , 38.5 , 50.78 , 53.62 , 55.37 , 60.22 , 60.5 , 61.68 , 68.65 , 79.83 , 80.25 , 85.44 ]) Line_Chart.add( 'Kotlin' , [ None , None , None , None , None , None , 0 , 3.5 , 10.1 , 23.1 , 35.2 , 24.03 , 46.53 , 46.74 , 47.31 , 59.93 , 16.11 , 22.94 , 23.56 , 1.86 , 16.07 ]) Line_Chart.add( 'C++' , [ 0.99 , 7.15 , 8.39 , 9.78 , 14.0 , 25.66 , 33.3 , 35.8 , 40.11 , 54.92 , 57.17 , 60.14 , 68.63 , 73.11 , 75.99 , 79.37 , 89.73 , 90.82 , 95.71 , 60.66 , 61.43 , 64.94 ]) Line_Chart.add( 'Java' , [ 14.1 , 15.8 , 15.1 , 8.8 , 8 , 10.1 , 18.4 , 15.1 , 16.6 , 16.1 , 17.4 , 19.27 , 21.24 , 29.29 , 38.04 , 43.24 , 54.0 , 62.55 , 11.28 , 12.08 , 13.19 , 29.35 ]) # Save the plot in svg format Line_Chart.render_to_file( 'Stacked_Filled_Line_Chart_Demo.svg' ) |
我希望您通过本教程学到一些新的、有趣的东西。
感谢您的阅读!
我也向您推荐这些教程: