pyexcel-pygal - Let you focus on data, instead of file formats

Author:C.W.
Source code:http://github.com/pyexcel/pyexcel-pygal.git
Issues:http://github.com/pyexcel/pyexcel-pygal/issues
License:New BSD License
Released:0.0.1
Generated:Aug 20, 2017

Introduction

pyexcel-pygal is a tiny plugin of pyexcel to turn pyexcel data into chart graphics using pygal.

To see pyexcel-pygal in action with Jupyter notebook, please checkout the resource on github folder

Installation

You can install it via pip:

$ pip install pyexcel-pygal

or clone it and install it:

$ git clone https://github.com/pyexcel/pyexcel-pygal.git
$ cd pyexcel-pygal
$ python setup.py install

Content

Plot pyexcel data in Jupyter Notebook

There are currently four type of data layouts for rendering charts.

1 Simple Layout

Series names are placed in the first row. The rest of the rows are data sets.

Pie chart
Browser usage in February 2012 (in %)19.5283.114871902105.73028135136.6290.681741008222.11967194736.3164.453391678167.7139731944.5218.2410343994.9492048062.3234.13096221992.0492686777Browser usage in February 2012 (in %)IEFirefoxChromeSafariOpera
title = 'Browser usage in February 2012 (in %)'
sheet = pyexcel.get_sheet(file_name='pie.csv')
svg = sheet.plot(chart_type='pie',
     title=title, width=600, height=400, explicit_size=True)
Box chart

Here is the source code using pyexcel:

V8 benchmark results00100010002000200030003000400040005000500060006000700070008000800090009000100001000011000110001200012000Min: 1660 Q1 : 4259 Q2 : 7369 Q3 : 8409.5 Max: 1246461.2346153846151.532360892Min: 1044 Q1 : 3224 Q2 : 6917 Q3 : 8774.5 Max: 11700166.811538462164.425925126Min: 34 Q1 : 42 Q2 : 69 Q3 : 119 Max: 144272.388461538325.474375432Min: 1828 Q1 : 3202.5 Q2 : 4436 Q3 : 5519.5 Max: 9013377.965384615203.90280315V8 benchmark resultsChromeFirefoxIEOpera
title = 'V8 benchmark results'
sheet = pyexcel.get_sheet(file_name='box.csv')
svg = sheet.plot(chart_type='box',
     title=title, width=600, height=400, explicit_size=True)

2 Complex layout

On top of previous layout, x labels were inserted as the first column. In other words, each column represents series data and the first column contains x labels. y labels locate in the first row

Line

Here is the source code using pyexcel:

Browser usage evolution (in %)001010202030304040505060607070808020022003200420052006200720082009201020112012Browser usage evolution (in %)08.79230769231307.96153846220023.952.7538461538294.237762238200310.896.7153846154269.957235073200423.8140.676923077224.211314327200535.3184.638461538183.743769051200608.79230769231307.961538462200216.652.7538461538249.54751658620032596.7153846154219.98861395200431140.676923077198.875112067200536.4184.638461538179.872960373200645.5228.6147.850815851200746.3272.561538462145.035682266200842.8316.523076923157.351891698200937.1360.484615385177.409718487201085.88.792307692316.03846153846200284.652.753846153810.261161915200384.796.71538461549.90927021696200474.5140.67692307745.8022234176200566184.63846153875.7130177515200658.6228.6101.753003407200754.7272.561538462115.476779631200844.8316.523076923150.314057737200936.2360.484615385180.576743769201026.6404.446153846214.358346781201120.1448.407692308237.231307154201214.28.79230769231257.992917339200215.452.7538461538253.770216963200315.396.7153846154254.12210866120048.9140.676923077276.64317733520059184.638461538276.291285637200610.4228.6271.36480186520078.9272.561538462276.64317733520085.8316.523076923287.55181997520096.7360.484615385284.38479469220106.8404.446153846284.03290299420117.5448.407692308281.5696611082012ChromeFirefoxIEOthers
title = 'Browser usage evolution (in %)'
sheet = pyexcel.get_sheet(file_name='line.csv')
svg = sheet.plot(chart_type='line',
    title=title, width=600, height=400, explicit_size=True)
Dot chart

Here is the source code using pyexcel:

V8 benchmark resultsChrome3.5Firefox2.5IE1.5Opera0.5RichardsDeltaBl…DeltaBlueCryptoRayTraceEarleyB…EarleyBoyerRegExpSplayNavierS…NavierStokes639533.888461538543.7788461538Richards821285.234615384643.7788461538DeltaBlue7520136.58076923143.7788461538Crypto7218187.92692307743.7788461538RayTrace12464239.27307692343.7788461538EarleyBoyer1660290.61923076943.7788461538RegExp2123341.96538461543.7788461538Splay8607393.31153846243.7788461538NavierStokes747333.8884615385119.259615385Richards809985.2346153846119.259615385DeltaBlue11700136.580769231119.259615385Crypto2651187.926923077119.259615385RayTrace6361239.273076923119.259615385EarleyBoyer1044290.619230769119.259615385RegExp3797341.965384615119.259615385Splay9450393.311538462119.259615385NavierStokes4333.8884615385194.740384615Richards4185.2346153846194.740384615DeltaBlue59136.580769231194.740384615Crypto79187.926923077194.740384615RayTrace144239.273076923194.740384615EarleyBoyer136290.619230769194.740384615RegExp34341.965384615194.740384615Splay102393.311538462194.740384615NavierStokes347233.8884615385270.221153846Richards293385.2346153846270.221153846DeltaBlue4203136.580769231270.221153846Crypto5229187.926923077270.221153846RayTrace5810239.273076923270.221153846EarleyBoyer1828290.619230769270.221153846RegExp9013341.965384615270.221153846Splay4669393.311538462270.221153846NavierStokesV8 benchmark resultsChromeFirefoxIEOpera
title = 'V8 benchmark results'
sheet = pyexcel.get_sheet(file_name='radar.csv')
svg = sheet.plot(chart_type='dot',
    title=title, width=600, height=400, explicit_size=True)
Funnel chart

Here is the source code using pyexcel:

V8 benchmark results-50000-50000-40000-40000-30000-30000-20000-20000-10000-100000010000100002000020000300003000040000400005000050000RichardsDeltaBlueCrypto639577.7538461538190.287354862Richards821277.7538461538180.116063914Richards752077.7538461538169.161401821Richards721877.7538461538158.898891656Richards1246477.7538461538145.193726431Richards166077.7538461538135.358762828Richards212377.7538461538132.724546791Richards860777.7538461538125.252926892Richards7473216.6220.300566502DeltaBlue8099216.6199.286026247DeltaBlue11700216.6174.54472833DeltaBlue2651216.6154.289187673DeltaBlue6361216.6134.308697329DeltaBlue1044216.6119.317410767DeltaBlue3797216.6113.312261651DeltaBlue9450216.696.6163594773DeltaBlue3472355.446153846210.469084549Crypto2933355.446153846195.165842188Crypto4203355.446153846176.41019615Crypto5229355.446153846159.849381978Crypto5810355.446153846145.887271018Crypto1828355.446153846135.412380231Crypto9013355.446153846124.492534634Crypto4669355.446153846105.741066575CryptoV8 benchmark results ChromeFirefoxOpera
title = 'V8 benchmark results'
sheet = pyexcel.get_sheet(file_name='funnel.csv')
svg = sheet.plot(chart_type='funnel',
    title=title, width=600, height=400, explicit_size=True)
Radar chart

Here is the source code using pyexcel:

