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

Axes label wrap produce incorrect bar plots when combined with layer and facet #3781

Open
dafxy opened this issue Jan 25, 2025 · 0 comments
Open
Labels
bug needs-triage Bug report needs maintainer response

Comments

@dafxy
Copy link

dafxy commented Jan 25, 2025

What happened?

When we wrap the text and try to use it with layer, fill, and facet, the plot created is all incorrect.

# data
import altair as alt
import pandas as pd
import textwrap 

x = ['a long long long  long long long title', 'a long long long  long long long title',
     'a long long long  long long long title', 'a long long long  long long long title',
     'another long long long title', 'another long long long title',
     'another long long long title', 'another long long long title']
df = pd.DataFrame({
    'y': [10, 20, 30 , 40] * 2,
    'x': x,
    'fill' :  ['a', 'b', 'a', 'b'] * 2,
    'facet' : ['case 1', 'case 1', 'case 2', 'case 2'] * 2
})
df_wrapped = df.copy()
df_wrapped['x'] = [s.split(' ') for s in df['x']]

This works as intended for the bar and text in the plot, but the labels are not readable:

# without wrapping the labels
x         = 'x'
y         = 'y'
fill      = 'fill'
facet     = 'facet'
dodge     = 0.6
base = (alt.Chart(df))
bar = (base.mark_bar()
       .encode(
           x       = alt.X(x).axis(labelAngle=0),
           y       = alt.Y(y),
           fill    = alt.Fill(fill),
           xOffset = alt.XOffset(fill),
       ))
txt = (base.mark_text(dy=-5, size=10)
       .encode(
           x       = alt.X(x),
           y       = alt.Y(y),
           text    = alt.Text(y).format(f'.2f'),
           fill    = alt.Fill(fill),
           xOffset = alt.XOffset(fill),
       ))
g = (alt.layer(bar, txt)
     .properties(width=300, height=250)
     .facet(facet=alt.Facet(facet), columns=2)
     .resolve_scale(
         x='independent',
         xOffset = 'independent'
     )
     )

Image

If we wrap the labels, the bars and text are incorrectly placed in the plot:

# wrapping the labels
x         = 'x'
y         = 'y'
fill      = 'fill'
facet     = 'facet'
dodge     = 0.6
base = (alt.Chart(df_wrapped))
bar = (base.mark_bar()
       .encode(
           x       = alt.X(x).axis(labelAngle=0),
           y       = alt.Y(y),
           fill    = alt.Fill(fill),
           xOffset = alt.XOffset(fill),
       ))
txt = (base.mark_text(dy=-5, size=10)
       .encode(
           x       = alt.X(x),
           y       = alt.Y(y),
           text    = alt.Text(y).format(f'.2f'),
           fill    = alt.Fill(fill),
           xOffset = alt.XOffset(fill),
       ))
g = (alt.layer(bar, txt)
     .properties(width=300, height=250)
     .facet(facet=alt.Facet(facet), columns=2)
     .resolve_scale(
         x='independent',
         xOffset = 'independent'
     )
     )

Image

What would you like to happen instead?

No response

Which version of Altair are you using?

No response

@dafxy dafxy added bug needs-triage Bug report needs maintainer response labels Jan 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug needs-triage Bug report needs maintainer response
Projects
None yet
Development

No branches or pull requests

1 participant