Search code examples
pythonpandasaltair

Having trouble plotting graph with Altair


I am trying to generate a bar graph with my data, however I am having some trouble formatting the graph and the axis' are showing as 'undefined'

here is the output of my code:

gross_genre = gross_revised[['genre','updated_inflation_values']].groupby('genre').sum().rename(columns={'updated_inflation_values':'sum_of_revenue_by_genre'}).sort_values(by='sum_of_revenue_by_genre', ascending=False)
gross_genre

    sum_of_revenue_by_genre
genre   
Musical 8676964512.00
Adventure   4108634373.00
Comedy  2384794127.00
Drama   2190190503.00
Action  77184895.00

here is my code for the graph:


alt.Chart(gross_genre,width=500,height=500).mark_bar().encode(
        x=alt.X('genre:N', title="Genre"),
        y=alt.Y('sum_of_revenue_by_genre:Q', title="Sum of Revenue")
    ).properties(title='Sum of Revenue by Genre')

however the X axis is showing up with my given title of "Genre", but below that it says "undefined" and there is no data

I would appreciate any help!


Solution

  • It seems like you have a grouped dataframe. Try passing resetting the index via alt.Chart(gross_genre.reset_index() ....