Altair: Specifying Images as labels along Y-axis

Created on 6 Feb 2020  路  2Comments  路  Source: altair-viz/altair

Hi,
Is there a way to specify images as labels along Y-axis in Altair as shown in the image?
(Image Source: https://in.pcmag.com/news/132040/countries-that-spend-the-most-time-on-social-media-face-the-most-consequences)

Countries

question

Most helpful comment

There are two ways to do something like this. Images can be included in the labels if the images can be expressed as unicode emojis; here is an example:

import altair as alt
import pandas as pd

df = pd.DataFrame({
    "country": ["Philippines 馃嚨馃嚟", "Nigeria 馃嚦馃嚞", "Mexico 馃嚥馃嚱"],
    "value": [100, 96, 94]
})

alt.Chart(df).mark_bar().encode(
    y='country:N',
    x='value:Q'
)

visualization - 2020-02-06T055441 783

If the images you want to use are available by URL, you can use an image mark along with concatenation to achieve roughly the same effect:

df = pd.DataFrame({
    "country": ["Philippines", "Nigeria", "Mexico"],
    "value": [100, 96, 94]
})

df['flag_image'] = [
    f"https://www.countries-ofthe-world.com/flags-normal/flag-of-{country}.png"
    for country in df['country']
]

flags = alt.Chart(df).mark_image().encode(
    y=alt.Y('country', axis=alt.Axis(domainOpacity=0, ticks=False)),
    url="flag_image"
)

chart = alt.Chart(df).mark_bar().encode(
    y=alt.Y('country', axis=None),
    x='value'
)

alt.concat(
    flags, chart
).configure_concat(
    spacing=0
).configure_view(
    strokeOpacity=0
)

Screen Shot 2020-02-06 at 6 03 48 AM

All 2 comments

There are two ways to do something like this. Images can be included in the labels if the images can be expressed as unicode emojis; here is an example:

import altair as alt
import pandas as pd

df = pd.DataFrame({
    "country": ["Philippines 馃嚨馃嚟", "Nigeria 馃嚦馃嚞", "Mexico 馃嚥馃嚱"],
    "value": [100, 96, 94]
})

alt.Chart(df).mark_bar().encode(
    y='country:N',
    x='value:Q'
)

visualization - 2020-02-06T055441 783

If the images you want to use are available by URL, you can use an image mark along with concatenation to achieve roughly the same effect:

df = pd.DataFrame({
    "country": ["Philippines", "Nigeria", "Mexico"],
    "value": [100, 96, 94]
})

df['flag_image'] = [
    f"https://www.countries-ofthe-world.com/flags-normal/flag-of-{country}.png"
    for country in df['country']
]

flags = alt.Chart(df).mark_image().encode(
    y=alt.Y('country', axis=alt.Axis(domainOpacity=0, ticks=False)),
    url="flag_image"
)

chart = alt.Chart(df).mark_bar().encode(
    y=alt.Y('country', axis=None),
    x='value'
)

alt.concat(
    flags, chart
).configure_concat(
    spacing=0
).configure_view(
    strokeOpacity=0
)

Screen Shot 2020-02-06 at 6 03 48 AM

Awesome. Thank you very much, Jake.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fischcheng picture fischcheng  路  4Comments

morberg picture morberg  路  3Comments

bmcfee picture bmcfee  路  3Comments

tonylee3399 picture tonylee3399  路  3Comments

nielsmde picture nielsmde  路  4Comments