V8 benchmark results120001200010000100008000800060006000400040002000200000Richards1.570796327DeltaBlue2.35619449Crypto3.141592654RayTrace3.926990817EarleyBoyer4.71238898RegExp5.497787144Splay6.283185307NavierStokes7.068583471V8 benchmark results6395219.682.4137100033Richards8212124.87053537889.2744720146DeltaBlue752096.9213521609157.0Crypto7218136.336814949216.527869094RayTrace12464219.6302.37037037EarleyBoyer1660238.748917593170.690255292RegExp2123254.233878905157.0Splay8607318.88598416986.0168510265NavierStokes7473219.669.8407591642Richards8099126.17404603490.2063990314DeltaBlue1170028.7302952503157.0Crypto2651189.019409314178.863172758RayTrace6361219.6231.189740527EarleyBoyer1044231.643054197165.610015979RegExp3797281.542928969157.0Splay9450328.61040436979.0645105379NavierStokes43219.6156.498481553Richards41219.127044806156.661867189DeltaBlue59218.637494651157.0Crypto79218.68869609157.651524198RayTrace144219.6158.679503637EarleyBoyer136221.168826984158.121611277RegExp34220.154664099157.0Splay102220.776620238156.158791542NavierStokes3472219.6116.505301193Richards2933185.766400422132.811133271DeltaBlue4203151.03372914157.0Crypto5229159.280909583200.124304169RayTrace5810219.6224.763306471EarleyBoyer1828240.686880337172.075775104RegExp9013366.634927257157.0Splay4669273.459214603118.494095207NavierStokesChromeFirefoxIEOpera
title = 'V8 benchmark results'
sheet = pyexcel.get_sheet(file_name='radar.csv')
svg = sheet.plot(chart_type='radar',
    title=title, width=600, height=400, explicit_size=True)

Histogram

To draw a histogram, heights, starts and stops should be placed sequentially in first, second and third columns.

Here is the source code using pyexcel:

pyexcel chart rendered by pygal001122334455001122334455667788991010111112121313141415155: 0133.179487179157.04: 5233.692307692187.1923076922: 0196.0247.576923077pyexcel chart rendered by pygalhistogram_wide…histogram_wide_bars.csv
sheet = pyexcel.get_sheet(file_name='histogram_wide_bars.csv')
svg = sheet.plot(chart_type='histogram',
     width=600, height=400, explicit_size=True)

In order to draw multiple histogram on the same chart, you will need to use a Book, each sheet of which become a histogram. Here is how you can draw multiple histogram.

Here is the source code using pyexcel

pyexcel chart rendered by pygal001122334455667788991010111112120011223344556677889910101111121213131414151510: 148.8769230769182.1602564112: 4123.55157.08: 11333.992307692207.3205128215: 0143.915384615245.0608974364: 5252.530769231257.6410256412: 0211.8282.801282051pyexcel chart rendered by pygalNarrow barsWide bars
book = pyexcel.get_book(file_name='histogram.xlsx')
svg = book.plot(chart_type='histogram',
     width=600, height=400, explicit_size=True)

XY

In order to draw XY graph, x, y data should be placed vertically at first and second column. In order to draw multiple lines, their data should be placed in individual sheets.

Here is the source code using pyexcel

pyexcel chart rendered by pygal-5-5-4-4-3-3-2-2-1-1001122334455-5-5-4-4-3-3-2-2-1-1001122334455pyexcel chart rendered by pygal-1: -5174.461538462307.961538462-1: 5174.4615384626.038461538461: -5257.538461538307.9615384621: 5257.5384615386.038461538460.2836621855: -5227.782890781307.961538462-0.2107957994: -4.5207.243866793292.865384615-0.6536436209: -4188.848649595277.769230769-0.9364566873: -3.5177.101029913262.673076923-0.9899924966: -3174.877234757247.576923077-0.8011436155: -2.5182.721726739232.480769231-0.4161468365: -2198.713900636217.3846153850.07073720167: -1.5218.938314531202.2884615380.5403023059: -1238.443326551187.1923076920.8775825619: -0.5252.453429494172.0961538461: 0257.538461538157.00.8775825619: 0.5252.453429494141.9038461540.5403023059: 1238.443326551126.8076923080.07073720167: 1.5218.938314531111.711538462-0.4161468365: 2198.71390063696.6153846154-0.8011436155: 2.5182.72172673981.5192307692-0.9899924966: 3174.87723475766.4230769231-0.9364566873: 3.5177.10102991351.3269230769-0.6536436209: 4188.84864959536.2307692308-0.2107957994: 4.5207.24386679321.1346153846-5: -18.30769230769187.1923076925: -1423.692307692187.192307692-5: 18.30769230769126.8076923085: 1423.692307692126.807692308-5: 0.28366218558.30769230769148.435584016-4.5: -0.210795799429.0769230769163.364411637-4: -0.653643620949.8461538462176.735009322-3.5: -0.936456687370.6153846154185.273788443-3: -0.989992496691.3846153846186.89015807-2.5: -0.8011436155112.153846154181.188374546-2: -0.4161468365132.923076923169.564433334-1.5: 0.07073720167153.692307692154.864280642-1: 0.5403023059174.461538462140.687026534-0.5: 0.8775825619195.230769231130.5037572660: 1216.0126.8076923080.5: 0.8775825619236.769230769130.5037572661: 0.5403023059257.538461538140.6870265341.5: 0.07073720167278.307692308154.8642806422: -0.4161468365299.076923077169.5644333342.5: -0.8011436155319.846153846181.1883745463: -0.9899924966340.615384615186.890158073.5: -0.9364566873361.384615385185.2737884434: -0.6536436209382.153846154176.7350093224.5: -0.2107957994402.923076923163.364411637x = -1x = 1x = cos(y)y = -1y = 1y = cos(x)
book = pyexcel.get_book(file_name='xy.xlsx')
svg = book.plot(chart_type='xy',
     width=600, height=400, explicit_size=True)

Save pyexcel data as svg chart

Line

_images/line.svg

Here is the source code using pyexcel:

title = 'Browser usage evolution (in %)'
x_labels = map(str, range(2002, 2013))
data = {
    'Firefox': [None, None, 0, 16.6, 25, 31, 36.4, 45.5, 46.3, 42.8, 37.1],
    'Chrome': [None, None, None, None, None, None, 0, 3.9, 10.8, 23.8, 35.3],  # flake8: noqa
    'IE': [85.8, 84.6, 84.7, 74.5, 66, 58.6, 54.7, 44.8, 36.2, 26.6, 20.1],
    'Others': [14.2, 15.4, 15.3, 8.9, 9, 10.4, 8.9, 5.8, 6.7, 6.8, 7.5]
}
pe.save_as(
    adict=data,
    dest_title=title,
    dest_x_labels=x_labels,
    dest_chart_type='line',
    dest_file_name='line.svg',
    dest_no_prefix=True
)

Here is the source code using pygal.Line directly

Histogram

_images/histogram.svg

Here is the source code using pyexcel:

data = {
    'Wide bars': [(5, 0, 10), (4, 5, 13), (2, 0, 15)],
    'Narrow bars':  [(10, 1, 2), (12, 4, 4.5), (8, 11, 13)]
}
pe.save_book_as(
    bookdict=data,
    dest_chart_type='histogram',
    dest_file_name='histogram.svg',
    dest_no_prefix=True
)

Here is the source code using pygal.Histogram directly

Single histogram
_images/single_histogram.svg

Here is the source code to draw single sheet histogram:

sheet_name = 'Wide bars'
data = [(5, 0, 10), (4, 5, 13), (2, 0, 15)]
pe.save_as(
    array=data,
    sheet_name=sheet_name,
    dest_chart_type='histogram',
    dest_file_name='single_histogram.svg',
    dest_no_prefix=True
)

XY

BASIC

Basic XY Lines, drawing cosinus:

_images/xy_cosinus.svg

Here is the source code using pyexcel:

data = {
    'x = cos(y)': [(cos(x / 10.), x / 10.) for x in range(-50, 50, 5)],
    'y = cos(x)': [(x / 10., cos(x / 10.)) for x in range(-50, 50, 5)],
    'x = 1':  [(1, -5), (1, 5)],
    'x = -1': [(-1, -5), (-1, 5)],
    'y = 1':  [(-5, 1), (5, 1)],
    'y = -1': [(-5, -1), (5, -1)]
}
pe.save_book_as(
    bookdict=data,
    dest_chart_type='xy',
    dest_title='XY Cosinus',
    dest_file_name='xy_cosinus.svg',
    dest_no_prefix=True
)

Here is the source code using pygal

Single xy line
_images/single_xy_cosinus.svg

Here is the source code to draw single sheet histogram:

from math import cos
sheet_name = 'x = cos(y)'
data = [(cos(x / 10.), x / 10.) for x in range(-50, 50, 5)]
pe.save_as(
    array=data,
    sheet_name=sheet_name,
    dest_chart_type='xy',
    dest_title='XY Cosinus',
    dest_file_name='single_xy_cosinus.svg',
    dest_no_prefix=True
)

