Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify plot title and facet title in Altair LayerChart

Tags:

python

altair

Using the iris dataset we can create a simple faceted chart:

import altair as alt
from vega_datasets import data
iris = data.iris.url

alt.Chart(iris, title='Main Title').mark_bar().encode(
    x='petalWidth:Q',
    y='count(petalLength):Q',
    color='species:N',
    facet=alt.Facet('species:N', title=None)
)

enter image description here

Here I can control both the main plot title and the title of the facets respectively.

Now let's say I want create the same chart, but add text annotations to each bar:

base = alt.Chart(iris).encode(
    x='petalWidth:Q',
    y='count(petalLength):Q',
    color='species:N',
    text='count(petalLength):Q'
)

c = base.mark_bar()
t = base.mark_text(dy=-6)

alt.layer(c, t).facet('species:N', title=None).properties(title='Main Title')

enter image description here

This time, there is the species title above the facets. How can I control both the main plot title and the facet title in this case?

like image 831
C. Braun Avatar asked Oct 29 '19 20:10

C. Braun


People also ask

How do I remove the legend from Altair?

Another thing you can do is move the legend to another position with the orient argument. You can remove the legend entirely by submitting a null value.

How do I get rid of gridlines on Altair?

We can remove the grid lines on x or y-axis by specifying the argument grid=False inside alt. X() or alt. Y() method in the encoding channels.


1 Answers

Keyword arguments passed to the facet method are passed through to the alt.FacetChart chart that is created.

Any keyword arguments you want passed to the facet encoding can be passed via an alt.Facet() wrapper, similar to other encodings.

For example:

alt.layer(c, t).facet(
    facet=alt.Facet('species:N', title=None),
    title='Main Title'
)

enter image description here

like image 78
jakevdp Avatar answered Oct 02 '22 08:10

jakevdp