You can download this code by clicking the button below.
This code is now available for download.
This function uses the Altair library to create a data visualization chart. It accepts a DataFrame and two string arguments representing the base columns for color and size. The function first checks if a global variable named 'data' is defined; if not, it uses the cars dataset from vega_datasets. Then, it creates an Altair chart with circular marks, where the color and size are based on the columns passed in.
Technology Stack : Altair, Vega_datasets, pandas
Code Type : Data visualization
Code Difficulty : Intermediate
def visualize_data(data, color_column, size_column):
import altair as alt
from vega_datasets import data as vega_data
# Assuming 'data' is a pandas DataFrame and 'color_column' and 'size_column' are column names in 'data'
if 'data' not in globals():
data = vega_data.cars()
chart = alt.Chart(data).mark_circle(size=60).encode(
x=alt.X(color_column, scale=alt.Scale(scheme='greenblue')),
y=alt.Y('Horsepower:Q', scale=alt.Scale(scheme='redblue')),
size=size_column,
tooltip=['Make', 'Horsepower', 'Miles_per_Gallon']
)
return chart