Search code examples
pythonmatplotlib

Unable to generate ERD diagram with Python code


The following Python is designed to generate an ERD using Visual Studio Code.

The chart is to created locally with matplotlib. The code executes without any errors, however the ERD diagram shows blank.

The python code is as follows:

import matplotlib.pyplot as plt

# Define the entities and their attributes for the ERD
entities = {
    "Customer": ["CustomerID (PK)", "CustomerName", "ContactInfo"],
    "CreditCardAccount": ["AccountID (PK)", "AccountStatus", "Balance", "CustomerID (FK)"],
    "CreditCard": ["CardID (PK)", "CardNumber", "ExpiryDate", "AccountID (FK)", "BrandID (FK)"],
    "CreditCardBrand": ["BrandID (PK)", "BrandName", "CardType"],
    "SecondaryCardHolder": ["SecondaryHolderID (PK)", "HolderName", "RelationToPrimary", "AccountID (FK)"],
    "PurchaseTransaction": ["TransactionID (PK)", "TransactionDate", "Amount", "CardID (FK)", "RetailerID (FK)"],
    "Retailer": ["RetailerID (PK)", "RetailerName", "Location"],
    "MonthlyStatement": ["StatementID (PK)", "StatementDate", "OutstandingBalance", "AccountID (FK)"],
    "CustomerServiceInteraction": ["InteractionID (PK)", "InteractionDate", "Notes", "CustomerID (FK)"],
}

# Relationships between entities
relationships = [
    ("Customer", "CreditCardAccount", "1:M"),
    ("CreditCardAccount", "CreditCard", "1:M"),
    ("CreditCard", "CreditCardBrand", "M:1"),
    ("CreditCardAccount", "SecondaryCardHolder", "1:M"),
    ("CreditCard", "PurchaseTransaction", "1:M"),
    ("PurchaseTransaction", "Retailer", "M:1"),
    ("CreditCardAccount", "MonthlyStatement", "1:M"),
    ("Customer", "CustomerServiceInteraction", "1:M"),
]

# Plotting the ERD
fig, ax = plt.subplots(figsize=(12, 8))

# Define positions for the entities
positions = {
    "Customer": (1, 5),
    "CreditCardAccount": (4, 5),
    "CreditCard": (7, 5),
    "CreditCardBrand": (10, 5),
    "SecondaryCardHolder": (4, 3),
    "PurchaseTransaction": (7, 3),
    "Retailer": (10, 3),
    "MonthlyStatement": (4, 1),
    "CustomerServiceInteraction": (1, 3),
}

# Draw entities as boxes
for entity, position in positions.items():
    plt.text(position[0], position[1], f"{entity}\n" + "\n".join(entities[entity]),
             ha='center', va='center', bbox=dict(facecolor='lightblue', edgecolor='black', boxstyle='round,pad=0.5'))

# Draw relationships as lines
for rel in relationships:
    start_pos = positions[rel[0]]
    end_pos = positions[rel[1]]
    ax.annotate("",
                xy=end_pos, xycoords='data',
                xytext=start_pos, textcoords='data',
                arrowprops=dict(arrowstyle="->", lw=1.5, color='black'),
                )
    # Add cardinality
    midpoint = ((start_pos[0] + end_pos[0]) / 2, (start_pos[1] + end_pos[1]) / 2)
    ax.text(midpoint[0], midpoint[1], rel[2], ha='center', va='center', fontsize=10)

# Hide axes
ax.set_axis_off()

# Show the ERD diagram
plt.title("Entity Relationship Diagram (ERD) for Credit Card Company", fontsize=16)
plt.show()

The output is as follows: enter image description here

Can someone let me know where why the ERD won't appear?


Solution

  • Unlike most Matplotlib artists, text and annotations do not automatically update the data limits of the axes. Your diagram is being drawn, but outside of the axes. You can update the axes limits manually with

    ax.set_xlim(0, 10)
    ax.set_ylim(0, 6)
    

    enter image description here