Make XKCD-Style Plots From Python

[Randall Munroe] certainly understands the power of graphical representation of data. The humorous plots in his xkcd webcomic are one of the favorite parts for many readers. Their distinctive, Tufteian style delivers the information – in this case, a punch line – without excessive decoration. To be honest, we can’t get enough of them. A recent reddit thread reminded us that you can generate a similar look for your own data (humorous or otherwise) in Python using Matplotlib.

If you already have a plot generated with Matplotlib, activating xkcd-mode is as simple as calling a method on the pyplot object:

matplotlib.pyplot.xkcd()

The documentation recommends that you install the “Humor Sans” font for best effect. On one of our linux boxes, we were able to do this with a simple:

sudo apt-get install fonts-humor-sans

There will undoubtedly be similar incantations for other operating systems. It’s really that simple. In fact, the featured image above was generated with this minimal script:

#!/usr/bin/env python3

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 1, 100)
y = (x > 0.5) * (x - 0.5)

plt.xkcd(scale=5, length=400)
plt.xticks([])
plt.yticks([])
plt.ylabel('Downloads of "humor sans" font')
plt.text(0, 0.25, 'Article on xkcd() published')
plt.plot(x, y)
plt.plot([0.3, 0.475], [0.2, 0.025], 'black')
plt.gca().set_aspect(2*9/16)
plt.savefig('xkcd_plot.png', dpi=300)

Beyond generating humorous graphs for those with little artistic talent, these plots can also be used instead of hand-drawn sketches to indicate a simple model or expected result. The comic look of the plots conveys the idea that they don’t represent actual data, perhaps only a concept. We saw this done at one of the talks at the Hackaday SuperConference 2018.

We’ve also covered some of the xkcd comics before, such as when they subtly dissed Arduino back in 2010, before that was cool.

Blacksmithing For The Uninitiated: What is a Forge?

Blacksmiths were the high technologists of fabrication up until the industrial revolution gained momentum. At its core, this is the art and science of making any needed tool or mechanism out of metal. Are you using the correct metal? Is the tool strong where it needs to be? And how can you finish a project quickly, efficiently, and beautifully? These are lessons Blacksmiths feel in their bones and it’s well worth exploring the field yourself to appreciate the knowledge base that exists at any well-used forge.

I had an unexpected experience a few days ago at the Hacker Hotel weekend hacker camp in the Netherlands. At the side of the hotel our friends at RevSpace in The Hague had set up a portable forge. There was the evocative coal fire smell of burning coke from the hearth, an anvil, and the sound of hammering. This is intensely familiar to me, because I grew up around it. He may be retired now, but my dad is a blacksmith whose work lay mostly in high-end architectural ironwork.

Working the RevSpace forge at Hacker Hotel, in not the most appropriate clothing for the job.
Working the RevSpace forge at Hacker Hotel, in not the most appropriate clothing for the job.

The trouble is, despite all that upbringing, I don’t consider myself to be a blacksmith. Sure, I am very familiar with forge work and can bash metal with the best of them, but I know blacksmiths. I can’t do everything my dad could, and there are people we’d encounter who are artists with metal. They can bend and shape it to their will in the way I can mould words or casually solder a tiny surface-mount component, and produce beautiful things in doing so. My enthusiastic metal-bashing may bear the mark of some experience at the anvil but I am not one of them.

It was a bit of a surprise then to see the RevSpace forge, and I found myself borrowing a blacksmith’s apron to protect my smart officewear and grabbing a bit of rebar. I set to and made a pretty simple standard of the dilletante blacksmith, a poker with a ring on one end. Hammer one end of the rebar down to a point, square off the other end for just over 3 times the diameter of the ring, then bend a right angle and form the ring on the pointy end of the anvil. Ten minutes or so of fun in the Dutch sunshine. Working a forge unexpectedly brought with it a bit of a revelation. I may not be a smith of a high standard, but I have a set of skills by virtue of my upbringing that I had to some extent ignored.

Where others might have put effort into learning them, they’re things I just know. It had perhaps never occurred to me that maybe all my friends in this community didn’t learn how to do this by hanging round the forge next to the house they grew up in. If I have this knowledge merely by virtue of my upbringing, perhaps I should share some of it in a series of articles for those in our community who’ve always fancied a go at a forge but have no idea where to start.

Continue reading “Blacksmithing For The Uninitiated: What is a Forge?”

Yes, You Can Put IoT on the Blockchain using Python and the ESP8266

Last year, we saw quite a bit of media attention paid to blockchain startups. They raised money from the public, then most of them vanished without a trace (or product). Ethics and legality of their fundraising model aside, a few of the ideas they presented might be worth revisiting one day.

One idea in particular that I’ve struggled with is the synthesis of IoT and blockchain technology. Usually when presented with a product or technology, I can comprehend how and/or why someone would use it – in this case I understand neither, and it’s been nagging at me from some quiet but irrepressible corner of my mind.

The typical IoT networks I’ve seen collect data using cheap and low-power devices, and transmit it to a central service without more effort spent on security than needed (and sometimes much less). On the other hand, blockchains tend to be an expensive way to store data, require a fair amount of local storage and processing power to fully interact with them, and generally involve the careful use of public-private key encryption.

I can see some edge cases where it would be useful, for example securely setting the state of some large network of state machines – sort of like a more complex version of this system that controls a single LED via Ethereum smart contract.

What I believe isn’t important though, perhaps I just lack imagination – so lets build it anyway.

Continue reading “Yes, You Can Put IoT on the Blockchain using Python and the ESP8266”

Computer Algebra for Electronic Design

