You can download this code by clicking the button below.
This code is now available for download.
This function accepts a dataset and a chart type, then generates a chart of a randomly chosen type. It uses the Altair library to create the chart.
Technology Stack : Altair
Code Type : Function
Code Difficulty : Intermediate
import random
from altair import Chart, Dataset
def generate_random_chart(data, chart_type):
"""
Generates a random chart from the given data using a random chart type from Altair.
"""
chart_types = {
'bar': Chart.bar,
'line': Chart.line,
'area': Chart.area,
'point': Chart.point,
'rule': Chart.rule,
'interval': Chart.interval
}
chart_func = random.choice(list(chart_types.values()))
chart = chart_func(data, title='Random Chart', width=400, height=300)
return chart
# Example usage:
# Assuming 'data' is a DataFrame or similar data structure
# chart = generate_random_chart(data, 'bar')
# chart.display()