Article 4SZB9 Drawing with Unicode block characters

Drawing with Unicode block characters

by
John
from John D. Cook on (#4SZB9)

My previous post contained the image below.

gauss_sierpinski.png

The point of the post was that the numbers that came up in yet another post made the fractal image above when you wrote them in binary. Here's the trick I used to make that image.

To make the pattern of 0's and 1's easier to see, I wanted to make a graphic with black and white squares instead of the characters 0 and 1. I thought about writing a program to create an image using strings of 0's and 1's as input, but then I thought of something more direct.

The Unicode character U+2588 is just a solid rectangle. I replaced the 1's with U+2588 and replaced the 0's with spaces. So I made a text file that looked like the the image above, and took a screen shot of it. The only problem was that the image was rectangular. I thought the pattern would be easier to see if it were a square, so I changed the aspect ratio on the image.

Here's another example using Unicode block elements, this time to make a little bar graph, sorta like Edward Tufte's idea of a sparkline. The characters U+2581 through U+2588 produce bars of increasing height.

sparkline.png

To create images like this, your font needs to include glyphs for Unicode block elements. The font needs to be monospaced as well so the letters will line up under the blocks. The example above was created using the APL385. It also looks good in Hack and Input Mono. In some fonts the block characters don't align consistently on the baseline.

Here's the Python code that produced the above graph of English letter frequencies.

# Letter frequencies via# http://www.norvig.com/mayzner.htmlfreq = [ 8.04, 1.48, 3.34, 3.82, 12.49, 2.40, 1.87, 5.05, 7.57, 0.16, 0.54, 4.07, 2.51, 7.23, 7.64, 2.14, 0.12, 6.28, 6.51, 9.28, 2.73, 1.05, 1.68, 0.23, 1.66, 0.09,]m = max(freq)for i in range(26): u = int(8*freq[i]/m) ch = chr(0x2580+u) if u > 0 else " " print(ch, end="")print()for i in range(26): print(chr(0x41+i), end="")
3QivgFUPOLc
External Content
Source RSS or Atom Feed
Feed Location http://feeds.feedburner.com/TheEndeavour?format=xml
Feed Title John D. Cook
Feed Link https://www.johndcook.com/blog
Reply 0 comments