Search code examples
numpymultidimensional-arraymatplotlibipythonprojection

Scatter plot (2D), which shows a dotted circle and other 2D-shapes made by geometrical functions with ipython, numpy and matplotlib


I would to create an array with the "shape" (n, 2), which is creating a dotted circle, when plotted on a scatterplot.

This would be the wanted form of the array:

array([ (x1, y1),
       (x2, y2),
       (x3, y3),
       (x4, y4),
       ....
       (xN, yN),
       ])

This is how it more or less should look like:

Dotted circle

I am as well looking for ways to extrapolate this to the 3rd (sphere) and Nth dimension displaying the data set with SPLOM (scatter plot matrix) or paralell coordinates. Generally I am looking for ways to draw automatically (thus, not by hand, but maybe close to) shapes (like circles or other functions) and clusters in N-dimensions. Which would like this:

array([ (x1, y1, z1, a1, .... n1),
       (x2, y2, z2, a2, .... n2),
       (x3, y3, z3, a3, .... n3),
       (x4, y4, z4, a4, .... n4),
       ....
       (xN, yN, zN, aN, .... nN),
       ])

Solution

  • Here is a method for the circle in 2D.

    def CreateCircleArray(radius=1):
        theta = np.linspace(0, 2*np.pi, 50)
        x = radius * np.cos(theta)
        y = radius * np.sin(theta)
        return np.array([x, y]).T
    
    def PlotArray(array):
        ax = plt.subplot(111, aspect="equal")
        ax.scatter(array[:, 0], array[:, 1], color="k")
        plt.show()
    
    PlotArray(CreateCircleArray())
    

    enter image description here

    Going to 3D is a case of extending the function to include the azimuthal angle allowing you to return x, y, z and hence scatter plot in 3D.

    Past that you will need to write a function that generates the Cartesian coordinates of the n-sphere. There is an SO post that is related but I am unsure how useful it will be.

    For other shapes you will need to think about what coordinate system is appropriate, for instance defining a square in this way is difficult if its edges are parallel to the axis.

    Good luck