Pie chart

_images/pie.svg

Here is the source code using pyexcel:

title = 'Browser usage in February 2012 (in %)'
data = OrderedDict()
data['IE'] = [19.5]
data['Firefox'] = [36.6]
data['Chrome'] = [36.3]
data['Safari'] = [4.5]
data['Opera'] = [2.3]
pe.save_as(
    adict=data,
    dest_title=title,
    dest_chart_type='pie',
    dest_file_name='pie.svg',
    dest_no_prefix=True
)

Here is the source code using pygal.Pie directly

Radar chart

_images/radar.svg

Here is the source code using pyexcel:

title = 'V8 benchmark results'
x_labels = [
    'Richards', 'DeltaBlue', 'Crypto', 'RayTrace',
    'EarleyBoyer', 'RegExp', 'Splay', 'NavierStokes']
data = {
    'Chrome': [6395, 8212, 7520, 7218, 12464, 1660, 2123, 8607],
    'Firefox': [7473, 8099, 11700, 2651, 6361, 1044, 3797, 9450],
    'Opera': [3472, 2933, 4203, 5229, 5810, 1828, 9013, 4669],
    'IE': [43, 41, 59, 79, 144, 136, 34, 102],
}
pe.save_as(
    adict=data,
    dest_x_labels=x_labels,
    dest_title=title,
    dest_chart_type='radar',
    dest_file_name='radar.svg',
    dest_no_prefix=True
)

Here is the source code using pygal.Radar directly

Box chart

_images/box.svg

Here is the source code using pyexcel:

title = 'V8 benchmark results'
data = OrderedDict()
data['Chrome'] = [6395, 8212, 7520, 7218, 12464, 1660, 2123, 8607]
data['Firefox'] = [7473, 8099, 11700, 2651, 6361, 1044, 3797, 9450]
data['Opera'] = [3472, 2933, 4203, 5229, 5810, 1828, 9013, 4669]
data['IE'] = [43, 41, 59, 79, 144, 136, 34, 102]
pe.save_as(
    adict=data,
    dest_title=title,
    dest_chart_type='box',
    dest_file_name='box.svg',
    dest_no_prefix=True
)

Here is the source code using pygal.Box directly

Dot chart

_images/dot.svg

Here is the source code using pyexcel:

title = 'V8 benchmark results'
data = OrderedDict()
data['x labels'] = [
    'Richards', 'DeltaBlue', 'Crypto',
    'RayTrace', 'EarleyBoyer', 'RegExp',
    'Splay', 'NavierStokes']
data['Chrome'] = [6395, 8212, 7520, 7218, 12464, 1660, 2123, 8607]
data['Firefox'] = [7473, 8099, 11700, 2651, 6361, 1044, 3797, 9450]
data['Opera'] = [3472, 2933, 4203, 5229, 5810, 1828, 9013, 4669]
data['IE'] = [43, 41, 59, 79, 144, 136, 34, 102]
pe.save_as(
    adict=data,
    dest_title=title,
    dest_chart_type='dot',
    dest_file_name='dot.svg',
    dest_x_label_rotation=30,
    dest_no_prefix=True
)

Here is the source code using pygal.Dot directly

Funnel chart

_images/funnel.svg

Here is the source code using pyexcel:

title = 'V8 benchmark results'
data = OrderedDict()
data['x labels'] = [
    'Richards', 'DeltaBlue', 'Crypto',
    'RayTrace', 'EarleyBoyer', 'RegExp',
    'Splay', 'NavierStokes']
data['Chrome'] = [6395, 8212, 7520, 7218, 12464, 1660, 2123, 8607]
data['Firefox'] = [7473, 8099, 11700, 2651, 6361, 1044, 3797, 9450]
data['Opera'] = [3472, 2933, 4203, 5229, 5810, 1828, 9013, 4669]
pe.save_as(
    adict=data,
    dest_title=title,
    dest_chart_type='funnel',
    dest_file_name='funnel.svg',
    dest_no_prefix=True
)

Here is the source code using pygal.Funnel directly