How to add different colours in an Altair grouped bar chart in python?

35 Views Asked by At

enter image description here

This is a Star Wars database and I have made this groouped bar chart in altair and genereted facets that now produce the preference of the fans for the original movies vs the prequels on the columns and gender based classification on the rows. But, I was only able to generate a colour distinction in the "Yes" or "No" preference. I want to know how can I change the colours of each of the graph separately. For example - the male could be blue and orange, and the female could be green and red etc.

This is how my data frame looks after the manipulation of the original dataset and this is the one I have passed in the chart as "alpha".

Index Fan Gender Preference Percentage
0 No Male Original Trilogy Preference 0.354167
1 Yes Male Original Trilogy Preference 0.645833
2 No Female Original Trilogy Preference 0.308824
3 Yes Female Original Trilogy Preference 0.691176
4 No Male Prequels Preference 0.313433
5 Yes Male Prequels Preference 0.686567
6 No Female Prequels Preference 0.193548
7 Yes Female Prequels Preference 0.806452

I have attached my chart code below -

chart = alt.Chart(alpha).mark_bar(size = 15).encode(
        y = alt.Y('Do you consider yourself to be a fan of the Star Trek franchise?:N',
                  axis = alt.Axis(title = ''),
                  sort = 'descending'
                 ),
        x = alt.X('Percentage:Q', 
                  axis = None,
                  scale=alt.Scale(domain=[0, 1])
                 ),
        color = alt.Color('Do you consider yourself to be a fan of the Star Trek franchise?:N', 
                          legend = None, 
                          scale=alt.Scale(domain=domain, range=range_)
                         ),
     )
    text = chart.mark_text(
                align = 'left',
                baseline = 'middle',
                dx = 5,
                color = '#4da74a'
            ).encode(
                text = alt.Text('Percentage:Q', format = '.0%') 
            )

    result = alt.layer(
                    chart, text, data = alpha
                ).facet(
                    row = alt.Row('Gender:N', title = None, sort = 'descending'),
                    column = alt.Column('Preference:N', title = None)
                ).configure(
                    background = '#f0f0f0'
                ).configure_view(
                    stroke = None
                ).configure_axis(
                    labelFontSize = 12,
                    domainOpacity = 0.4,
                    labelColor = '#ababab',
                    labelOpacity = 1,
                    labelFontWeight = 600,
                    labelPadding = 5,
                    tickSize = 10,
                    tickColor = '#ababab',
                    tickOpacity = 1
                ).configure_title(
                    anchor = 'start',
                    fontSize = 25,
                    fontWeight = 700,
                    dy = -20,
                    subtitleColor = '#000000',
                    subtitleFontWeight = 600
                ).properties(
                    title = {"text":"Preference for Original Trilogy vs. Prequels",
                             "subtitle": "by Familiarity with Expanded Universe and Gender(Grouped Bar Chart)"}
    )
result

I tried some configure methods but none seemed to work.

0

There are 0 best solutions below