Advertisement

# List to Graph: How to Represent Your List as a Graph in Python

Read Time: 5 min

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 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 the power of Python to handle data, why not check out these two courses:

## Using Pillow

To draw the bar graph using Pillow, we will mainly need two modules: the Image module and the ImageDraw module, both of which will be imported from Pillow (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).

### Installing Pillow

It's recommended to install Python packages and libraries in a virtual environment. Create and activate a virtual environment, and install Pillow with pip.

 1 python -m venv env  2 source env/bin/activate  3 python -m pip install Pillow 

On Windows, use .\env\Scripts\activate to activate your virtual environment.

## 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:

 1 from PIL import Image, ImageDraw  2 img = Image.open('blank.png')  3 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 (a list) can look as follows:

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

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

 1 for i in data:  2  x = x + 30  3  y = 200 - int(i)  4  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,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:

 1 from PIL import Image, ImageDraw  2 img = Image.open('blank.png')  3 draw_img = ImageDraw.Draw(img)  4 data = ['4','5','87','1','44','83','93','2','54','84','100','64']  5 x = 0  6 for i in data:  7  x = x + 30  8  y = 200 - int(i)  9  draw_img.line((x,200,x,y), width=10, fill=(255,0,0,255))  10   11 img.show() 

## 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:

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

### Using Matplotlib

Matplotlib is a visualization library that makes it easy to create visualizations in Python. To get started, first install Matplotlib using pip in your virtual environment.

 1 pip install matplotlib 

Import the pyplot submodule under the alias plt.

 1 import matplotlib.pyplot as plt 

We will use the same data we used above. Convert the data from strings to integers.

 1 data = list(map(int, data)) 

Specify the x-coordinates as a sequence of the points in the data list.

 1 x = range(len(data)) 

Then, use Matplotlib's bar() function to draw a bar graph with x-coordinates on the x-axis and the data on the y-axis, and display the graph.

 1 plt.bar(x,data)  2 plt.ylabel('y-axis')  3 plt.show() 

Putting it all together, we have this code:

 1 import matplotlib.pyplot as pl  2 3 data = ['4','5','87','1','44','83','93','2','54','84','100','64']  4 5 data = list(map(int, data))  6 7 x = range(len(data))  8 plt.bar(x,data)  9 plt.ylabel('y-axis')  10 plt.show() 

And here is the resulting bar graph:

## Conclusion

As we saw from this tutorial, to gain more control over 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 and Matplotlib in our tutorial.

Other libraries you can use include Seaborn, Plotly, and Bokeh. 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.

Advertisement
Did you find this post useful?
Want a weekly email summary?
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.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.