File: charting.rst

package info (click to toggle)
python-agate 1.13.0-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 2,008 kB
  • sloc: python: 8,578; makefile: 126
file content (135 lines) | stat: -rw-r--r-- 4,379 bytes parent folder | download | duplicates (2)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
======
Charts
======

Agate offers two kinds of built in charting: very simple text bar charts and SVG charting via `leather <https://leather.rtfd.io/>`_. Both are intended for efficiently exploring data, rather than producing publication-ready charts.

Text-based bar chart
====================

agate has a builtin text-based bar-chart generator:

.. code-block:: python

    table.limit(10).print_bars('State Name', 'TOTAL', width=80)

.. code-block:: bash

    State Name         TOTAL
    ALABAMA           19,582 ▓░░░░░░░░░░░░░
    ALASKA             2,705 ▓░░
    ARIZONA           46,743 ▓░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
    ARKANSAS           7,932 ▓░░░░░
    CALIFORNIA        76,639 ▓░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
    COLORADO          21,485 ▓░░░░░░░░░░░░░░░
    CONNECTICUT        4,350 ▓░░░
    DELAWARE           1,904 ▓░
    DIST. OF COLUMBIA  2,185 ▓░
    FLORIDA           59,519 ▓░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
                             +-------------+------------+------------+-------------+
                             0          20,000       40,000       60,000      80,000

Text-based histogram
====================

:meth:`.Table.print_bars` can be combined with :meth:`.Table.pivot` or :meth:`.Table.bins` to produce fast histograms:

.. code-block:: Python

    table.bins('TOTAL', start=0, end=100000).print_bars('TOTAL', width=80)

.. code-block:: bash

    TOTAL              Count
    [0 - 10,000)          30 ▓░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
    [10,000 - 20,000)     12 ▓░░░░░░░░░░░░░░░░░░░░░░
    [20,000 - 30,000)      7 ▓░░░░░░░░░░░░░
    [30,000 - 40,000)      1 ▓░░
    [40,000 - 50,000)      2 ▓░░░░
    [50,000 - 60,000)      1 ▓░░
    [60,000 - 70,000)      1 ▓░░
    [70,000 - 80,000)      1 ▓░░
    [80,000 - 90,000)      0 ▓
    [90,000 - 100,000]     0 ▓
                             +-------------+------------+------------+-------------+
                             0.0          7.5         15.0         22.5         30.0

SVG bar chart
=============

.. code-block:: Python

    table.limit(10).bar_chart('State Name', 'TOTAL', 'docs/images/bar_chart.svg')

.. figure:: ../images/bar_chart.svg

SVG column chart
================

.. code-block:: Python

    table.limit(10).column_chart('State Name', 'TOTAL', 'docs/images/column_chart.svg')

.. figure:: ../images/column_chart.svg

SVG line chart
==============

.. code-block:: Python

    by_year_exonerated = table.group_by('exonerated')
    counts = by_year_exonerated.aggregate([
        ('count', agate.Count())
    ])

    counts.order_by('exonerated').line_chart('exonerated', 'count', 'docs/images/line_chart.svg')

.. figure:: ../images/line_chart.svg

SVG dots chart
==============

.. code-block:: Python

    table.scatterplot('exonerated', 'age', 'docs/images/dots_chart.svg')

.. figure:: ../images/dots_chart.svg

SVG lattice chart
==================

.. code-block:: Python

    top_crimes = table.group_by('crime').having([
        ('count', agate.Count())
    ], lambda t: t['count'] > 100)

    by_year = top_crimes.group_by('exonerated')
    
    counts = by_year.aggregate([
        ('count', agate.Count())
    ])

    by_crime = counts.group_by('crime')

    by_crime.order_by('exonerated').line_chart('exonerated', 'count', 'docs/images/lattice.svg')

.. figure:: ../images/lattice.svg

Using matplotlib
================

If you need to make more complex charts, you can always use agate with `matplotlib <https://matplotlib.org/>`_.

Here is an example of how you might generate a line chart:

.. code-block:: python

    import pylab

    pylab.plot(table.columns['homeruns'], table.columns['wins'])
    pylab.xlabel('Homeruns')
    pylab.ylabel('Wins')
    pylab.title('How homeruns correlate to wins')

    pylab.show()