Better Programming

Advice for programmers.

Visualize Trees and Graphs in Seconds With DSPlot

Bill Tran
3 min readJan 11, 2022

--

Source

Introducing DSPlot, a package that draws beautiful graphs for you.

It was a hot summer morning. I was grinding LeetCode problems in preparation for possible upcoming internship interviews in software engineering when I bumped into a problem that looks like this:

Source: LeetCode

Input for this problem is a tree, which is given in test cases as an adjacency list: [1,2,2,3,4,4,3] .

Looking at this list does not give us much of a clear understanding of the input, but its visualized form surely does.

Therefore, I thought that some kind of engine which could transform the list representation of a tree or graph to an image would be good to have.

So I decided to write DSPlot, which stands for Data Structures Plotting, a Python package that can draw and render PNG image of trees, graphs, and matrices given in the form of adjacency lists/maps.

How DSPlot Works

When designing this package, I have only one thing in mind: to make sure its interface is intuitive and minimize the number of user steps from an input list to a rendered image.

Installation

As a Python package, DSPlot works on almost every platform that Python does. It uses Graphviz as the engine to draw graphs, so installing Graphviz is a prerequisite.

After installing Graphviz, all you need to do to get DSPlot is:

pip install dsplot

Usage

DSPlot supports drawing trees, graphs (both directed and undirected), and matrices. Using its function plot , a PNG image of the input data structure will be rendered into the directory of your choice, with the current directory as default.

  • Tree
Result (PNG format)
  • Graph
Directed graph
Undirected graph
  • Matrix
3x3 matrix

Conclusion

I hope you find this package of good use. Per DSPlot’s open-source nature, any contribution is highly appreciated: https://github.com/billtrn/dsplot

Want to Connect With the Author?Check out my personal blog.

--

--

Bill Tran
Bill Tran

Written by Bill Tran

CS Student, Software Engineer, Writer

No responses yet