For AI agents: a documentation index is available at the root level at /llms.txt and /llms-full.txt. Append /llms.txt to any URL for a page-level index, or .md for the markdown version of any page.
PlaygroundChangelogSign In
OverviewAPI ReferencePre-recorded STTStreaming STTVoice AgentsSpeech UnderstandingGuardrailsLLM GatewayFAQ
OverviewAPI ReferencePre-recorded STTStreaming STTVoice AgentsSpeech UnderstandingGuardrailsLLM GatewayFAQ
  • Getting started
    • Transcribe a pre-recorded audio file
    • Model selection
    • View model benchmarks
    • Evaluate model accuracy
    • Cloud endpoints & data residency
    • Manage concurrent requests
    • Webhooks
  • Models
    • Medical Mode
  • Features
    • Boost specific terms
    • Label speakers
    • Transcribe multiple audio channels
    • Transcribe audio with mixed languages
    • Correct spelling of terms
    • Include filler words
    • Search for words in transcript
    • Set the start and end of the transcript
  • Guides
      • Build a meeting notetaker
      • Build a medical scribe
      • Build a contact center application
        • Iterate over Speaker Labels with Make.com
        • Calculate the Talk / Listen Ratio of Speakers
        • Plot A Speaker Timeline with Matplotlib
        • Generate Custom Speaker Labels with Pyannote
        • Use Speaker Diarization with Async Chunking
        • Setup A Speaker Identification System using Pinecone & Nvidia TitaNet
LogoLogo
PlaygroundChangelogSign In
On this page
  • Quickstart
  • Get Started
  • Step-by-Step Instructions
GuidesTutorialsSpeaker labels

Plot A Speaker Timeline with Matplotlib

Was this page helpful?
Previous

Generate Custom Speaker Labels with Pyannote

Next
Built with

In this guide, we’ll show you how to plot a speaker timeline with matplotlib, using results from the speaker diarization model.

Quickstart

1import assemblyai as aai
2import matplotlib.pyplot as plt
3
4aai.settings.api_key = "YOUR_API_KEY"
5
6config = aai.TranscriptionConfig(speaker_labels=True, speech_models=["universal-3-pro", "universal-2"])
7transcriber = aai.Transcriber()
8transcript = transcriber.transcribe("./my-audio.mp3", config)
9utterances = transcript.utterances
10
11def plot_speaker_timeline(utterances):
12 fig, ax = plt.subplots(figsize=(12, 4))
13 colors = ['b', 'g', 'r', 'c', 'm', 'y', 'k']
14 speaker_colors = {}
15
16 for utterance in utterances:
17 start = utterance.start / 60000 # in minutes
18 end = utterance.end / 60000 # in minutes
19 speaker = utterance.speaker
20
21 if speaker not in speaker_colors:
22 speaker_colors[speaker] = colors[len(speaker_colors) % len(colors)] # set a colour for each new speaker
23
24 ax.barh(speaker, end - start, left=start, color=speaker_colors[speaker], height=0.4) # create horizontal bar plot
25
26 ax.set_xlabel('Time (mins)')
27 ax.set_ylabel('Speakers')
28 ax.set_title('Speaker Timeline')
29 ax.grid(True, which='both', linestyle='--', linewidth=0.5)
30 plt.show()
31
32plot_speaker_timeline(utterances)

Get Started

Before we begin, make sure you have an AssemblyAI account and an API key. You can sign up for a free account and get your API key from your dashboard.

Step-by-Step Instructions

Install the SDK.

$pip install -U assemblyai
$!pip install -U matplotlib

Import the assemblyai package and set the API key.

1import assemblyai as aai
2
3aai.settings.api_key = "YOUR_API_KEY"

Create a TranscriptionConfig object and set speaker labels to True.

1config = aai.TranscriptionConfig(speaker_labels=True, speech_models=["universal-3-pro", "universal-2"])

Create a Transcriber object.

1transcriber = aai.Transcriber()

Use the Transcriber object’s transcribe method and pass in the audio file’s path and config object as parameters. The transcribe method saves the results of the transcription to the Transcriber object’s transcript attribute.

1transcript = transcriber.transcribe("./my-audio.mp3", config)
Alternatively, you can use an audio URL available on the internet.

Extract the utterances from the transcript and set this to utterances.

1utterances = transcript.utterances

Import the matplotlib.pyplot library. Then use the following plot_speaker_timeline function which results in a plot image of the speaker timeline. This function extracts the start and end timestamps of each utterance per speaker and plots the data onto the horizontal bar chart. The X and Y axis are labelled accordingly.

1import matplotlib.pyplot as plt
2
3def plot_speaker_timeline(utterances):
4 fig, ax = plt.subplots(figsize=(12, 4))
5 colors = ['b', 'g', 'r', 'c', 'm', 'y', 'k']
6 speaker_colors = {}
7
8 for utterance in utterances:
9 start = utterance.start / 60000 # in minutes
10 end = utterance.end / 60000 # in minutes
11 speaker = utterance.speaker
12
13 if speaker not in speaker_colors:
14 speaker_colors[speaker] = colors[len(speaker_colors) % len(colors)] # set a colour for each new speaker
15
16 ax.barh(speaker, end - start, left=start, color=speaker_colors[speaker], height=0.4) # create horizontal bar plot
17
18 ax.set_xlabel('Time (mins)')
19 ax.set_ylabel('Speakers')
20 ax.set_title('Speaker Timeline')
21 ax.grid(True, which='both', linestyle='--', linewidth=0.5)
22 plt.show()

Finally, call the plot_speaker_timeline function passing utterances as a parameter to see the plot image result.

1plot_speaker_timeline(utterances)