Python: How to Script a Bar Chart

What a lovely bar chart...

Say you have a message filled with static, and you suspect there’s something off about it. The pixels appear to be random, but not quite. How, how, how might you determine if the image is indeed entirely randomized?

A Secret Message

Here’s the image, by the way. On the right is the message hidden inside of it. I wrote a script that does that in Processing a few weeks back. Unfortunately, even though the pixels appear semi-random, the message is pretty easy to pull out if you look at the distribution of RGB values over the picture as a whole.

You could get those to print to the console in Python, but it’s far more enjoyable to look at a bar chart. That’s our task today.

Bar Chart Basics

Bar charts in Python happen with the matplotlib module, but you’ll also want numpy. If you have trouble installing, try this post. You’ll need two lists to start off. We’re going to put the colors along the X axis, and their frequency in the image along the Y.

Bar Code Example in Python

There are a number of arguments you can add to customize the chart here. The simplest rendering of the above code will result in this bar chart.

Bar Chart Example

Ok, so that in mind, let’s make a chart with the most frequently occurring colors in our static image.

Testing the Theory

Static is made of gray colors. Since gray colors have the same red, green, and blue values all canceling each other out, I’m condensing that into a single number, which is why it’s represented as rgbvalues.append(w[1]) below.

Keep in mind, rgbvalues and occ are both lists. Those are populated from the dictionary containing the colors as keys, and the number of times that color appears in the image as values. Since we only want the most frequently occurring colors, we’re sorting that dictionary by value, and then appending the first 20 to the rgbvalues and occ lists.

Color Bar Chart

And voila. As you might expect in a randomized image, 19 of those colors appear approximately the same number of times.

Pixel Color Distribution

The gray value at 100 is the one with the message. More coming on this soon.

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: