File: arc.rst

package info (click to toggle)
python-altair 5.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,952 kB
  • sloc: python: 25,649; sh: 14; makefile: 5
file content (126 lines) | stat: -rw-r--r-- 3,761 bytes parent folder | download
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
.. currentmodule:: altair

.. _user-guide-arc-marks:

Arc
~~~

Arc marks are circular arcs defined by a center point plus angular and radial extents.
Arc marks are typically used for radial plots such as pie and donut charts.

Arc Mark Properties
-------------------
.. altair-plot::
    :hide-code:
    :div_class: properties-example

    import altair as alt
    import numpy as np
    import pandas as pd

    rad_slider = alt.binding_range(min=0, max=100, step=1)
    rad_var = alt.param(bind=rad_slider, value=0, name="radius")

    rad2_slider = alt.binding_range(min=0, max=100, step=1)
    rad_var2 = alt.param(bind=rad_slider, value=50, name="radius2")

    theta_slider = alt.binding_range(min=-2 * np.pi, max=2 * np.pi)
    theta_var = alt.param(bind=theta_slider, value=-0.73, name="theta_single_arc")

    theta_slider2 = alt.binding_range(min=-2 * np.pi, max=2 * np.pi)
    theta2_var = alt.param(bind=theta_slider, value=0.73, name="theta2_single_arc")

    corner_slider = alt.binding_range(min=0, max=50, step=1)
    corner_var = alt.param(bind=corner_slider, value=0, name="cornerRadius")

    pad_slider = alt.binding_range(min=0, max=np.pi / 2)
    pad_var = alt.param(bind=pad_slider, value=0, name="padAngle")

    source = pd.DataFrame({"category": [1, 2, 3, 4, 5, 6], "value": [4, 6, 10, 3, 7, 8]})

    c1 = alt.Chart(source, title="Single Arc").mark_arc(
        radius=rad_var,
        radius2=rad_var2,
        theta=theta_var,
        theta2=theta2_var,
        cornerRadius=corner_var,
        padAngle=pad_var,
    )

    c2 = (
        alt.Chart(source, title="Stacked Arcs")
        .mark_arc(
            radius=rad_var,
            radius2=rad_var2,
            cornerRadius=corner_var,
            padAngle=pad_var,
        )
        .encode(
            theta=alt.Theta(field="value", type="quantitative"),
            color=alt.Color(field="category", type="nominal"),
        )
    )

    alt.hconcat(c1.properties(width=200), c2.properties(width=200)).add_params(
        rad_var, rad_var2, theta_var, theta2_var, corner_var, pad_var
    )

An ``arc`` mark definition can contain any :ref:`standard mark properties <mark-properties>`
and the following special properties:

.. altair-object-table:: altair.MarkDef
   :properties: radius radius2 innerRadius outerRadius theta theta2 cornerRadius padAngle radiusOffset radius2Offset thetaOffset theta2Offset

Examples
--------

We can create a pie chart by encoding ``theta`` or ``color`` arc marks.

.. altair-plot::
    import pandas as pd
    import altair as alt

    source = pd.DataFrame({"category": [1, 2, 3, 4, 5, 6], "value": [4, 6, 10, 3, 7, 8]})

    alt.Chart(source).mark_arc().encode(
        theta=alt.Theta(field="value", type="quantitative"),
        color=alt.Color(field="category", type="nominal"),
    )


Setting ``innerRadius`` to non-zero values will create a donut chart.

.. altair-plot::
    import pandas as pd
    import altair as alt

    source = pd.DataFrame({"category": [1, 2, 3, 4, 5, 6], "value": [4, 6, 10, 3, 7, 8]})

    alt.Chart(source).mark_arc(innerRadius=50).encode(
        theta=alt.Theta(field="value", type="quantitative"),
        color=alt.Color(field="category", type="nominal"),
    )


You can also add a text layer to add labels to a pie chart.

.. altair-plot::
    import pandas as pd
    import altair as alt

    source = pd.DataFrame(
        {"category": ["a", "b", "c", "d", "e", "f"], "value": [4, 6, 10, 3, 7, 8]}
    )

    base = alt.Chart(source).encode(
        theta=alt.Theta("value:Q").stack(True),
        color=alt.Color("category:N").legend(None),
    )

    pie = base.mark_arc(outerRadius=120)
    text = base.mark_text(radius=140, size=20).encode(
        text="category:N"
    )

    pie + text