Skip to main content
 首页 » 编程设计

python之为不同对象上的多个位置实例绘制 X Y 位置

2024年12月31日8yyy_WW

我正在尝试生成如下所示格式的 XY 散点图:

我的数据框 (df) 如下所示:

如果轮播编号为 1、2、3 或 4,则圆圈应为一种颜色,如果为 5 或更大,则应为不同颜色。

上面 XY 散点图中显示的圆以标称 XY 坐标为中心,其半径等于描述的 TOLPL。

到目前为止,我有一些(狡猾的)代码可以成功生成大量图形,但它只显示一个 X Y 点(循环中的最后一个点),而不是全部。

理想情况下,对于描述中的每个项目,数字将显示为 5 个横向然后向下形成一个网格。

代码是:


编辑 2018 年 9 月 12 日 15:57

  • 添加代码以使用我的数据生成示例 DataFrame。
  • 清理代码,使其成为我目前拥有的最小工作示例。

df = {'DESCRIPTION': ['Hub Bore Top', 'Hub Bore Top', 'Hub Bore Top', 'Hub Bore Top', 'Hub Bore Top', 'Hub Bore Top', 'Hub Bore Top', 'Hub Bore Top', 'View Y Top Hole 1', 'View Y Top Hole 1', 'View Y Top Hole 1', 'View Y Top Hole 1', 'View Y Top Hole 1', 'View Y Top Hole 1', 'View Y Top Hole 1', 'View Y Top Hole 1', 'View Y Top Hole 1', 'View Y Top Hole 1', 'View Y Top Hole 1', 'View Y Top Hole 1', 'View Y Top Hole 1'], 
'CAROUSEL': [1, 1, 1, 6, 6, 2, 2, 2, 6, 6, 6, 2, 2, 2, 6, 1, 1, 1, 1, 2, 6], 
'AXIS': ['Y', 'Z', 'D', 'D', 'Z', 'Y', 'Z', 'D', 'Y', 'Y', 'X', 'D', 'X', 'Y', 'Z', 'D', 'Z', 'Y', 'X', 'Z', 'D'], 
'NOMINAL': [0.000, 3.000, 85.000, 85.000, 3.000, 0.000, 3.000, 85.000, 0.000, -7.087, 94.234, 10.600, 94.234, -7.087, 11.000, 10.600, 11.000, -7.087, 94.234, 11.000, 10.600], 
'MEAS': [0.081, 3.047, 85.013, 85.013, 3.001, 0.077, 2.992, 85.001, -0.038, -7.075, 94.478, 10.456, 94.479, -7.160, 11.000, 10.466, 11.000, -7.166, 94.487, 11.000, 10.405], 
'TOLPL': [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.25, 0.25, 0.1, 0.25, 0.25, 0.1, 0.1, 0.1, 0.25, 0.25, 0.1, 0.1], 
'TOLMI': [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.25, 0.25, 0.1, 0.25, 0.25, 0.1, 0.1, 0.1, 0.25, 0.25, 0.1, 0.1] 
        } 
 
feat = df 
 
features = set(feat['DESCRIPTION'].tolist()) 
carousels = set(feat['CAROUSEL'].tolist()) 
 
for feat_idx, feature in enumerate(features):  
    feat = df 
     
    for caro_idx, carousel in enumerate(carousels): 
            # select all data for current carousel and store in feat 
            feat = feat[feat['CAROUSEL']==carousel] 
 
            feat = feat.pivot(index='DESCRIPTION', columns='AXIS', values=['MEAS', 'NOMINAL', 'TOLPL', 'TOLMI']) 
 
            if caro_idx == 0: 
                try: 
                    # store data from current feature and carousel in variables 
                    nominal_x = feat['NOMINAL'][['X']]['X'][feat_idx] 
                    nominal_y = feat['NOMINAL'][['Y']]['Y'][feat_idx] 
                    tol_rad = feat['TOLPL'][['X']]['X'][feat_idx] 
                    description = feat.index[feat_idx] 
                     
                    # generate matplotlib graph with tolerance circle 
                    fig, ax = plt.subplots(figsize=(2,2)) 
                    tol_circle = plt.Circle((nominal_x, nominal_y), tol_rad, color='grey', fill=False) 
                    ax.set_xlim((nominal_x - 4*tol_rad, nominal_x + 4*tol_rad)) 
                    ax.set_ylim((nominal_y - 4*tol_rad, nominal_y + 4*tol_rad)) 
                    ax.add_artist(tol_circle) 
                    ax.set(title=description, xlabel='x (mm)', ylabel='y (mm)') 
                    colour='r' 
                except: 
                    pass 
            # change plotted point colour if carousel number is 5 or greater 
            elif caro_idx <4: 
                colour = 'r' 
            else: 
                colour= 'b' 
             
            # get the measured x, y, and d values 
            meas_x = feat['MEAS'][['X']]['X'][feat_idx] 
            meas_y = feat['MEAS'][['Y']]['Y'][feat_idx] 
            meas_d = feat['MEAS'][['D']]['D'][feat_idx] 
             
            # create a matplotlib circle with the measured x, y, and d values and plot them on current ax.  
            plot_circle = plt.Circle((meas_x, meas_y), tol_rad/4, color=colour) 
            ax.add_artist(plot_circle) 
 
 
        

因此,作为概览,代码在描述列中创建了所有独特“功能”的列表,以及独特的轮播编号。

然后我将数据转换为特定轮播编号,为每个特征抓取值,然后绘制它。我不知道如何正确地执行此操作,这就是为什么它如此 hacky 的原因!

过去几天我一直在努力解决这个问题,非常感谢您的帮助!

请您参考如下方法:

让我们一起努力:

import matplotlib.pyplot as plt 
import numpy as np 
 
fig = plt.figure(figsize=(16, 10)) 
 
 
for k in range(1,5): 
 
    ax = fig.add_subplot(2,2,k) 
 
    ax.set_xlim((-2.0, 2.0)) 
    ax.set_ylim((-2.0, 2.0)) 
 
    x, y, z = np.random.rand(3) 
 
    tol_circle = plt.Circle((x, y), np.random.rand(), color='grey', fill=False) 
 
    ax.add_artist(tol_circle) 
 
    ax.scatter(x, y) 
 
    x, y, z = np.random.rand(3) 
 
    plot_circle = plt.Circle((x, y), z, color='red', fill=False) 
    ax.add_artist(plot_circle) 
 
    ax.scatter(x, y) 
 
plt.show() 

现在,你想做什么?