Don’t get me wrong. Like most people, there’s nothing I enjoy more than solving a long, involved math problem by hand. But, sometimes, a few pages of algebraic scratches on paper is just a means to an end. I find this especially true during electronic design sessions, be it circuit design or PCB layout; I just need the answer, and any time spent finding it distracts me from the larger task at hand. For me, at least, this seems to happen at least once a week, and about five years ago I decided to do something about it. I had heard of computer algebra packages, of course, but they weren’t taught as part of the undergraduate engineering curriculum when I went to school. So, I set about learning one: let the computers do the math!

The package I chose is wxMaxima, a document-based front-end to the Maxima computer-algebra system. Descended from code originally written in the late 1960s, it’s a general-purpose package supporting symbolic computation for algebra and calculus. There’s solid, mature code underneath with a modern UI veneer on top. Plus, it’s FOSS.

As I’ve progressed, I’ve found that some additional functions make the Maxima environment especially convenient for circuit design. A few are simple enough that I’d typically just re-create them as needed, so I never really got organized – there were several versions of my “library” floating around on various machines. I finally got my act together, cleaned up the most-frequently used functions, and put them into a GitHub repo.

Let’s have a look at how we can use them to take the tedium out of math for some design problems.

Continue reading “Computer Algebra for Electronic Design”

Multiple OLEDs? Save Pins By Sharing the I2C Clock

Inexpensive OLED displays with I2C interfaces abound, but there is a catch: they tend to be stuck on I2C address 0x3C. Some have a jumper or solder pads to select an alternate (usually 0x3D), but they lack any other method. Since an I2C bus expects every device to have a unique address, this limits the number of displays per bus to one (or two, at best.) That is all still true, but what [Larry Bank] discovered is a way to get multiple OLED displays working with considerably fewer microcontroller pins than usually needed.

While bit-banging I2C to host one display per bus on the same microcontroller, an idea occurred to him. The I2C start signal requires both clock (SCL) and data (SDA) to be brought low together, but what would happen if the displays shared a single clock line? To be clear, each OLED would — logically speaking — still be on its own I2C bus with its own data line, but they would share a clock signal. Would a shared clock cause attached devices to activate unintentionally?

A quick test consisting of four OLED displays (all with address 0x3C) showed that it was indeed possible to address each display with no interference if they shared a clock. Those four individually controlled displays needed only five I/O lines (four SDA, one shared SCL) instead of eight. The Multi_OLED library is available on GitHub, and in case it is useful for devices other than OLED displays, bit-banged I2C with support for shared clock lines is available separately.

There’s more to do with OLEDs than get clever with signals: check out these slick number-change animations, and that even looks to be a project that could benefit from a few saved GPIO pins, since it uses one small display per digit.

Building A Simple Python API for Internet of Things Gadgets

It’s no secret that I rather enjoy connecting things to the Internet for fun and profit. One of the tricks I’ve learned along the way is to spin up simple APIs that can be used when prototyping a project. It’s easy to do, and simple to understand so I’m happy to share what has worked for me, using Web2Py as the example (with guest appearances from ESP8266 and NodeMCU).

Barring the times I’m just being silly, there are two reasons I might do this. Most commonly I’ll need to collect data from a device, typically to be stored for later analysis but occasionally to trigger some action on a server in the cloud. Less commonly, I’ll need a device to change its behavior based on instructions received via the Internet.

Etherscan is an example of an API that saves me a lot of work, letting me pull data from Ethereum using a variety of devices.

In the former case, my first option has always been to use IoT frameworks like Thingsboard or Ubidots to receive and display data. They have the advantage of being easy to use, and have rich features. They can even react to data and send instruction back to devices. In the latter case, I usually find myself using an application programming interface (API) – some service open on the Internet that my device can easily request data from, for example the weather, blockchain transactions, or new email notifications.

Occasionally, I end up with a type of data that requires processing or is not well structured for storage on these services, or else I need a device to request data that is private or that no one is presently offering. Most commonly, I need to change some parameter in a few connected devices without the trouble of finding them, opening all the cases, and reprogramming them all.

At these times it’s useful to be able to build simple, short-lived services that fill in these gaps during prototyping. Far from being a secure or consumer-ready product, we just need something we can try out to see if an idea is worth developing further. There are many valid ways to do this, but my first choice is Web2Py, a relatively easy to use open-source framework for developing web applications in Python. It supports both Python 2.7 and 3.0, although we’ll be using Python 3 today.

Continue reading “Building A Simple Python API for Internet of Things Gadgets”

How To Make Bisected Pine Cones Look Great, Step-by-Step

[Black Beard Projects] sealed some pine cones in colored resin, then cut them in half and polished them up. The results look great, but what’s really good about this project is that it clearly demonstrates the necessary steps and techniques from beginning to end. He even employs some homemade equipment, to boot.

Briefly, the process is to first bake the pine cones to remove any moisture. Then they get coated in a heat-activated resin for stabilizing, which is a process that infuses and pre-seals the pine cones for better casting results. The prepped pine cones go into molds, clear resin is mixed with coloring and poured in. The resin cures inside a pressure chamber, which helps ensure that it gets into every nook and cranny while also causing any small air bubbles introduced during mixing and pouring to shrink so small that they can’t really be seen. After that is cutting, then sanding and polishing. It’s an excellent overview of the entire process.

The video (which is embedded below) also has an outstanding depth of information in the details section. Not only is there an overview of the process and links to related information, but there’s a complete time-coded index to every action taken in the entire video. Now that’s some attention to detail.

Continue reading “How To Make Bisected Pine Cones Look Great, Step-by-Step”