In [1]:
import numpy as np
import random
import csv
from matplotlib.patches import Rectangle
from matplotlib.collections import PatchCollection
import matplotlib.pyplot as plt
%matplotlib inline
In [69]:
nx=ny=28
imgdata = np.zeros(shape=(nx,ny),dtype=float)
In [5]:
def plotmap(imgdata):
w = 1./nx
for y in range(ny):
for x in range(nx):
yi = ny-y-1
v = imgdata[x][y]
r = Rectangle((x*w,yi*w),w,w,facecolor=(1.-v,1.-v,1.),edgecolor=None)
plt.gca().add_patch(r)
In [6]:
def plotLine(x1,y1,x2,y2,c='b',ax=None):
if ax: # given axis handle
ax.plot([x1, x2], [y1, y2], color=c, linestyle='-', linewidth=4.);
else:
plt.gca().plot([x1, x2], [y1, y2], color=c, linestyle='-', linewidth=4.);
In [10]:
def plotarrowmap(arrowbools,anx):
w = 1./anx
arrl = 0.1
arrL = (1.0-2*arrl)*w
arrw = 0.005
for y in range(anx):
for x in range(anx):
ax = (x+0.5)*w
if arrowbools[x][y]:
arr = plt.arrow(ax,(y+arrl)*w,0,arrL,width=arrw,
head_width=5*arrw,color=(0,0,1.),length_includes_head=True)
plt.gca().add_patch(arr)
else:
arr = plt.arrow(ax,(y+1-arrl)*w,0,-arrL,width=arrw,
head_width=5*arrw,color=(0,0,1.),length_includes_head=True)
# arr = plt.arrow(x+0.5*w,y+0.95*w,x+0.5*w,y+0.05*w,
# width=arrw,head_width=3*arrw,color=(0,0,0.75))
plt.gca().add_patch(arr)
In [23]:
anx = 10
# line
# y = 5x - 2.5
arrowbools = np.zeros(shape=(anx,anx),dtype=bool)
for x in range(anx):
for y in range(anx):
xx = x/float(anx)
yy = y/float(anx)
if 3*xx - 1. < yy:
arrowbools[x][y] = 1
if random.uniform(0,1.) < 0.1:
arrowbools[x][y] = not arrowbools[x][y]
f,ax = plt.subplots(1,1,figsize=(8,8))
ax.set_xticks([])
ax.set_yticks([])
dx,dy = 1./anx, 1./anx
# Paint gridlines
for ix in range(anx+1):
x1 = dx*ix
y1 = 0.
y2 = 1.
x2 = x1
plotLine(x1,y1,x2,y2,c="lightgrey")
for iy in range(anx+1):
x1 = 0.
y1 = dy*iy
y2 = y1
x2 = 1.
plotLine(x1,y1,x2,y2,c="lightgrey")
plt.gca().set_axis_off()
plt.subplots_adjust(top = 1, bottom = 0, right = 1, left = 0,
hspace = 0.1, wspace = 0.1)
plt.margins(0.01,0.01)
# boundary = plt.Rectangle((0.,0.),1.,1.,angle=0.,color='k',fill=False,linewidth=5.,zorder=2);
# plt.gca().add_artist(boundary);
plotarrowmap(arrowbools,anx)
In [13]:
f.savefig("/home/michael/msc/paper/ising.eps",format="eps",dpi=1000)
In [ ]:
for x in range(12):
for y in range(12):
In [199]:
iline = 5
with open("mnist_samples.csv",'r') as fsrc:
reader = csv.reader(fsrc,delimiter=',')
ni = 0
for line in reader:
if ni < iline:
ni+=1
continue
lbl = float(line[0])
pixels = [float(p)/255. for p in line[1:]]
i = 0
for y in range(ny):
for x in range(nx):
yi = ny-y-1
imgdata[x][y] = pixels[x + y*nx]
break
f,ax = plt.subplots(1,1,figsize=(8,8))
ax.set_xticks([])
ax.set_yticks([])
dx,dy = 1./nx, 1./ny
# Paint gridlines
for ix in range(nx+1):
x1 = dx*ix
y1 = 0.
y2 = 1.
x2 = x1
plotLine(x1,y1,x2,y2,c="lightgrey")
for iy in range(ny+1):
x1 = 0.
y1 = dy*iy
y2 = y1
x2 = 1.
plotLine(x1,y1,x2,y2,c="lightgrey")
plt.gca().set_axis_off()
plt.subplots_adjust(top = 1, bottom = 0, right = 1, left = 0,
hspace = 0.1, wspace = 0.1)
plt.margins(0.01,0.01)
plotmap(imgdata)
In [202]:
f.savefig("/home/michael/msc/paper/mnist_sample.eps",dpi=1000)
In [ ]: