vega / altair

Declarative statistical visualization library for Python
https://altair-viz.github.io/
BSD 3-Clause "New" or "Revised" License
9.39k stars 795 forks source link

docs: add example with overlapping bars in a grouped bar chart #3612

Closed mattijn closed 1 month ago

mattijn commented 1 month ago

This PR adds an example that demonstrates how to create partially overlapping bars in a grouped bar chart.

The example looks as such:

image

The overlapping effect was created using the following code (method-based syntax):

import altair as alt
import pandas as pd

source = pd.DataFrame(
    {
        "category": list("AABBCC"),
        "group": list("xyxyxy"),
        "value": [0.1, 0.6, 0.7, 0.2, 0.6, 0.1],
    }
)

base = alt.Chart(source, width=alt.Step(12)).encode(
    x="category:N",
    y="value:Q",
    xOffset=alt.XOffset("group:N").scale(paddingOuter=0.5),
)

alt.layer(
    base.mark_bar(size=20, stroke="white", fillOpacity=0.9).encode(fill="group:N"),
    base.mark_text(dy=-5).encode(text="value:Q"),
)

Explanation:

mattijn commented 1 month ago

Thanks for your review, I will open a new PR with another example based on your suggestion to have text overlay on the bars instead of the text above the bars. That also will highlight an issue that I hope becomes easier to write with your work on expressions (edit: done per https://github.com/vega/altair/pull/3614).