Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add title section to customization page #2838

Merged
merged 12 commits into from
Jan 19, 2023
79 changes: 79 additions & 0 deletions doc/user_guide/customization.rst
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,85 @@ Global configurations should be reserved for creating themes that are applied
just before the chart is rendered.


Adjusting the Title
-------------------
By default an Altair chart does not have a title, as seen in this example.

.. altair-plot::

import altair as alt
from vega_datasets import data

iowa = data.iowa_electricity.url

alt.Chart(iowa).mark_area().encode(
x="year:T",
y=alt.Y("net_generation:Q", stack="normalize"),
color="source:N"
)

You can add a simple title by passing the `title` keyword argument with the data.

.. altair-plot::

alt.Chart(iowa, title="Iowa's green energy boom").mark_area().encode(
x="year:T",
y=alt.Y("net_generation:Q", stack="normalize"),
color="source:N"
)

It is also possible to add a subtitle by passing in an `alt.Title` object.

.. altair-plot::

alt.Chart(
iowa,
title=alt.Title(
"Iowa's green energy boom",
subtitle="A growing share of the state's energy has come from renewable sources"
)
).mark_area().encode(
x="year:T",
y=alt.Y("net_generation:Q", stack="normalize"),
color="source:N"
)

The subtitle can run to two lines by passing a list where each list item is a line (if you don't want to create this list manually as in the example below, you can use the ``wrap`` function from the `textwrap library https://docs.python.org/3/library/textwrap.html`_ to split a string into a list of substrings of a certain length).

.. altair-plot::

alt.Chart(
iowa,
title=alt.Title(
"Iowa's green energy boom",
subtitle=["A growing share of the state's energy", "has come from renewable sources"]
)
).mark_area().encode(
x="year:T",
y=alt.Y("net_generation:Q", stack="normalize"),
color="source:N"
)

The ``Title`` object can also configure a number of other attributes, e.g., the position of the title and subtitle (see see :ref:`user-guide-customization` for details).

.. altair-plot::

alt.Chart(
iowa,
title=alt.Title(
"Iowa's green energy boom",
subtitle=["A growing share of the state's energy", "has come from renewable sources"],
anchor='start',
orient='bottom',
offset=20
)
).mark_area().encode(
x="year:T",
y=alt.Y("net_generation:Q", stack="normalize"),
color="source:N"
)


Adjusting Axis Limits
---------------------
The default axis limit used by Altair is dependent on the type of the data.
Expand Down
8 changes: 7 additions & 1 deletion tests/examples_methods_syntax/iowa_electricity.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@

source = data.iowa_electricity()

alt.Chart(source, title="Iowa's renewable energy boom").mark_area().encode(
alt.Chart(
source,
title=alt.Title(
"Iowa's green energy boom",
subtitle="A growing share of the state's energy has come from renewable sources"
)
).mark_area().encode(
alt.X("year:T").title("Year"),
alt.Y("net_generation:Q")
.title("Share of net generation")
Expand Down