Skip to main content

A Python library for drawing graphs from scratch.

Project description

graphD

A Python library for drawing graphs from scratch. This library implements its own rendering system without relying on external drawing libraries.

Features

  • Pure Python implementation, no external dependencies
  • Custom rendering engine with a force-directed layout algorithm
  • Support for node and edge attributes (color, size, labels, etc.)
  • Outputs to PPM image format (viewable in most image viewers)
  • Built from scratch, every pixel calculated individually

Installation

pip install graphD

Usage

Basic Example

import graphD

# Create a graph
g = graphD.Graph()

# Add nodes with attributes
g.add_node('A', color='#FF0000', size=20, label='Node A')  # Red node
g.add_node('B', color='#00FF00', size=15, label='Node B')  # Green node
g.add_node('C', color='#0000FF', size=25, label='Node C')  # Blue node

# Add edges
g.add_edge('A', 'B', color='#888888')  # Gray edge
g.add_edge('B', 'C')
g.add_edge('A', 'C')

# Render the graph to an image file
image_file = graphD.plot(g, filename="my_graph.ppm")
print(f"Graph image saved to: {image_file}")

Advanced Usage

You can directly use the renderer components for more control:

from graphD import Graph, GraphRenderer, Color

# Create graph and add elements
graph = Graph()
graph.add_node('1', color='#FF0000')
graph.add_node('2', color='#00FF00')
graph.add_edge('1', '2')

# Create a custom renderer
renderer = GraphRenderer(width=1200, height=900)
renderer.node_color = Color(0, 0, 255)  # Default blue for nodes
renderer.edge_color = Color(0, 0, 0)     # Default black for edges
renderer.render(graph, "custom_graph.ppm")

How It Works

The library uses a force-directed layout algorithm to position nodes in a visually appealing way:

  1. Nodes are initially arranged in a circle
  2. Repulsive forces push nodes away from each other
  3. Attractive forces pull connected nodes toward each other
  4. After several iterations, nodes settle into a balanced layout

Drawing is performed using basic algorithms:

  • Bresenham's line algorithm for edges
  • Circle drawing for nodes
  • Simple pixel-based font for labels

The output is saved in PPM (Portable Pixmap) format, which is a simple image format supported by many image viewers.

Contributing

Contributions are welcome! Please open an issue or submit a pull request.

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

graphd-0.0.1.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

graphd-0.0.1-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file graphd-0.0.1.tar.gz.

File metadata

  • Download URL: graphd-0.0.1.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for graphd-0.0.1.tar.gz
Algorithm Hash digest
SHA256 39c8f2b846ef6635d466702e18a761dd53f2007e714400acaf67c2d34ae275ee
MD5 9c085c664ddf81c6d98bc48ed39a317a
BLAKE2b-256 86fc18894ca427fad125a12f0a4db48c380b97497236dca2631b837fc006b45e

See more details on using hashes here.

File details

Details for the file graphd-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: graphd-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 8.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for graphd-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fc64a178949652bb454ae31dd9d6eca66b2550772e6d751eb6266696bab8c652
MD5 f47c8c779cdb41ee212abb59ba503c4f
BLAKE2b-256 c5102b0d5532094cd001f4642d180c220720ac27abfebbe90cd05477baa65078

See more details on using hashes here.

Supported by

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