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

Grouped bar charts with grid #230

Closed
ngaloppo opened this issue Oct 14, 2016 · 8 comments
Closed

Grouped bar charts with grid #230

ngaloppo opened this issue Oct 14, 2016 · 8 comments

Comments

@ngaloppo
Copy link

I'm trying to produce a grouped bar chart, with horizontal grid lines turned on. Starting from the grouped bar chart example in the altair tutorials, I turned on the grid on the Y axis, as follows:

Chart(population, transform=t).mark_bar().encode(
    X('gender:N', axis=False, scale=Scale(bandSize=6)),
    Y('sum(people):Q', axis=Axis(grid=True)),
    Color('gender:N', scale=Scale(range=["#FF9800", "#03A9F4"])),
    Column('age:O', axis=Axis(orient='bottom'))
).configure(
    facet=FacetConfig(cell=CellConfig(strokeWidth=0, height=250))
)

However, this causes the grid lines to be interrupted between groups. How can we make the grid lines go across group columns?

vega 1

Thanks,

--nico

@jakevdp
Copy link
Collaborator

jakevdp commented Oct 14, 2016

You can set the padding attribute within the scale; for example:

from altair import *
population = load_dataset('population')

Chart(population).mark_bar().encode(
    X('sex:N', axis=False, scale=Scale(bandSize=6)),
    Y('sum(people):Q', axis=Axis(grid=True)),
    Color('sex:N', scale=Scale(range=["#FF9800", "#03A9F4"])),
    Column('age:O', axis=Axis(orient='bottom'), scale=Scale(padding=0))
).configure_facet_cell(
    strokeWidth=0, height=250
)

vega

The result looks a bit different than yours because I didn't apply any transform.

@ngaloppo
Copy link
Author

ngaloppo commented Oct 14, 2016

Thanks that helps. Now what if I want the bars to be grouped, not only sequentially, but also visually? In other words, how do I get the padding between groups back? ;-)

Sorry, that's not very useful, but in many vizualizations I really do want the categories be spatially separated for ease of interpretation, but still have the gridlines to be able to compare bar height across categories...

@ngaloppo
Copy link
Author

Apparently this is a "feature" of vega-lite, and the desired appearance can only be achieved through editing of the resulting vega spec.

https://groups.google.com/d/msg/vega-js/P7a_k0sEVNA/pqJ8hVaAAQAJ

@ngaloppo
Copy link
Author

There is now a feature request for this in vega/vega-lite#1636

@jakevdp
Copy link
Collaborator

jakevdp commented Oct 31, 2016

Thanks!

@ellisonbg
Copy link
Collaborator

Closing as vega-lite-related.

@jeet183
Copy link

jeet183 commented Mar 8, 2021

You can set the padding attribute within the scale; for example:

from altair import *
population = load_dataset('population')

Chart(population).mark_bar().encode(
    X('sex:N', axis=False, scale=Scale(bandSize=6)),
    Y('sum(people):Q', axis=Axis(grid=True)),
    Color('sex:N', scale=Scale(range=["#FF9800", "#03A9F4"])),
    Column('age:O', axis=Axis(orient='bottom'), scale=Scale(padding=0))
).configure_facet_cell(
    strokeWidth=0, height=250
)

vega

The result looks a bit different than yours because I didn't apply any transform.

scale is not available in Column in latest version of altair. How to achieve this in latest version?

@jakevdp
Copy link
Collaborator

jakevdp commented Mar 8, 2021

Use the spacing argument to configure_facet; see https://altair-viz.github.io/gallery/us_population_over_time.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants