

Do not write any import statements.
Import statements will be ignored.
These libraries are already available:
pandas as pd
numpy as np
json
plotly
plotly.express as px
plotly.graph_objects as go
from plotly.subplots import make_subplots
pyecharts
This code runs in a backend Python environment and must only produce Plotly JSON (not render plots).
You must build a Plotly figure object named fig.
The final output must be exactly:
result = fig.to_json()
Do not call fig.show(). Do not write files. Do not return anything else.
The pandas dataframe is always named df.
customdata rules (must not be changed)This system links chart elements to underlying events using Plotly customdata.
A) Required structure (when available)
When event_uuid exists in df, customdata must be a list of dicts of the form:
{'event_list': [...]}
B) Segment-level behavior
For grouped or stacked charts (and any multi-part chart), event_list must include only the events in that exact segment (not all events in the x-category).
C) Ordering + uniqueness
Preserve the original df order when collecting event UUIDs.
Do not include duplicates in event_list.
D) Fail-safe requirement
If event_uuid does not exist in df, the code must still run successfully and generate the chart JSON.
In that case, either omit customdata entirely or set it safely to empty lists, without throwing errors.
Use this example as the baseline pattern for creating charts. Users will modify chart logic and styling later, but must preserve the output and customdata principles.
# The default dataframe is named as df.
# Count occurrences of each primary category
category_counts = df['Primary category'].value_counts().sort_values(ascending=True)
# Create customdata for each bar (fail-safe if event_uuid is missing)
customdata = []
for category in category_counts.index:
category_rows = df[df['Primary category'] == category]
event_uuids = []
if 'event_uuid' in df.columns:
for uuid in category_rows['event_uuid'].tolist():
s = str(uuid)
if s not in event_uuids:
event_uuids.append(s)
customdata.append({'event_list': event_uuids})
# Create the plot
fig = go.Figure()
# Add the bar trace
fig.add_trace(
go.Bar(
x=category_counts.values,
y=category_counts.index,
orientation='h',
marker_color='#1976d2',
customdata=customdata,
hovertemplate="🎥 Click to see video clips<br>Category: %{y}<br>Count: %{x}<extra></extra>"
)
)
# Update layout
fig.update_layout(
title="Primary Category Distribution",
plot_bgcolor='black',
paper_bgcolor='black',
font_color='white',
height=400,
width=1200,
showlegend=False,
margin=dict(l=100, r=50, t=50, b=50)
)
# Update axes
fig.update_xaxes(
title_text="Count",
gridcolor='rgba(128, 128, 128, 1)',
gridwidth=1,
griddash='dot'
)
fig.update_yaxes(
title_text="Primary Category",
gridcolor='rgba(255, 100, 0, 1)',
gridwidth=1,
griddash='dot'
)
result = fig.to_json()
Uses dataframe df
Creates Plotly figure fig
Ends with result = fig.to_json()
Adds per-element customdata using {'event_list': [...]} when event_uuid is available
Does not crash if event_uuid is missing
Apply these chart-level styling rules (example):
Chart title must be: Invovlements of the players
X-axis title must be: Players with their jersey numbers
Y-axis title must be: Number of Involvements
Font size for axis titles must be 14px
Use a dark background
Bars must be light pink