Let me start by showing you the following graph:
It looks familiar, doesn't it? This is called a bar graph. We normally draw them using different applications like Microsoft Excel and Microsoft Word.
If we want to have more control over the process of graph creation, programmatically creating such graphs would be the best choice.
In this tutorial I will show you how we can create bar graphs using Python. Ready?
If you're interested in digging deeper into Python and learning how to use of the power of Python to handle data, why not check out these two courses:
What Modules Do We Need?
In order to carry out the task of drawing a bar graph, we will mainly need two modules: the Image module and the ImageDraw module, both of which will be imported from the Python Imaging Library (PIL). The
Image module will be used to load an image, while the
ImageDraw module will be used to create the 2D graphics (i.e. draw a line).
Mentioning PIL, let me show you how we can install it. But I will be showing you how to install Pillow instead. The reason for that is the most recent version for PIL, PIL 1.1.7, was released in 2009 and supports only Python 1.5.2–2.7. It doesn't thus have support for Python 3.X, and I'm afraid that its development has been discontinued since the last commit for the PIL project was in 2011.
Pillow here comes to the rescue as it has forked the PIL repository and added Python 3.X support. So I will be using Pillow for the sake of readers who are using Python 3.X. But it doesn't hurt to work with Pillow if you are using previous Python versions.
The steps mentioned in this section about installing Pillow are for Mac OS X users, since I'm currently writing this tutorial on a Mac OS X El Capitan system, but you can follow the instructions in the documentation for other operating systems.
An easy way to install Pillow on your Mac is through pip, which can be installed by typing the following simple command on your terminal:
$ sudo easy_install pip
Pillow at this point can be simply installed by typing:
$ sudo pip install pillow
Building the Program
Let's now walk through the building blocks of the program step by step. The first thing we need is a blank image on which we will be drawing our bar graph. You can download the blank image.
Let's now read this blank image and draw it. We can do this as follows:
from PIL import Image, ImageDraw img = Image.open('blank.png') draw_img = ImageDraw.Draw(img)
Since we want to draw a bar graph, we need some data. For this, we can use lists. Thus, our data (list) can look as follows:
data = ['4','5','87','1','44','83','93','2','54','84','100','64']
At this point, all we need to do is draw the bar graph. We will treat the bars we see on the graph as lines. So we will make use of the
line() method of the
I will show you the code that will perform the task of drawing a bar graph, and I will explain it afterwards:
for i in data: x = x + 30 y = 200 - int(i) draw_img.line((x,200,x,y), width=10, fill=(255,0,0,255))
As you can see, we loop through our list and draw a bar graph using the data in the list.
x = x + 30 provides us with the required space between each data point in the list on the x-axis. Before I proceed, I want to remind you that the
(0,0) point on the image is the top left point of the axis. So it would be like drawing the lines upside down to the normal way we are used to when marking the point coordinates on a graph.
If we jump to the last line of the previous script portion, we can read this part
draw_img.line((x,200, x,y) as follows: draw a line from the point
(x,200) to the point
(x,y). So, if we start with the first data point
4, the line would be drawn from
(34,196). Thus, when I show you the output in a while, the x-axis will appear as if it was what we are normally used to (starting from the left bottom for the point
(0,0)). The trick here was to use
y = 200. As you will see, we will always have a line drawn where
y = 200 for all the data points, and this will give us the impression of the way we used to draw points on a graph when viewing our result.
width represents the width (thickness) of the line, and
fill=(255,0,0,255) represents the color of the line. That is, the
RGBA color (an extension of the RGB color with an Alpha channel that represents the opacity).
Finally, we can view the image using the statement:
Putting It All Together
Now that we have covered the building blocks of our program, let's put everything together and see how our script looks:
from PIL import Image, ImageDraw img = Image.open('blank.png') draw_img = ImageDraw.Draw(img) data = ['4','5','87','1','44','83','93','2','54','84','100','64'] x = 0 for i in data: x = x + 30 y = 200 - int(i) draw_img.line((x,200,x,y), width=10, fill=(255,0,0,255)) img.show()
Now comes the interesting part, the result of the above script. If you run the program, you should have something similar to the following:
I have cropped the top part of the image in this snapshot; it should look larger in the original output.
As we saw from this tutorial, in order to gain more control over some regular tasks like drawing a graph for some data, using a programming language (i.e. Python) would be the way to go.
We don't, however, always need to perform such tasks from scratch; we can instead benefit from user-defined libraries such as Pillow in our tutorial. Using such a library made it easy for us to draw the bar graph, and using Python gave us the control and flexibility of using some tricks to output the graph in the way we wanted.
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.Update me weekly
Envato Tuts+ tutorials are translated into other languages by our community members—you can be involved too!Translate this post