Generating Plotly charts

Generating Plotly charts

Plotly is a set of open-source charting libraries that help you create interactive, web-ready charts (like bar charts, line charts, scatter plots, heatmaps, and even 3D charts). Plotly charts are designed for modern dashboards and reports: users can hover to see exact values, zoom/pan, toggle series on/off, and often export the chart as an image or share it as interactive HTML.

Why Plotly?

Interactive by default: hover tooltips, zoom, pan, and legend toggles help users explore data quickly.
Looks professional: charts are "publication-quality" and work well in dashboards and client deliverables.
Works across languages: Plotly supports Python, JavaScript, R (and more), so teams can use it in different stacks.
Easy to share: charts can be rendered in web apps or saved as interactive HTML.

Plotly website (official)
https://plotly.com/
https://plotly.com/python/
https://plotly.com/javascript/

In this article, you will learn how to generate plotly charts using the EZ Charts feature.

Before you begin

To learn how to open EZ Charts window from templates page in SPAN sports performance analysis platform, refer to this article.
To learn how to access EZ Charts for a file, refer to this article.

Generating Plotly charts

After opening the EZ Charts window from the templates page, click any bar chart from the list to generate a basic Plotly chart.



Once clicked, it will create a chart. A success message at the bottom right of the page will say "New chart created successfully," indicating that the chart has been created successfully.



To access the chart, click the "Charts" button on the file page. Upon clicking, the chart will be displayed as shown below.



In the EZ Charts window, to check the code and modify this basic Plotly chart, click the "View chart" button for the chart.



Upon clicking, it will open the code editor with the default EZ Charts code written for this basic Plotly chart.



LLM prompt for ChatGPT

Notes
Note: While we support plotly express (px), px can sometimes make customdata harder to manage consistently for creating video linked charts. If you want fewer surprises, add: “Prefer go.Figure + go.Bar/go.Scatter so customdata is explicit.”

You can use LLMs such as ChatGPT to modify the code and improve the chart. Below is the ChatGPT prompt used to modify the chart.

Part 1: The master prompt contains points 1–6, which you should not edit.
Part 2: Point 7 contains user-defined requirements where you can specify your needs.

When providing the prompt to ChatGPT, make sure to combine all seven sections and submit them together in a single prompt.

Part 1: The master prompt

Quote
You will write
Python code only that generates a Plotly figure JSON to be rendered later in a frontend JavaScript app using Plotly.js.

1) Imports (must not be changed)
  • 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

2) Execution context and output (must not be changed)
  • 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.

3) Input data (must not be changed)
  • The pandas dataframe is always named df.

4) 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.

5) Starter example code (must not be changed)

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()
6) Final checklist (must not be changed)
  • 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

Part 2: The user-defined prompt


7) User-modifiable chart instructions (users may change ONLY this section)

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


The above combined Part 1 and Part 2 ChatGPT prompt will generate a code in response. In the code editor, replace the default EZ Charts code with the updated code generated by ChatGPT, and click the "SAVE CHANGES" button at the top right to update the chart.



Re-launch the charts page from the file to view the updated chart, as shown below.


Accessing video-linked charts

These charts are video-linked and clickable. Click on any chart segment to open the related video clips, as shown below.



    • Related Articles

    • Generating Apache ECharts

      Apache ECharts is an open-source JavaScript charting and data-visualization library used to build interactive charts in the browser. It supports many chart types (bar, line, pie, scatter, heatmaps, sankey, etc.) and includes user-friendly ...
    • Introduction to EZ Charts

      Introduction to EZ Charts Today, many people want to visualise and understand data but building dashboards in a simple and flexible way is still a challenge. Coaches, analysts, and teams (both in sports and non-sports domains) often want to create ...
    • Share EZ Charts publicly

      In this article, you will learn how to share EZ Charts publicly. Before you begin To learn how to open EZ Charts window from templates page in SPAN sports performance analysis platform, refer to this article. To learn how to access EZ Charts for a ...
    • Open EZ Charts window

      EZ Charts are specific to a template. In this article, you will learn how to open EZ Charts window from templates page. Before you begin To learn how to create a template in SPAN sports performance analysis platform, refer to this article. Open EZ ...
    • Access EZ Charts for a file

      In this article, you will learn how to access EZ Charts for a file. Before you begin To learn how to create a template in SPAN sports performance analysis platform, refer to this article. To learn basics of video tagging in SPAN sports performance ...