Skip to main content

Visualize Tree, Graph, and Matrix data structures with ease.

Project description

Data Structure Plot (DSPlot)

Build Status Coverage Status GitHub license

DSPlot is a tool to simply visualize tree and graph data structures by serving as a Pythonic interface to the Graphviz layout. DSPlot allows you to easily draw trees, graphs (both directed and undirected), and matrices by passing data in primitive form and directly output an image.

⬇ Installation

0. Prerequisites

  • Python 3.7 or later
  • pip
  • virtualenv

1. Install Graphviz

  • MacOS:
brew install graphviz
  • Linux:
apt-get install graphviz libgraphviz-dev

2. Install package

$ pip install dsplot

🤟 Usage

  • Binary Tree:
from dsplot.tree import BinaryTree

tree = BinaryTree(nodes=[5, 4, 8, 11, None, 13, 4, 7, 2, None, None, 5, 1])
tree.plot()

tree

  • Graph:
from dsplot.graph import Graph

graph = Graph(
    {0: [1, 4, 5], 1: [3, 4], 2: [1], 3: [2, 4], 4: [], 5: []}, directed=True
)
graph.plot()

directed

from dsplot.graph import Graph

graph = Graph(
    {1: [2, 4], 2: [1, 3], 3: [2, 4, 5], 4: [1, 3], 5: [3, 6, 7], 6: [5], 7: [5]}, directed=False
)
graph.plot()

undirected

  • Matrix:
from dsplot.matrix import Matrix

matrix = Matrix([[1, 2, 3], [4, 5, 6], [1, 2, 6]])
matrix.plot()

matrix

  • Customization:
    You can customize the border color, shape, style, and fill color of the nodes, and the orientation (left to right - LR, top to bottom - TB) of the graph.
from dsplot.graph import Graph

graph = Graph(
    {0: [1, 4, 5], 1: [3, 4], 2: [1], 3: [2, 4], 4: [], 5: []}, directed=True
)
graph.plot(fill_color='#aec6cf')

colored

from dsplot.tree import BinaryTree

tree = BinaryTree(nodes=[5, 4, 8, 11, None, 13, 4, 7, 2, None, None, 5, 1])
tree.plot(orientation='LR', border_color='#FFCE30', fill_color='#aec6cf')

colored

  • Edge values for Graphs:
    For edge values, str and int data types are supported at the moment.
from dsplot.graph import Graph

graph = Graph(
    {0: [1, 4, 5], 1: [3, 4], 2: [1], 3: [2, 4], 4: [], 5: []},
    directed=True,
    edges={'01': 1, '04': 4, '05': 5, '13': 3, '14': 4, '21': 2, '32': 3, '34': 4},
)
graph.plot()

edge

🎁 Additional features

1. Tree traversals:

from dsplot.tree import BinaryTree

tree = BinaryTree(nodes=[5, 4, 8, 11, None, 13, 4, 7, 2, None, None, 5, 1])

print(tree.preorder())
# [5, 4, 11, 7, 2, 8, 13, 4, 5, 1]

print(tree.inorder())
# [7, 11, 2, 4, 5, 13, 8, 5, 4, 1]

print(tree.postorder())
# [7, 2, 11, 4, 13, 5, 1, 4, 8, 5]

2. Graph traversals:

from dsplot.graph import Graph

graph = Graph(
    {0: [1, 4, 5], 1: [3, 4], 2: [1], 3: [2, 4], 4: [], 5: []}, directed=True
)

print(graph.bfs())
# [0, 1, 4, 5, 3, 2]

print(graph.dfs())
# [0, 1, 3, 2, 4, 5]

📄 License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dsplot-0.9.0.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

dsplot-0.9.0-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file dsplot-0.9.0.tar.gz.

File metadata

  • Download URL: dsplot-0.9.0.tar.gz
  • Upload date:
  • Size: 7.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.10.2 Darwin/22.2.0

File hashes

Hashes for dsplot-0.9.0.tar.gz
Algorithm Hash digest
SHA256 6b6e1ee8e74908c45726715a69a7ba07ff2d517b8a73d8937887e311d2ecbf89
MD5 91ffbd3d109bf5c9f89f208b98078c66
BLAKE2b-256 36205db681dcb9db7f80504c3320c62c28a885446a65ba4d6671a41adcf5626e

See more details on using hashes here.

File details

Details for the file dsplot-0.9.0-py3-none-any.whl.

File metadata

  • Download URL: dsplot-0.9.0-py3-none-any.whl
  • Upload date:
  • Size: 8.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.10.2 Darwin/22.2.0

File hashes

Hashes for dsplot-0.9.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0c7349196d53982c220d29e0c0dc2993e750730c8df12668aaf60aa249889ecf
MD5 3633a7f3ca6dfb03396b93e5556ec49d
BLAKE2b-256 9ff1495c03b0e47a8ac7ce42432a8b81a519befdd4245038a1ef65a1cb48e9b2

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page