It's awesome! Easy visualization with 5 lines of code

Sometimes we do not need particularly complex functions for data visualization, we just want to show simple data graphically

Today, I will introduce to you a python visualization library that is very suitable for novices-pygal

image

Pygal is relatively small, focusing on SVG graphics, good at interaction, the most important thing is that it can draw very beautiful graphics with very little code

pygal can draw 14 common visualizations such as Line, Bar, Histogram, Pie, Radar, Funnel, Gauge, etc. Figure

And it comes with 16 beautiful themes, the tones are quite soft, it feels very suitable for a delicate and gentle person like me

The installation of pygal is relatively simple, just enter the following code directly on the command line:

pip install pygal

Below, we use the monthly living expenses of a dormitory in 2020 as an example to introduce the pygal drawing method

First, pandas reads the data:

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

image

I want to display pygal graphics directly on jupyter. I need to create a basic html template. You can use it directly:

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>
"""

Enter the topic below:

1. pygal draws a line chart (theme: 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)))#图片渲染

image


Many people learn python and don't know where to start.
Many people learn python and after mastering the basic grammar, they don't know where to find cases to get started.
Many people who have done case studies do not know how to learn more advanced knowledge.
For these three types of people, I will provide you with a good learning platform, free to receive video tutorials, e-books, and course source code!
QQ group: 721195303


2. pygal draws histogram (themes: DarkStyle, NeonStyle)

Draw a vertical bar chart

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)))#图片渲染

image

Draw a horizontal histogram

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)))#图片渲染

image

3. pygal draws a pie chart (theme: DarkSolarizedStyle)

Ordinary pie chart

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)))#图片渲染

image

Doughnut chart

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)))#图片渲染

 

image

4. Pygal draws radar chart (theme: 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)))#图片渲染

 

image

5. pygal draws a box plot (theme: 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)))#图片渲染

image

 

6. Pygal draws a scatter chart (theme: 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)))#图片渲染

image

7. pygal draws a funnel chart (theme: 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)))#图片渲染

 

 

image

8. pygal draws dashboard diagram (theme: 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 = [0, 5000]
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)))#图片渲染

image

Careful readers can find that pygal's graphics drawing routines are basically the same, and its core code is only 5 lines of code, which can be said to be an invincible one.

I still want to recommend the Python learning group I built myself : 721195303 , all of whom are learning Python. If you want to learn or are learning Python, you are welcome to join. Everyone is a software development party and share dry goods from time to time (only Python software development related), including a copy of the latest Python advanced materials and zero-based teaching compiled by myself in 2021. Welcome friends who are in advanced and interested in Python to join!

Guess you like

Origin blog.csdn.net/aaahtml/article/details/113238662