Python+Matplotlib生成ERD图无报错却显示空白的问题求助
排查Matplotlib生成空白ERD图的问题
我在VS Code中编写了Python代码,通过Matplotlib生成信用卡公司的ERD图,代码执行无报错,但生成的图表是空白的。相关代码如下:
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()
问题原因
空白图表的核心原因是Matplotlib默认的坐标轴范围未包含所有绘制元素:
- 你定义的实体位置x坐标最大到10,y坐标最大到5,但Matplotlib新建画布的默认坐标轴范围是
x: 0-1、y: 0-1,所有实体和连线都超出了可视区域,因此显示空白。
修复方案
在隐藏坐标轴代码之后、显示图表之前,添加坐标轴范围适配逻辑,两种方式二选一即可:
方式1:自动适配所有元素
添加ax.autoscale()让Matplotlib自动计算并调整坐标轴范围:
# Hide axes ax.set_axis_off() # 新增:自动适配所有绘制内容 ax.autoscale() # Show the ERD diagram plt.title("Entity Relationship Diagram (ERD) for Credit Card Company", fontsize=16) plt.show()
方式2:手动设置坐标轴范围
根据你定义的实体位置,手动设置包含所有元素的范围:
# Hide axes ax.set_axis_off() # 新增:手动设置坐标轴范围,覆盖所有实体位置 ax.set_xlim(0, 11) ax.set_ylim(0, 6) # Show the ERD diagram plt.title("Entity Relationship Diagram (ERD) for Credit Card Company", fontsize=16) plt.show()
修改后重新运行代码,就能正常显示完整的ERD图了。
内容的提问来源于stack exchange,提问作者Patterson
相关产品推荐
相关产品推荐

