0

I have plotted a set of points from a data set on a scatter plot. These points represent 'hits' on a detector (with dimensions 2000 mm x 2000 mm), such that each hit represents a specific x and y coordinate on the detector corresponding to where the particle has landed. The centre of the graph is (0,0), whereas going to the left goes towards negative x values, and going to the right goes to positive x values. Up is also positive y values, and down is negative y values. I am trying to divide this plot into 7 sections, each line starting from the centre of the plot (0,0) and extending to the edge of the plot (so that R = 2000). Each line should be separated 2pi / 7 rads (or 360/7 degrees) so that the points can all be organized into these sections.

I have no idea how to start as I am relatively new with python. I've attached an image of what the graph itself looks like, as well as the code I've written for this graph. Anything helps, thank you all.

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

JohanC
  • 71,591
  • 8
  • 33
  • 66
  • Well, you can use trig functions to get an X, Y coordinate for the angle/radius combos, right? And you want to draw from the point (0, 0) in your center out to that point. You can see the answer [here](https://stackoverflow.com/questions/12864294/adding-an-arbitrary-line-to-a-matplotlib-plot-in-ipython-notebook) to see how to draw a line. All you need to do is calculate the X, Y coord that each line extends out to and draw a line from the center to there, for each angle. – alkasm Mar 01 '20 at 08:46

1 Answers1

1

Lines can be represented and drawn via a line collection. As the plot seems to have some kind of rotational symmetry, it can be useful to set an aspect ratio of 1.

An alternative to drawing lines, could be to color the dots depending on their zone.

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

JohanC
  • 71,591
  • 8
  • 33
  • 66