以360/7度间隔将散点图划分为多个部分

2024-05-14 08:31:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经根据散点图上的数据集绘制了一组点。这些点表示探测器上的“命中”(尺寸为2000 mm x 2000 mm),因此每次命中表示探测器上与粒子着陆位置对应的特定x和y坐标。图的中心是(0,0),而向左是负x值,向右是正x值。向上也是正y值,向下是负y值。我试图将此图分成7个部分,每条线从图的中心(0,0)开始并延伸到图的边缘(因此R=2000)。每条线应分开2pi/7 rads(或360/7度),以便将所有点组织成这些部分

我不知道如何开始,因为我对python比较陌生。我附上了一张图本身的图像,以及我为这个图编写的代码。任何有帮助的,谢谢大家

c1 = np.logical_and(np.logical_and((hitpz1 > 0), (hitdet1 == (Detector))), (hitpid1 == 11))
plt.plot(hitx1[c1].flatten(), hity1[c1].flatten(), '.', color = 'g')
plt.ylabel("Particle Y Position (mm)")
plt.xlabel("Particle X Position (mm)")
plt.title("Particle by Radial Position, (Detector 28, Elastic)")
plt.xlim(-2000,2000)
plt.ylim(-2000,2000)
plt.show()

resulting plot


Tags: and数据尺寸np绘制positionplt中心
1条回答
网友
1楼 · 发布于 2024-05-14 08:31:25

可以通过线集合来表示和绘制线。由于绘图似乎具有某种旋转对称性,因此将纵横比设置为1可能很有用

画线的另一种方法是根据点的区域给点上色

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection

# first, create some dummy test data
N = 500
theta = np.random.normal(0, 0.15, N) + np.random.randint(0, 7, N) * 2 * np.pi / 7 + np.pi
r = np.random.normal(1, 0.2, N) ** 2 * 1000
x = r * np.cos(theta)
y = r * np.sin(theta)

fig, axes = plt.subplots(ncols=2, figsize=(9, 4))

for ax in axes:
    if ax == axes[0]:
        ax.scatter(x, y, color='limegreen', s=5)
        # create and draw seven line segments
        segs = np.zeros((14, 2))
        for i in range(0, 14, 2):
            segs[i, 0] = 5000 * np.cos(2 / 7 * i * np.pi)
            segs[i, 1] = 5000 * np.sin(2 / 7 * i * np.pi)
        line_segments = LineCollection([segs], linewidths=1, colors='crimson', linestyle='solid')
        ax.add_collection(line_segments)
    else:
        # color each dot depending on its zone
        z = np.floor((theta) / (2 * np.pi) * 7)
        cmap = plt.cm.get_cmap('Set1', 7)
        ax.scatter(x, y, c=z, cmap=cmap, s=5)
    ax.set_ylabel("Particle Y Position (mm)")
    ax.set_xlabel("Particle X Position (mm)")
    ax.set_title("Particle by Radial Position")
    ax.set_xlim(-2000, 2000)
    ax.set_ylim(-2000, 2000)
    ax.set_aspect(1)
plt.tight_layout()
plt.show()

resulting plot

相关问题 更多 >

    热门问题