Skip to content

Instantly share code, notes, and snippets.

@SKaplanOfficial
Last active January 18, 2023 21:40
Show Gist options
  • Save SKaplanOfficial/d6bb3786015b915356fa2f907acfd1c2 to your computer and use it in GitHub Desktop.
Save SKaplanOfficial/d6bb3786015b915356fa2f907acfd1c2 to your computer and use it in GitHub Desktop.
PyXA script to create a scatterplot of the average duration of songs per genre in your Music.app library
from collections import Counter
import matplotlib.pyplot as plt
import numpy as np
import PyXA # Version 0.2.0
app = PyXA.Application("Music")
tracks = app.tracks().greater_than("duration", 0)
durations = tracks.duration()
genres = tracks.genre()
data = {genre: [0, 0] for genre in genres}
for index, duration in enumerate(durations):
data[genres[index]][0] += duration
data[genres[index]][1] += 1
for genre in data:
data[genre][0] = data[genre][0] / data[genre][1]
labels = list(data.keys())
values = [v[0] for v in data.values()]
plt.scatter(labels, values)
plt.xticks(rotation='vertical', fontsize=8)
plt.title("Average Playtime By Genre")
plt.ylabel("Average Playtime (seconds)")
plt.xlabel("Genre")
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment