Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
September 9, 2016 01:00 pm

List to Graph: How to Represent Your List as a Graph In Python

Let me start by showing you the following graph:

Example Bar 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?

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.

Installing Pillow

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:

Pillow at this point can be simply installed by typing:

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:

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:

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 ImageDraw module.

I will show you the code that will perform the task of drawing a bar graph, and I will explain it afterwards:

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,200) to (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.

The 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: img.show().

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:

Output

Now comes the interesting part, the result of the above script. If you run the program, you should have something similar to the following:

Viewing a graph from the list

I have cropped the top part of the image in this snapshot; it should look larger in the original output.

Conclusion

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.


Original Link:

Share this article:    Share on Facebook
No Article Link

TutsPlus - Code

Tuts+ is a site aimed at web developers and designers offering tutorials and articles on technologies, skills and techniques to improve how you design and build websites.

More About this Source Visit TutsPlus - Code