Skip to content

Instantly share code, notes, and snippets.

@maedoc
Created January 17, 2014 09:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maedoc/8470563 to your computer and use it in GitHub Desktop.
Save maedoc/8470563 to your computer and use it in GitHub Desktop.
`show()` a matplotlib figure in ASCII
# coding: utf-8
import PIL
import tempfile
def show_ascii(w=200, h=50, chars=' .\',;"oO%8@#'):
from pylab import tight_layout, savefig
tight_layout()
with tempfile.NamedTemporaryFile(suffix='.png') as f:
savefig(f.name)
im = PIL.Image.open(f.name).resize((w, h)).convert("L")
print '\n'.join([''.join([chars[11-im.getpixel((j, i))/22] for j in range(im.size[0])]) for i in range(im.size[1])])
if __name__ == '__main__':
# prevent a figure from popping up
import matplotlib as mpl
mpl.use('Agg')
from pylab import *
x = r_[:12:100j]
plot(x, sin(x))
show_ascii()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment