[Python] How can I keep distincts data points with similar X value?

Discussão em 'Python' iniciado por Stack, Agosto 3, 2025.

  1. Stack

    Stack Membro Participativo

    I have a dataframe with 4 rows, two of which having the same value for the "a" field. I'd like to have a bar chart with 4 bars, one for each row.

    Trying with Altair

    source = pd.DataFrame({
    'a': ['A', 'B', 'C', 'B'],
    'b': [10, 20, 30, 15]
    })

    alt.Chart(source).mark_bar().encode(
    x='a',
    y='b'
    )


    This will result in a 3-bar chart, the values for 'B' having been added (20+15=35)

    3-bar chart with Altair

    What I was expecting is a 4-bar chart, like I get with:

    source.plot(kind="bar", x="a", y="b")


    4-bar chart plotting directly from Pandas

    If each value must be unique, I guess I could use the index of the dataframe, but I have not found any way to get the labels from the 'a' column.

    What would be the proper way to solve this?

    Continue reading...

Compartilhe esta Página