File: rect.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 (91 lines) | stat: -rw-r--r-- 2,654 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
.. currentmodule:: altair

.. _user-guide-rect-marks:

Rect
~~~~

The ``rect`` mark represents an arbitrary rectangle.

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

    import altair as alt

    x_slider = alt.binding_range(min=1, max=100, step=1, name="x")
    x_var = alt.param(bind=x_slider, value=25)

    x2_slider = alt.binding_range(min=1, max=100, step=1, name="x2")
    x2_var = alt.param(bind=x2_slider, value=75)

    y_slider = alt.binding_range(min=1, max=100, step=1, name="y")
    y_var = alt.param(bind=y_slider, value=25)

    y2_slider = alt.binding_range(min=1, max=100, step=1, name="y2")
    y2_var = alt.param(bind=y2_slider, value=75)

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

    alt.Chart().mark_rect(cornerRadius=cornerRadius_var, color="orange").encode(
        x=alt.XDatum(x_var, type="quantitative", scale=alt.Scale(domain=[0, 100])),
        x2=alt.X2Datum(x2_var),
        y=alt.XDatum(y_var, type="quantitative", scale=alt.Scale(domain=[0, 100])),
        y2=alt.X2Datum(y2_var),
    ).add_params(x_var, x2_var, y_var, y2_var, cornerRadius_var)


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

.. altair-object-table:: altair.MarkDef
   :properties: width height align baseline cornerRadius

Examples
--------
Heatmap
^^^^^^^

Using the ``rect`` marks with discrete fields on ``x`` and ``y`` channels creates a heatmap.

.. altair-plot::
    import altair as alt
    from vega_datasets import data

    source = data.seattle_weather()

    alt.Chart(source).mark_rect().encode(
        alt.X("date(date):O").axis(labelAngle=0, format="%e").title("Day"),
        alt.Y("month(date):O").title("Month"),
        alt.Color("max(temp_max):Q").title("Max Temp"),
    )


Ranged Rectangles
^^^^^^^^^^^^^^^^^
Specifying both ``x`` and ``x2`` and/or ``y`` and ``y2`` creates a rectangle that spans over certain x and/or y values.

For example, we can use ``rect`` to create an annotation ``layer`` that provides a shading between global ``min`` and ``max`` values.

.. altair-plot::
    import altair as alt
    from vega_datasets import data

    source = data.cars()

    point = alt.Chart(source).mark_point().encode(
        x="Horsepower:Q",
        y="Miles_per_Gallon:Q",
    )

    rect = alt.Chart(source).mark_rect().encode(
        y="max(Miles_per_Gallon)",
        y2="min(Miles_per_Gallon)",
        opacity=alt.value(0.2),
    )

    point + rect