Pygal 是一个 Python 开发的动态 SVG 图表库:介绍和数据可视化
Pygal 是一个 Python 开发的动态 SVG 图表库:介绍和数据可视化
=====
1.1 Pygal 是另一个简单易用的数据图库,它以面向对象的方式来创建各种数据图。
1.2 Pygal 可以非常方便地生成各种格式的数据图,包括 PNG、SVG 等。
1.3 Pygal 也可以生成 XML etree、HTML 表格(这些都需要安装其他包)。
1.4 优点:支持中文,自动配色且优美,交互性强,代码简单。
2 准备:
=====
2.1 环境:
华为笔记本电脑、深度deepin-linux操作系统、python3.8和微软vscode编辑器。
2.2 安装:
1 2 3 |
<span class="hljs-selector-tag">pip</span> <span class="hljs-selector-tag">install</span> <span class="hljs-selector-tag">pygal</span> #本机安装<span class="hljs-selector-id">#sudo</span> <span class="hljs-selector-tag">pip3</span><span class="hljs-selector-class">.8</span> <span class="hljs-selector-tag">install</span> <span class="hljs-selector-tag">pygal</span> #没反应 #推荐国内源安装<span class="hljs-selector-id">#sudo</span> <span class="hljs-selector-tag">pip3</span><span class="hljs-selector-class">.8</span> <span class="hljs-selector-tag">install</span> <span class="hljs-selector-tag">-i</span> <span class="hljs-selector-tag">https</span>:<span class="hljs-comment">//mirrors.aliyun.com/pypi/simple pygal #超快</span> |
1 2 3 |
<span class="hljs-selector-id">#Python</span>框架<span class="hljs-selector-tag">-pygal</span>之世界地图 <span class="hljs-selector-tag">pip</span> <span class="hljs-selector-tag">install</span> <span class="hljs-selector-tag">pygal_maps_world</span> #本机安装,推荐国内源安装<span class="hljs-selector-tag">sudo</span> <span class="hljs-selector-tag">pip3</span><span class="hljs-selector-class">.8</span> <span class="hljs-selector-tag">install</span> <span class="hljs-selector-tag">-i</span> <span class="hljs-selector-tag">https</span>:<span class="hljs-comment">//mirrors.aliyun.com/pypi/simple pygal_maps_world</span> |
2.3 官网地址:
1 2 3 |
<span class="hljs-symbol">http:</span>/<span class="hljs-regexp">/www.pygal.org/en</span><span class="hljs-regexp">/stable/documentation</span><span class="hljs-regexp">/first_steps.html</span> <span class="hljs-regexp">http:/</span><span class="hljs-regexp">/www.pygal.org/en</span><span class="hljs-regexp">/stable/documentation</span><span class="hljs-regexp">/types/index</span>.html<span class="hljs-comment">#chart-types</span> <span class="hljs-comment">https://github.com/Kozea/pygal</span> |
3 数据可视化:
==========
3.1 饼图
3.1.1 代码:
1 2 3 4 5 6 7 8 9 |
<span class="hljs-keyword">import</span> pygal # 数据,形式字典<span class="hljs-keyword">data</span> = {<span class="hljs-string">'苹果'</span>:<span class="hljs-number">0.16881</span>, <span class="hljs-string">'花生'</span>:<span class="hljs-number">0.14996</span>, <span class="hljs-string">'梨子'</span>:<span class="hljs-number">0.07471</span>, <span class="hljs-string">'桃子'</span>:<span class="hljs-number">0.06992</span>, <span class="hljs-string">'西瓜'</span>:<span class="hljs-number">0.04762</span>, <span class="hljs-string">'芒果'</span>:<span class="hljs-number">0.03541</span>, <span class="hljs-string">'桔子'</span>:<span class="hljs-number">0.02925</span>, <span class="hljs-string">'杨梅'</span>:<span class="hljs-number">0.02411</span>, <span class="hljs-string">'香蕉'</span>:<span class="hljs-number">0.06316</span>, <span class="hljs-string">'哈密瓜'</span>:<span class="hljs-number">0.01409</span>, <span class="hljs-string">'樱桃'</span>:<span class="hljs-number">0.36326</span>} # 创建图(饼图)graph = pygal.Pie() # 从字典中添加数据<span class="hljs-keyword">for</span> k <span class="hljs-keyword">in</span> <span class="hljs-keyword">data</span>.keys(): graph.add(k, <span class="hljs-keyword">data</span>[k]) # 设置图标题graph.title = <span class="hljs-string">'2020年5月水果销售情况'</span> # 设置图例位置(下面)graph.legend_at_bottom = True # 指定路径和目录保存,文件名为:fruitpie.svggraph.render_to_file(<span class="hljs-string">'/home/xgj/Desktop/pygal/fruitpie.svg'</span>) |
3.1.2 具体操作步骤和效果图:
3.2 空心饼图:
3.2.1 代码:
1 2 3 4 5 6 7 8 9 10 11 |
<span class="hljs-keyword">import</span> pygal # 数据,形式字典<span class="hljs-keyword">data</span> = {<span class="hljs-string">'苹果'</span>:<span class="hljs-number">0.16881</span>, <span class="hljs-string">'花生'</span>:<span class="hljs-number">0.14996</span>, <span class="hljs-string">'梨子'</span>:<span class="hljs-number">0.07471</span>, <span class="hljs-string">'桃子'</span>:<span class="hljs-number">0.06992</span>, <span class="hljs-string">'西瓜'</span>:<span class="hljs-number">0.04762</span>, <span class="hljs-string">'芒果'</span>:<span class="hljs-number">0.03541</span>, <span class="hljs-string">'桔子'</span>:<span class="hljs-number">0.02925</span>, <span class="hljs-string">'杨梅'</span>:<span class="hljs-number">0.02411</span>, <span class="hljs-string">'香蕉'</span>:<span class="hljs-number">0.06316</span>, <span class="hljs-string">'哈密瓜'</span>:<span class="hljs-number">0.01409</span>, <span class="hljs-string">'樱桃'</span>:<span class="hljs-number">0.36326</span>} graph = pygal.Pie()<span class="hljs-keyword">for</span> k <span class="hljs-keyword">in</span> <span class="hljs-keyword">data</span>.keys(): graph.add(k, <span class="hljs-keyword">data</span>[k]) graph.title = <span class="hljs-string">'2020年5月水果销售情况'</span> graph.legend_at_bottom = True#空心饼图,增加这一行,空心内圆半径比值<span class="hljs-number">0.5</span> graph.inner_radius = <span class="hljs-number">0.5</span> graph.render_to_file(<span class="hljs-string">'/home/xgj/Desktop/pygal/fruit-inner-pie.svg'</span>) |
3.2.2 效果图:
3.3 半圆形:
3.3.1 代码:
1 2 3 4 5 6 7 8 9 |
<span class="hljs-keyword">import</span> pygal # 数据,形式字典<span class="hljs-keyword">data</span> = {<span class="hljs-string">'苹果'</span>:<span class="hljs-number">0.16881</span>, <span class="hljs-string">'花生'</span>:<span class="hljs-number">0.14996</span>, <span class="hljs-string">'梨子'</span>:<span class="hljs-number">0.07471</span>, <span class="hljs-string">'桃子'</span>:<span class="hljs-number">0.06992</span>, <span class="hljs-string">'西瓜'</span>:<span class="hljs-number">0.04762</span>, <span class="hljs-string">'芒果'</span>:<span class="hljs-number">0.03541</span>, <span class="hljs-string">'桔子'</span>:<span class="hljs-number">0.02925</span>, <span class="hljs-string">'杨梅'</span>:<span class="hljs-number">0.02411</span>, <span class="hljs-string">'香蕉'</span>:<span class="hljs-number">0.06316</span>, <span class="hljs-string">'哈密瓜'</span>:<span class="hljs-number">0.01409</span>, <span class="hljs-string">'樱桃'</span>:<span class="hljs-number">0.36326</span>} graph = pygal.Pie()<span class="hljs-keyword">for</span> k <span class="hljs-keyword">in</span> <span class="hljs-keyword">data</span>.keys(): graph.add(k, <span class="hljs-keyword">data</span>[k]) graph.title = <span class="hljs-string">'2020年5月水果销售情况'</span> graph.legend_at_bottom = True#半圆饼图:增加这一行graph.half_pie = True graph.render_to_file(<span class="hljs-string">'/home/xgj/Desktop/pygal/fruit-half-pie.svg'</span>) |
3.3.2 效果图:
=====请注意:小不规范(故意的),在python中命名,建议这样:fruit_half_pie.svg=====
3.4 柱状图:
3.4.1 代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<span class="hljs-string">''</span><span class="hljs-string">'</span> <span class="hljs-string">#方法一:</span> <span class="hljs-string">import pygal </span> <span class="hljs-string">bar_chart = pygal.Bar() </span> <span class="hljs-string">bar_chart.add('</span><span class="hljs-string">Firsrt组',</span> <span class="hljs-string">[0,</span> <span class="hljs-number">1</span><span class="hljs-string">,</span> <span class="hljs-number">1</span><span class="hljs-string">,</span> <span class="hljs-number">2</span><span class="hljs-string">,</span> <span class="hljs-number">3</span><span class="hljs-string">,</span> <span class="hljs-number">5</span><span class="hljs-string">,</span> <span class="hljs-number">8</span><span class="hljs-string">,</span> <span class="hljs-number">13</span><span class="hljs-string">])</span> <span class="hljs-string">bar_chart.add('Second组',</span> <span class="hljs-string">[55,</span> <span class="hljs-number">34</span><span class="hljs-string">,</span> <span class="hljs-number">21</span><span class="hljs-string">,</span> <span class="hljs-number">13</span><span class="hljs-string">,</span> <span class="hljs-number">8</span><span class="hljs-string">,</span> <span class="hljs-number">5</span><span class="hljs-string">,</span> <span class="hljs-number">3</span><span class="hljs-string">,</span> <span class="hljs-number">2</span><span class="hljs-string">])</span> <span class="hljs-string">bar_chart.render_to_file('/home/xgj/Desktop/pygal/bar_chart.svg')</span> <span class="hljs-string">''</span><span class="hljs-string">'</span> <span class="hljs-string">#方法二:</span> <span class="hljs-string">import pygal </span> <span class="hljs-string">bar_chart = pygal.Bar() </span> <span class="hljs-string">#数据</span> <span class="hljs-string">data0=[0, 1, 1, 2, 3, 5, 8, 13]</span> <span class="hljs-string">data1=[55, 34, 21, 13, 8, 5, 3, 2]</span> <span class="hljs-string">labels=['</span><span class="hljs-string">Firsrt组','Second组']</span> <span class="hljs-string">#第一条bar</span> <span class="hljs-string">bar_chart.add(labels[0],</span> <span class="hljs-string">data0)</span> <span class="hljs-comment">#第二条bar </span> <span class="hljs-comment">bar_chart.add(labels[1], data1)</span> <span class="hljs-comment">#当然,也可以第n条bar......</span> <span class="hljs-comment">#默认左上角,也可自定义如下</span> <span class="hljs-comment">#bar_chart.legend_at_bottom = True</span> <span class="hljs-comment">bar_chart.render_to_file('/home/xgj/Desktop/pygal/bar_chart.svg') </span> |
3.4.2 以上故意中英文定义:看看中文支持否;数据两种布局,主要是为了今后用python从外部读取数据做准备。
3.4.3 效果图:
3.5 stackbar叠柱图:
3.5.1 代码:
1 2 3 4 5 6 7 8 9 10 11 12 |
<span class="hljs-keyword">import</span> pygal # 数据读取年的数据,并字符串化year_data = [str(i) <span class="hljs-keyword">for</span> i in <span class="hljs-keyword">range</span>(<span class="hljs-number">2011</span>, <span class="hljs-number">2020</span>)] #数据banana_data = [<span class="hljs-number">3500</span>,<span class="hljs-number">4300</span>,<span class="hljs-number">7500</span>,<span class="hljs-number">5500</span>,<span class="hljs-number">3200</span>,<span class="hljs-number">2100</span>,<span class="hljs-number">4700</span>,<span class="hljs-number">6100</span>,<span class="hljs-number">3900</span>] apple_data =[<span class="hljs-number">3100</span>,<span class="hljs-number">3400</span>,<span class="hljs-number">5100</span>,<span class="hljs-number">2700</span>,<span class="hljs-number">3800</span>,<span class="hljs-number">2600</span>,<span class="hljs-number">3200</span>,<span class="hljs-number">4200</span>,<span class="hljs-number">4700</span>] # 创建图(叠加柱状图)garph = pygal.StackedBar() #garph = pygal.Dot() # 创建图(点图)#garph = pygal.StackedLine() # 创建图(叠加折线图)# 添加数据garph.add(<span class="hljs-string">'香蕉的历年销量'</span>, banana_data) garph.add(<span class="hljs-string">'苹果的历年销量'</span>, apple_data) garph.x_labels = year_data # 设置 X 轴刻度garph.y_label_rotation = <span class="hljs-number">45</span> # 设置Y轴的标签旋转多少度 garph.title = <span class="hljs-string">'香蕉与苹果历年的销量分析'</span> # 设置图标题 garph.x_title = <span class="hljs-string">'年份'</span> # 设置 X 轴标题 garph.y_title = <span class="hljs-string">'销量(吨)'</span> # 设置 Y 轴标题 garph.legend_at_bottom = True # 设置图例位置(下面)# 设置页边距(margin_bottom、margin_top、margin_left、margin_right)garph.margin = <span class="hljs-number">35</span> garph.show_x_guides = True # 显示X轴的网格线garph.show_y_guides = True # 显示Y轴的网格线# 输出到图片文件garph.render_to_file(<span class="hljs-string">'/home/xgj/Desktop/pygal/stackbar.svg'</span>) |
3.5.2 效果图:
3.5 单个直方图:
3.5.1 代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<span class="hljs-keyword">import</span> pygal #直方图<span class="hljs-keyword">data</span> = [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>,<span class="hljs-number">6</span>,<span class="hljs-number">1</span>,<span class="hljs-number">1</span>,<span class="hljs-number">1</span>,<span class="hljs-number">1</span>,<span class="hljs-number">1</span>] #Python <span class="hljs-keyword">set</span>() 函数# 重复的被删除 x_value = list(<span class="hljs-keyword">set</span>(<span class="hljs-keyword">data</span>)) #统计freq = []<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> x_value: #<span class="hljs-keyword">data</span>.count(i) #计算<span class="hljs-keyword">data</span>中某个数,比如<span class="hljs-number">1</span>,有几个=<span class="hljs-number">8</span>个 res = <span class="hljs-keyword">data</span>.count(i) #append添加到列表中 freq.append(res)#可视化hist = pygal.Bar()#标题hist.title = <span class="hljs-string">"直方图"</span> hist._x_title = <span class="hljs-string">'x坐标'</span> hist._y_title = <span class="hljs-string">'y坐标'</span> hist.x_labels = x_valuehist.add(<span class="hljs-string">'人数'</span>,freq) #添加到图表 #图表渲染为svg,用浏览器打开svg文件hist.render_to_file(<span class="hljs-string">"/home/xgj/Desktop/pygal/a.svg"</span>) |
3.5.2 复习python的基础知识:注释里有。
3.5.3 效果图:
3.6 自定义直方图:
3.6.1 代码:
1 2 3 4 |
<span class="hljs-keyword">import</span> pygal hist = pygal.Histogram()#(y轴高度,x轴开始坐标,x轴结束坐标)hist.add(<span class="hljs-string">'人数'</span>,[(<span class="hljs-number">4</span>,<span class="hljs-number">1</span>,<span class="hljs-number">1.5</span>),(<span class="hljs-number">5</span>,<span class="hljs-number">2</span>,<span class="hljs-number">2.5</span>),(<span class="hljs-number">6</span>,<span class="hljs-number">3</span>,<span class="hljs-number">3.5</span>)]) #标题hist.title = <span class="hljs-string">"自定义直方图"</span> hist.render_to_file(<span class="hljs-string">"/home/xgj/Desktop/pygal/a1.svg"</span>) |
3.6.2 效果图:
4 浏览器自动打开数据可视化:
======================
4.1 提前安装:lxml
1 2 3 |
#安装lxml: pip install lxml#本机安装:sudo pip3<span class="hljs-number">.8</span> install lxml #很慢 <span class="hljs-meta">#sudo pip3.8 install -i https:<span class="hljs-comment">//mirrors.aliyun.com/pypi/simple lxml #超快</span></span> |
4.2 雷达图的示例的代码:
1 2 3 4 5 6 7 8 9 10 11 |
<span class="hljs-keyword">import</span> pygal #数据pythond = [<span class="hljs-number">48</span>,<span class="hljs-number">60</span>,<span class="hljs-number">60</span>,<span class="hljs-number">34</span>,<span class="hljs-number">30</span>] JavaScriptd = [<span class="hljs-number">41</span>,<span class="hljs-number">56</span>,<span class="hljs-number">73</span>,<span class="hljs-number">32</span>,<span class="hljs-number">30</span>] cd = [<span class="hljs-number">55</span>,<span class="hljs-number">70</span>,<span class="hljs-number">54</span>,<span class="hljs-number">39</span>,<span class="hljs-number">50</span>] #标签labels = [<span class="hljs-string">'下载量'</span>,<span class="hljs-string">'提交量'</span>,<span class="hljs-string">'薪酬'</span>,<span class="hljs-string">'人数'</span>,<span class="hljs-string">'普及'</span>] #雷达图设计(Radar:雷达、Bar:柱状、Line:线状),对应修改即可rader_chart = pygal.Radar()rader_chart.title = <span class="hljs-string">"3组计算机语言的数据比较"</span> #添加各个顶点含义rader_chart.x_labels = labelsrader_chart.add(<span class="hljs-string">"python"</span>,pythond) rader_chart.add(<span class="hljs-string">"JavaScript"</span>,JavaScriptd) rader_chart.add(<span class="hljs-string">"C"</span>,cd) #将数据渲染到浏览器;这是启动自动浏览器打开才有这样的#文件默认保存地址和名称:file:<span class="hljs-comment">///tmp/tmp5ehvol2e.html,随机文件名</span> <span class="hljs-comment">rader_chart.render_in_browser() #如果没有安装,就报错,安装lxml就可以了</span> |
4.3 操作步骤和效果图:
5 世界地图绘制:
============
5.1 代码来源:
1 |
<span class="hljs-meta">#https:<span class="hljs-comment">//www.cnblogs.com/keqipu/p/7283991.html</span></span> |
5.2 代码:
1 2 3 4 5 6 7 8 9 |
import pygal.maps.world worldmap_chart = pygal.maps.world.World()worldmap_chart.title = <span class="hljs-string">'Some countries'</span> worldmap_chart.<span class="hljs-keyword">add</span>(<span class="hljs-string">'F countries'</span>, [<span class="hljs-string">'fr'</span>, <span class="hljs-string">'fi'</span>]) worldmap_chart.<span class="hljs-keyword">add</span>(<span class="hljs-string">'M countries'</span>, [<span class="hljs-string">'ma'</span>, <span class="hljs-string">'mc'</span>, <span class="hljs-string">'md'</span>, <span class="hljs-string">'me'</span>, <span class="hljs-string">'mg'</span>, <span class="hljs-string">'mk'</span>, <span class="hljs-string">'ml'</span>, <span class="hljs-string">'mm'</span>, <span class="hljs-string">'mn'</span>, <span class="hljs-string">'mo'</span>, <span class="hljs-string">'mr'</span>, <span class="hljs-string">'mt'</span>, <span class="hljs-string">'mu'</span>, <span class="hljs-string">'mv'</span>, <span class="hljs-string">'mw'</span>, <span class="hljs-string">'mx'</span>, <span class="hljs-string">'my'</span>, <span class="hljs-string">'mz'</span>]) worldmap_chart.<span class="hljs-keyword">add</span>(<span class="hljs-string">'U countries'</span>, [<span class="hljs-string">'ua'</span>, <span class="hljs-string">'ug'</span>, <span class="hljs-string">'us'</span>, <span class="hljs-string">'uy'</span>, <span class="hljs-string">'uz'</span>]) worldmap_chart.render_to_file(<span class="hljs-string">'/home/xgj/Desktop/pygal/world-map.svg'</span>) |
5.3 效果图:
===非常不错===
自己整理并分享出来,喜欢的就点赞、转发、评论和收藏。
https://m.toutiaocdn.com/i6837777192449999364/?app=news_article×tamp=1592087382&use_new_style=0&req_id=2020061406294101001404701227C849A7&group_id=6837777192449999364&tt_from=android_share&utm_medium=toutiao_android&utm_campaign=client_share