5行代码搞定14种常见图的python可视化库,还自带16种优美主题,真是太喜欢了

Python与算法之美

共 5985字,需浏览 12分钟

 · 2021-07-10

有时候我们做数据可视化并不需要特别复杂的功能,仅仅是想把简单的数据用图形展示出来

今天就给大家介绍一种非常适合新手的python可视化库--pygal

pygal比较小众,专注于SVG图,擅长交互,最主要的是它能用非常少的代码就可画出非常漂亮的图形

pygal能绘制Line(折线图)、Bar(柱状图)、Histogram(直方图)、Pie(饼图)、Radar(雷达图)、Funnel(漏斗图)、Gauge(仪表盘图)等14种常见可视化图

而且自带16种漂亮的主题,色调相当柔和,感觉很适合我这样细腻温柔的人

pygal的安装比较简单,直接在命令行输入以下代码即可:

pip install pygal

下面,我们依托某宿舍2020年各月生活费花销情况为例,对pygal绘制方法进行介绍

首先pandas读取数据:

import pandas as pd
data=pd.read_excel('生活费开销.xlsx')

我想在jupyter上直接显示pygal的图形,需要创建html的基础模板,你们可以拿去直接用:

import pygal
#设置pygal与jupyter notebook交互
from IPython.display import display, HTML
base_html = """
<!DOCTYPE html>
<html>
  <head>
  <script type="text/javascript" src="http://kozea.github.com/pygal.js/javascripts/svg.jquery.js"></script>
  <script type="text/javascript" src="https://kozea.github.io/pygal.js/2.0.x/pygal-tooltips.min.js""></script>
  </head>
  <body>
    <figure>
      {rendered_chart}
    </figure>
  </body>
</html>
"""

下面进入主题:

1.pygal绘制折线图(主题:DefaultStyle)

from pygal.style import *
people=data['人员'].unique()
label=data['月份'].unique()
line_chart = pygal.Line(style=DefaultStyle)
line_chart.title = '520寝室2020年生活费花销情况'
line_chart.x_labels = label
for i in people:
    line_chart.add(i, data[data.人员==i]['花销'].values.tolist())
HTML(base_html.format(rendered_chart=line_chart.render(is_unicode=True)))#图片渲染

2.pygal绘制柱状图(主题:DarkStyle、NeonStyle)

绘制竖状柱状图

from pygal.style import *
people=data['人员'].unique()
label=data['月份'].unique()
line_chart = pygal.Bar(style=DarkStyle)
line_chart.title = '520寝室2020年生活费花销情况'
line_chart.x_labels = label
for i in people:
    line_chart.add(i, data[data.人员==i]['花销'].values.tolist())
HTML(base_html.format(rendered_chart=line_chart.render(is_unicode=True)))#图片渲染

绘制横状柱状图

from pygal.style import *
people=data['人员'].unique()
label=data['月份'].unique()
line_chart = pygal.HorizontalBar(style=NeonStyle)
line_chart.title = '520寝室2020年生活费花销情况'
line_chart.x_labels = label
for i in people:
    line_chart.add(i, data[data.人员==i]['花销'].values.tolist())
HTML(base_html.format(rendered_chart=line_chart.render(is_unicode=True)))#图片渲染

3.pygal绘制饼图(主题:DarkSolarizedStyle)

普通饼状图

from pygal.style import *
people=data['人员'].unique()
label=data['月份'].unique()
line_chart = pygal.Pie(style=DarkSolarizedStyle)
line_chart.title = '520寝室2020年1月生活费花销情况'
line_chart.x_labels = label
for i in people:
    line_chart.add(i, data[(data.人员==i)&(data.月份=='1月')]['花销'].values.tolist())
HTML(base_html.format(rendered_chart=line_chart.render(is_unicode=True)))#图片渲染

圆环图

from pygal.style import *
people=data['人员'].unique()
label=data['月份'].unique()
pie_chart = pygal.Pie(inner_radius=0.45,style=LightSolarizedStyle)
pie_chart.title = '520寝室2020年1月生活费花销情况'
for i in people:
    pie_chart.add(i, data[(data.人员==i)&(data.月份=='1月')]['花销'].values.tolist()[0])
HTML(base_html.format(rendered_chart=pie_chart.render(is_unicode=True)))#图片渲染


4.pygal绘制雷达图(主题:LightStyle)

from pygal.style import *
people=data['人员'].unique()
label=data['月份'].unique()
radar_chart = pygal.Radar(style=LightStyle)
radar_chart.title = '520寝室2020年生活费花销情况'
radar_chart.x_labels = label
for i in people:
    radar_chart.add(i, data[data.人员==i]['花销'].values.tolist())
HTML(base_html.format(rendered_chart=radar_chart.render(is_unicode=True)))#图片渲染

5.pygal绘制箱形图(主题:CleanStyle)

from pygal.style import *
people=data['人员'].unique()
label=data['月份'].unique()
box_plot = pygal.Box(style=CleanStyle)
box_plot.title = '520寝室2020年生活费花销情况'
for i in people:
    box_plot.add(i, data[data.人员==i]['花销'].values.tolist())
HTML(base_html.format(rendered_chart=box_plot.render(is_unicode=True)))#图片渲染


6.pygal绘制散点图(主题:RedBlueStyle)

from pygal.style import *
people=data['人员'].unique()
label=data['月份'].unique()
dot_chart = pygal.Dot(x_label_rotation=30,style=RedBlueStyle)
dot_chart.title = '520寝室2020年生活费花销情况'
dot_chart.x_labels=label
for i in people:
    dot_chart.add(i, data[data.人员==i]['花销'].values.tolist())
HTML(base_html.format(rendered_chart=dot_chart.render(is_unicode=True)))#图片渲染

7.pygal绘制漏斗图(主题:DarkColorizedStyle)

from pygal.style import *
people=data['人员'].unique()
label=data['月份'].unique()
funnel_chart = pygal.Funnel(style=DarkColorizedStyle)
funnel_chart.title = '520寝室2020年生活费花销情况'
funnel_chart.x_labels=label
for i in people:
    funnel_chart.add(i, data[data.人员==i]['花销'].values.tolist())
HTML(base_html.format(rendered_chart=funnel_chart.render(is_unicode=True)))#图片渲染


8.pygal绘制仪表盘图(主题:LightColorizedStyle)

from pygal.style import *
people=data['人员'].unique()
label=data['月份'].unique()
gauge_chart = pygal.Gauge(human_readable=True,style=LightColorizedStyle)
gauge_chart.title = '520寝室2020年1月生活费花销情况'
gauge_chart.range = [05000]
for i in people:
    gauge_chart.add(i, data[(data.人员==i)&(data.月份=='1月')]['花销'].values.tolist())
HTML(base_html.format(rendered_chart=gauge_chart.render(is_unicode=True)))#图片渲染

细心的读者可以发现,pygal绘制图形的套路基本都一样,其核心的代码也就5行代码,可以说是一招打遍天下无敌手。

文中代码、数据以及pygal学习教程已打包完毕,请扫描下方二维码在后台回复“pygal”获取

浏览 18
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报