In this blog post, I will share with you how to create North Indian-style Vedic astrology charts using Python. The tools I will be using will be the svgwrite library, a powerful resource that enables us to craft SVG (Scalable Vector Graphics) files programmatically. Let’s start.
Imports
The Python libraries used in our code are:
- svgwrite: This library provides an interface for creating SVG files.
- math: This library provides mathematical functions.
- random: This library is used to generate random numbers.
Code
The code begins by importing the necessary libraries:
import svgwrite
from svgwrite import cm, mm
import math
import random
Next, this creates a new SVG document using svgwrite.Drawing. The size of the drawing is set to 100% of the viewport, and the SVG profile is set to ‘full’, which allows for the most features:
dwg = svgwrite.Drawing('astrology_chart.svg', size=("100%", "100%"), profile='full')
This would set the width and height of the SVG to ‘100%’, which means the SVG will take up 100% of the width and height of its container. The viewBox attribute is used to specify the aspect ratio and coordinate system of the SVG. In this case, we set it to ‘0 0 800 600’, which means the width and height of the SVG’s coordinate system are 100 units.
# Set the size of the SVG to a percentage of the viewport
dwg.attribs['width'] = '100%'
dwg.attribs['height'] = '100%'
dwg.attribs['viewBox'] = '0 0 800 600'
This would define a linear gradient and add it to the defs section of the SVG. This gradient will be used to fill the polygons that represent the houses in the chart:
gradient = svgwrite.gradients.LinearGradient(start=(0, 0), end=(0, 1), id="grad")
gradient.add_stop_color(0, 'white')
gradient.add_stop_color(1, '#f0f3bf')
dwg.defs.add(gradient)
The houses in the chart are represented as polygons. The points for each polygon are defined, and then the polygon is created and added to the SVG drawing:
points_h1 = [(100,225), (200,300), (300,225), (200,150)]
h1 = svgwrite.shapes.Polygon(points_h1, fill="url(#grad)")
dwg.add(h1)
This process is repeated for each of the 12 houses. The centers of the polygons (houses) are defined in the centers list:
centers = [
(190, 75), # 1st house
(100, 30), # 2nd house
(30, 75), # 3rd house
(90, 150), # 4th house
(30, 225), # 5th house
(90, 278), # 6th house
(190, 225), # 7th house
(290, 278), # 8th house
(360, 225), # 9th house
(290, 150), # 10th house
(360, 75), # 11th house
(290, 30), # 12th house
]
Each tuple in the list represents the (x, y) coordinates of the center of a house. The coordinates are defined in the SVG’s coordinate system. The placeholders for the planets are generated in a circular arrangement around the center of each house. This is done in the following part of the code: Next, we randomly assign each planet to a house. The planets are represented as text elements, which are placed in a circular arrangement within each house:
for i, center in enumerate(centers):
house_planets = houses[i]
for j, planet in enumerate(house_planets):
angle = 2 * math.pi * j / len(house_planets)
x = center[0] + radius * math.cos(angle)
y = center[1] + radius * math.sin(angle)
color = colors[planets.index(planet)]
dwg.add(dwg.text(planet[:2], insert=(x, y), font_size='11px', fill=color))
In this part of the code, I first iterated over the list of centers, which are the center points of each house. For each house, i would get the list of planets assigned to that house. Then i iterated over this list of planets. For each planet, i calculated an angle based on the index of the planet in the list and the total number of planets in the house. This angle is used to calculate the x and y coordinates of the planet’s position within the house. The position is calculated such that the planets are arranged in a circular pattern around the center of the house. Finally, the SVG document is saved:
dwg.save()
After running the Python script, an SVG file named ‘astrology_chart.svg’ will be generated. This file contains a graphical representation of the North Indian style Vedic astrology chart. The chart consists of 12 houses, each represented by a polygon. Each house contains one or more planets, represented by text elements. The planets are arranged in predefined placeholder within each house. The size and position of the elements in the chart are calculated dynamically, so the chart can be easily customized by modifying the code. Open the SVG file in a web browser or image viewer to see the generated chart. The chart should look similar to the image provided below.You can find the complete code for generating the North Indian style Vedic astrology chart on my GitHub GIST. Feel free to clone, and explore the code in more detail. If you have any questions or suggestions, don’t hesitate to post. Happy coding!
Additional Details
The colors of the planets are defined in the colors list. Each color corresponds to a planet in the planets list. The colors are then used to fill the text elements that represent the planets. The houses are randomly generated for testing purposes. In a real-world application, i would replace this with the actual house data.
Possible Improvements
While this code serves its purpose, there are several ways it could be improved:
- Modularization: The code could be broken down into functions to improve readability and reusability.
- Error Handling: The code currently does not handle any errors or exceptions. Adding error handling would make the code more robust.
- Customization: The code could be modified to allow for more customization, such as different color schemes or chart styles.
0 Post a Comment:
Post a Comment