-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathplot.py
More file actions
executable file
·57 lines (45 loc) · 1.66 KB
/
plot.py
File metadata and controls
executable file
·57 lines (45 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import sys
import matplotlib.pyplot as plt
import pandas as pd
def load_and_arrange_data_frame():
"""Get the the artist out of the dataframe and drop other columns.
Relies on you having run downloader.py first to populate the CSV.
"""
try:
dataframe = pd.read_csv("data/lastfm_scrobbles.csv")
except FileNotFoundError:
print(
"""
data/lastfm_scrobbles.csv not found.
Make sure you run downloader.py before running this script.
"""
)
sys.exit(1)
dataframe = dataframe.drop(["album", "track", "datetime", "timestamps"], axis=1)
dataframe["scrobbles"] = dataframe.groupby("artist")["artist"].transform("count")
dataframe = dataframe.drop_duplicates()
return dataframe.sort_values(by="scrobbles", ascending=False)
def get_plot(dataframe):
"""Arrange the plot.
Creates a bar chart with Artist on the x axis and number of scrobbles of
that artist on the y axis.
Rotates the artist names on the x axis so they fit on the chart.
"""
plt.xkcd()
dataframe = dataframe.iloc[0:20]
dataframe.plot(x="artist", y="scrobbles", kind="bar")
plt.tick_params(axis="x", pad=6)
plt.margins(0.2)
plt.xticks(fontsize=8, horizontalalignment="left")
plt.tight_layout()
plt.xticks(rotation=-45)
plt.ylabel("Scrobbles")
plt.tick_params(axis="x", which="major", pad=10)
plt.subplots_adjust(right=0.9, bottom=0.3)
plt.tight_layout()
return plt
arranged_dataframe = load_and_arrange_data_frame()
plot = get_plot(arranged_dataframe)
# Save plot to ./chart.png
plt.savefig("chart.png", dpi=500)
print("Saved chart to ./chart.png.")