from fractions import Fraction as Rat
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
%matplotlib inline
sns.set_context('paper')
levels = 16
periods = 4
x, y = [], []
for q in range(1,levels):
for p in range(1, periods * q + 1):
x.append(Rat(p,q))
y.append(Rat(1,x[-1].denominator))
x, y = np.array(x), np.array(y)
x, indices = np.unique(x, return_index=True)
y = y[indices]
level_set = 0.27
ls_color = '#0000ff'
fig, ax = plt.subplots(1,1, figsize=(10,6))
ax.scatter(x[:-1],y[:-1], marker='.', color='#000000')
ax.plot([0,periods], [level_set] * 2, linestyle=':', color=ls_color)
ax.text(-0.4*level_set,0.95*level_set, '$\lambda$', color=ls_color, fontsize=12)
ax.set_xlabel('$x$',fontsize=12)
ax.set_ylabel('$f(x)$', fontsize=12)
ax.set_title("Thomae's function", fontsize=12)
sns.despine(fig=fig)
fig.savefig('2018-06-14-thomaes-function.png',bbox_inches='tight')