import pandas as pd import matplotlib.pyplot as plt from matplotlib.patches import Rectangle def col(char): return ord(char) - ord('A') + 0 # 读取Excel文件,指定表格路径 file_path = '预埋件位置.xlsx' # 使用pandas读取Excel df = pd.read_excel(file_path, sheet_name='测量结果') # 根据你的Excel表格来设置sheet_name # 打印列名 print("DataFrame 列名:", df.columns) # 创建一个图形和坐标轴 fig, ax = plt.subplots() # 初始化边界变量 x_min, x_max = float('inf'), float('-inf') y_min, y_max = float('inf'), float('-inf') x_max_original = 0 for index, row in df.iloc[2:30].iterrows(): original_x = row[col('K')] - row[col('D')] / 2 if(original_x > x_max_original): x_max_original = original_x for index, row in df.iloc[2:30].iterrows(): # 打印B列和C列的值 print(f"行 {index+2} -> A列-编号: {row[col('A')]}, D列-测量宽度: {row[col('D')]}, E列-测量高度: {row[col('E')]},K列-测量中心x: {row[col('K')]}, L列-测量中心y: {row[col('L')]}") # 转换x坐标 x_original = row[col('K')] x = x_max_original - x_original - row[col('D')] / 2 y = row[col('L')] - row[col('E')] / 2 # 更新边界 x_min = min(x_min, x) x_max = max(x_max, x + row[col('D')]) y_min = min(y_min, y) y_max = max(y_max, y + row[col('E')]) # 绘制矩形 rect = Rectangle((x, y), row[col('D')], row[col('E')], linewidth=1, edgecolor='r', facecolor='none') ax.add_patch(rect) # 在矩形中心绘制编号(A列) ax.text( x + row[col('D')] / 2, y + row[col('E')] / 2, str(row[col('A')]), ha='center', va='center', fontsize=12, color='blue' # 编号字体大小为12,颜色为蓝色 ) # 在矩形上边绘制宽度(D列) ax.text( x + row[col('D')] / 2, y + row[col('E')], str(row[col('D')]), ha='center', va='bottom', fontsize=6, color='green' # 宽度字体大小为10,颜色为绿色 ) # 在矩形右边绘制高度(E列) ax.text( x + row[col('D')], y + row[col('E')] / 2, str(row[col('E')]), ha='left', va='center', fontsize=6, color='red' # 高度字体大小为10,颜色为红色 ) # 设置坐标轴的范围,外扩100左右 ax.set_xlim(x_min, x_max ) # 留出一些边距 ax.set_ylim(y_min, y_max ) # 留出一些边距 # 设置坐标轴的比例 ax.set_aspect('equal') # 显示图形 plt.show()