Skip to content

Instantly share code, notes, and snippets.

@quanon
Last active November 6, 2024 23:15
Show Gist options
  • Save quanon/4ce5b08ccc49b8fb7cc9319b13a8e5a5 to your computer and use it in GitHub Desktop.
Save quanon/4ce5b08ccc49b8fb7cc9319b13a8e5a5 to your computer and use it in GitHub Desktop.
Draw a scatter plot of yo-yo diameter/width
import csv
import os
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import numpy as np
from scipy.stats import gaussian_kde
# Use Agg (Anti-Grain Geometry) to avoid the error “ModuleNotFoundError: No module named ‘_tkinter’”.
matplotlib.use('Agg')
XMIN = 40.0
XMAX = 80.0
YMIIN = 30.0
YMAX = 60.0
diameters = []
widths = []
# CSV output by Notion
csv_filepath = os.path.join(os.environ['HOME'], 'Downloads/yoyos.csv')
with open(csv_filepath, 'r', encoding='utf-8') as f:
reader = csv.reader(f)
for i, row in enumerate(reader):
if i == 0:
continue
diameters.append(round(float(row[3]), 1))
widths.append(round(float(row[4]), 1))
x = np.array(diameters)
y = np.array(widths)
fig, ax = plt.subplots()
ax.set_xlabel('Diameter (mm)', fontsize=10)
ax.set_xlim(XMIN, XMAX)
ax.xaxis.set_major_locator(ticker.MultipleLocator(5.0))
ax.xaxis.set_minor_locator(ticker.MultipleLocator(1.0))
ax.set_ylabel('Width (mm)', fontsize=10)
ax.set_ylim(YMIIN, YMAX)
ax.yaxis.set_major_locator(ticker.MultipleLocator(5.0))
ax.yaxis.set_minor_locator(ticker.MultipleLocator(1.0))
ax.set_axisbelow(True)
ax.grid(which='both')
xy = np.vstack([x, y])
z = gaussian_kde(xy)(xy)
plt.scatter(x, y, alpha=0.8, c=z, cmap='jet')
plt.savefig(os.path.join(os.environ['HOME'], 'Downloads/fig.png'))