Graph visualisation basics with Python Part I: Flowcharts

Creating flowchart using the SchemDraw package

Himalaya Bir Shrestha
Towards Data Science

--

Recently, I was assigned the task of creating a logic tree diagram to represent a problem-solving process at my work. In the logic tree, I had to represent various equations to solve the problem. Apparently, I could create a logic tree using programs such as MS PowerPoint or Paint. But using these programs would be a manual process, which could result in some inconsistencies with respect to shape and size in the visualisation. Moreover, modifying such plots manually could be a tedious process.

I wanted to automate this process. So I started by exploring the possibility of creating a logic tree with Python. I came across different packages that could serve my purpose, such as networkx and graphviz. I wanted to explore further possibilities with Python to represent the problem-solving processes. In this series, I am going to share my findings regarding the different possibilities of graph visualisation using Python. In the first part of the series, I am going to share a technique I figured out to create a flowchart in Python using the SchemDraw package. In the subsequent parts, I am going to share the ways to visualise tree structures such as logic trees, and organograms using other packages. Let’s get started.

Image by Kelly Sikkema from Unsplash

Virtual environment for this project

I start by creating a virtual environment named graphs for this project. Python virtual environment creates an isolated environment for projects. This implies that each project has its own dependencies (Real Python, 2018). Using a virtual environment avoids installing the Python packages globally which could break system tools or other projects (PyPA, 2022).

To create the virtual environment, I created a yml file as shown below. It consists of the list of the dependencies that are required for this project.

Environment file to create a virtual environment named graphs.

To create the virtual environment named graphs using the given yml file, I run the following in the terminal:

conda env create -n graphs --file graphs-environment.yml

Once the environment is created, I activate it using:

conda activate graphs

It is also possible to create a unique kernel in jupyter notebook for this virtual environment, which I created using the following command in the terminal:

python -m ipykernel install --user --name graphs --display-name "graphs"

Once the kernel is installed, then to run a notebook in the given environment, I simply go to Kernel, Change kernel, and select graphs. If the environment is not required anymore, it can also be removed easily using:

conda env remove -n graphs

Flowchart

A flowchart is a picture that represents the different steps in a process in sequential order. Flowcharts could be of different types but their primary purpose is to reflect the flow of the process to solve a problem or achieve an objective. I recall in my high school learning about flowcharts for the first time in computer science class. Sketching the flowchart using pen and paper to solve simple problems such as taking the sum of n numbers or printing a sequence of numbers was an interesting challenge back then.

In the following section, I am going to describe a way to create a flowchart in Python using the SchemDraw package. I am going to consider an example of a problem to detect whether a string is a palindrome or not.

Elements of a flowchart

The SchemDraw package, developed by Colling J. Delker, allows for the creation of high-quality electrical circuit schematic diagrams. However, I found that this package could also be utilised to create customised flowcharts.

There are six main elements in a flowchart. The start or end nodes are represented by an elliptical shape. A parallelogram represents a user-defined input while a rectangle represents a process. A diamond shape illustrates a decision-making stage in the process. The different shapes are connected by pointed arrows, also known as a connector. With SchemDraw, it is possible to construct these basic shapes in Python by importing schemdraw.Drawing() and passing the corresponding parameters and labels for each element. Moreover, this package allows the user to control the size of the elements such as width and height, as well as the direction of the arrows as shown in the screenshot below.

Constructing the basic elements of a flowchart using the SchemDraw package. Image by Author.

Flowchart to represent the palindrome problem

A palindrome is a word or sequence that is read the same forwards or backward. The algorithm for this problem is very simple. First, I ask the user to enter a string. Next, I reverse the string. If the string and its reverse are the same, then the string is a palindrome and if not, it is not a palindrome. This problem could be stated in Python is just a few lines of code as shown below:

string = input(“Enter a string: “)reverse_string = string[::-1]if string == reverse_string:
print (f”{string} is a palindrome.”)

else:
print (f”{string} is not a palindrome.”)

The code to get the flowchart for this problem using SchemDraw is given in the gist below. It starts with a start node, followed by an input box to enter the string. Next, there is a process box to reverse the string, and a diamond box to check whether the string and its reverse are the same. There are two different arrows diverging out of the decision box based on whether the decision is True or False. After printing the result, the arrows ultimately culminate at the end node.

As a result, I get the flowchart as shown below, which can also be saved as an image file.

Flowchart to solve the palindrome problem created using SchemDraw. Image by Author.

Conclusion

Graph visualisation is an interesting concept to represent a network, process flow such as a supply chain or a problem-solving process, tree structures such as decision tree, organisational tree, logic tree, and folder tree. While there are sophisticated packages available for data visualisation in Python such as matplotlib, seaborn, Bokeh, Plotly, etc., there are some packages existing to represent graphs and networks in Python although they are not as popular.

In this post, I started by creating a virtual environment for this project. And then I explained a methodology to create simple flowcharts in Python using the SchemDraw package. In the subsequent parts of this series, I am going to share some ways I figured out to represent tree structures such as organograms and logic trees using packages such as networkx and graphviz.

Thank you for reading!

References

PyPA, 2022. Installing packages using pip and virtual environments.

Real Python, 2018. Python Virtual Environments: A Primer.

--

--

I write about the intersection of data science with sustainability in simple words. Views reflected are of my own, and don’t reflect that of my employer.