O Python a seguir foi projetado para gerar um ERD usando o Visual Studio Code.
O gráfico é criado localmente com matplotlib. O código executa sem erros, no entanto, o diagrama ERD mostra em branco.
O código python é o seguinte:
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()
Alguém pode me dizer por que o ERD não aparece?
Ao contrário da maioria dos artistas do Matplotlib, texto e anotações não atualizam automaticamente os limites de dados dos eixos. Seu diagrama está sendo desenhado, mas fora dos eixos. Você pode atualizar os limites dos eixos manualmente com