Random Bar Chart Generator with Altair

  • Share this:

Code introduction


This function generates a random bar chart from a given dataset. It uses the Altair library to create the chart and extracts categories and counts from the data to plot the chart.


Technology Stack : Altair

Code Type : Python Function

Code Difficulty : Intermediate


                
                    
import random
import altair as alt

def generate_random_chart(data):
    # Generate a random chart from a given dataset
    color = alt.Color('category', title='Category')
    chart = alt.Chart(data).mark_bar().encode(
        x='category:N',
        y='count(*)',
        color=color
    )
    return chart

# Example usage
data = alt.datasets.barley()
random_chart = generate_random_chart(data)
random_chart.show()                
              
Tags: