Sitemap
Data Science Collective

Advice, insights, and ideas from the Medium data science community

Follow publication

How to build an interactive data visualization with gen AI

7 min readFeb 18, 2025

--

Photo by Stockcake
The final dashboard result from this tutorial.

Note on use of gen AI

What is PyCafe?

The data

Chart generation with Vizro-AI

Prototyping a set of Plotly charts

Common job titles

Chart 1: the most common job titles for my connections.
import vizro.plotly.express as px
from vizro.models.types import capture

@capture("graph")

def custom_chart(data_frame):
# Count the frequency of each position
position_counts = data_frame["Position"].value_counts().head(20)

# Create a horizontal bar chart
fig = px.bar(
position_counts,
x=position_counts.values,
y=position_counts.index,
orientation="h",
title="Top 20 Most Common Positions",
)

# Update layout for better readability
fig.update_layout(
yaxis_title="Position",
xaxis_title="Frequency",
yaxis=dict(categoryorder="total ascending"),
)

return fig

Common employers

Chart 2: the most common companies for my connections (note the data has been sanitised; these companies are fictional for the purpose of this tutorial).
import vizro.plotly.express as px
from vizro.models.types import capture

@capture("graph")
def custom_chart(data_frame):
# Count the occurrences of each company
company_counts = data_frame["Company"].value_counts().nlargest(5)
# Create a DataFrame for the top 5 companies
top_companies = company_counts.reset_index()
top_companies.columns = ["Company", "Count"]

# Create a doughnut chart
fig = px.pie(
top_companies,
values="Count",
names="Company",
hole=0.4,
title="Top 5 Most Common Companies",
)

return fig

Cumulative connection total

Cumulative connection count.
import pandas as pd
import plotly.graph_objects as go
from vizro.models.types import capture

@capture("graph")
def custom_chart(data_frame):
# Convert 'Connected On' to datetime and extract the year
data_frame["Year"] = pd.to_datetime(data_frame["Connected On"]).dt.year
# Count connections per year
yearly_counts = data_frame["Year"].value_counts().sort_index()
# Calculate cumulative sum of connections
cumulative_counts = yearly_counts.cumsum()
# Create figure with secondary y-axis
fig = go.Figure()
# Add bar chart for yearly connections
fig.add_trace(go.Bar(x=yearly_counts.index, y=yearly_counts, name="Connections per Year"))
# Add scatter plot for cumulative connections
fig.add_trace(
go.Scatter(
x=cumulative_counts.index,
y=cumulative_counts,
name="Cumulative Connections",
yaxis="y2",
)
)

# Set up the layout for secondary y-axis
fig.update_layout(yaxis2=dict(title="Cumulative Total", overlaying="y", side="right"))
return fig

Adding the charts to a Vizro dashboard

import vizro.plotly.express as px
from vizro import Vizro
import vizro.models as vm

page = vm.Page(
title="My LinkedIn connections",
layout=vm.Layout(grid=[[0, 1], [0, 2]]),
components=[

],
)

dashboard = vm.Dashboard(pages=[page], theme="vizro_light")
Vizro().build(dashboard).run()
components=[
vm.Graph(id="role_bar_chart_id", figure=common_jobs(data_frame=df)),
vm.Graph(id="company_pie_chart_id", figure=common_companies(data_frame=df)),
vm.Graph(id="growth_cumulative_chart_id", figure=cumulative_totals(data_frame=df)),
],
The basic project found at py.cafe/stichbury/linkedin-connections-prototype

Adding interactivity

controls=[
vm.Parameter(
targets=["role_bar_chart_id.top_n"],
selector=vm.Slider(min=0, max=30, step=5, value=20, title="Show top n positions:"),
),

vm.Parameter(
targets=["company_pie_chart_id.top_n"],
selector=vm.Slider(min=0, max=30, step=5, value=5, title="Show top n companies:"),
)
]

Summary

Resources

--

--

Data Science Collective
Data Science Collective

Published in Data Science Collective

Advice, insights, and ideas from the Medium data science community

Jo Stichbury
Jo Stichbury

Written by Jo Stichbury

Technical content creator writing about data science and software. Old-school Symbian C++ developer, now accidental cat herder and goose chaser.

Responses (6)