Skip to content

For clean Markdown of any page, append .md to the page URL. For a complete documentation index, see For full documentation content, see For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at

Plot A Speaker Timeline with Matplotlib

For the complete documentation index, see llms.txtIn this guide, we'll show you how to plot a speaker timeline with matplotlib, using results from the speaker diarization model.## Quickstart```python import assemblyai as aai import matplotlib.pyplot as plt

aai.settings.api_key = "YOUR_API_KEY"

config = aai.TranscriptionConfig(speaker_labels=True, speech_models=["universal-3-pro", "universal-2"]) transcriber = aai.Transcriber() transcript = transcriber.transcribe("./my-audio.mp3", config) utterances = transcript.utterances

def plot_speaker_timeline(utterances): fig, ax = plt.subplots(figsize=(12, 4)) colors = ['b', 'g', 'r', 'c', 'm', 'y', 'k'] speaker_colors = {}

for utterance in utterances:
    start = utterance.start / 60000 # in minutes
    end = utterance.end / 60000 # in minutes
    speaker = utterance.speaker

    if speaker not in speaker_colors:
        speaker_colors[speaker] = colors[len(speaker_colors) % len(colors)] # set a colour for each new speaker

    ax.barh(speaker, end - start, left=start, color=speaker_colors[speaker], height=0.4) # create horizontal bar plot

ax.set_xlabel('Time (mins)')
ax.set_ylabel('Speakers')
ax.set_title('Speaker Timeline')
ax.grid(True, which='both', linestyle='--', linewidth=0.5)
plt.show()

plot_speaker_timeline(utterances) ### Get StartedBefore 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 InstructionsInstall the SDK.bash pip install -U assemblyai !pip install -U matplotlib Import the `assemblyai` package and set the API key.python import assemblyai as aai

aai.settings.api_key = "YOUR_API_KEY" Create a `TranscriptionConfig` object and set speaker labels to `True`.python config = aai.TranscriptionConfig(speaker_labels=True, speech_models=["universal-3-pro", "universal-2"]) Create a `Transcriber` object.python transcriber = 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.python transcript = 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`.python utterances = 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.python import matplotlib.pyplot as plt

def plot_speaker_timeline(utterances): fig, ax = plt.subplots(figsize=(12, 4)) colors = ['b', 'g', 'r', 'c', 'm', 'y', 'k'] speaker_colors = {}

for utterance in utterances:
    start = utterance.start / 60000 # in minutes
    end = utterance.end / 60000 # in minutes
    speaker = utterance.speaker

    if speaker not in speaker_colors:
        speaker_colors[speaker] = colors[len(speaker_colors) % len(colors)] # set a colour for each new speaker

    ax.barh(speaker, end - start, left=start, color=speaker_colors[speaker], height=0.4) # create horizontal bar plot

ax.set_xlabel('Time (mins)')
ax.set_ylabel('Speakers')
ax.set_title('Speaker Timeline')
ax.grid(True, which='both', linestyle='--', linewidth=0.5)
plt.show()

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