Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a 2d mesh in python

I am really new to programming...

But here is my question :

I cannot post images but the plot I wish to have is a "crown" (two concentric circle with radius a I mean, mathematically speaking is really easy to define but how can I do it with a python program ?

I thought of something like this :

def Fm1(X, Y):
    r =r = sqrt(1.*X**2+1.*Y**2)
    cos = 1.*X/r
    sin = 1.*Y/r
    teta = where( sin >= 0. , arccos(cos) , -arccos(cos) )
    teta = where(r == 0. , 0., teta)
    return r, teta


def F(r,teta):                                                                  
    X = r*cos(teta)                                                             
    Y = r*sin(teta)                                                             
    return X,Y

Those are simply the function that let you pass from the cartesian to the polar coordinates, and then :

r=sy.linspace(a,b,N+1) # radius division
t=sy.linspace(0,2.*pi,2**NN) #angle (theta) division
R,T=meshgrid(r,t) #creating a mesh

X,Y = F(R,T)#transform from polar to cartesian

#Plotting :
fig=plt.figure()
ax=fig.add_subplot(111)                                 
ax.plot(X, Y)
plt.show()

But the result is : concentric polygons. I wish I had N+1 circles at equidistance from radius a to radius b and 2**NN lines (origin center and a given angle).

Sorry I know it's really a trivial question,

Thanks

like image 236
lasofivec Avatar asked Oct 05 '22 18:10

lasofivec


1 Answers

In my answers, I'll use two libraries:

import numpy as np
import pylab

I believe these are the constants in your setup:

r_a = 0.50
r_b = 0.75
circles = 6  
lines   = 50
origin = (0, 0)

Option 1: Plot directly

First, draw the circles:

for r in np.linspace(r_a, r_b, circles):
    pylab.gca().add_patch(pylab.Circle(origin, radius=r, 
                                       fill=False, color='black'))

Then draw the lines:

r_ab = np.array([r_a, r_b])
for theta in np.linspace(0, 2 * np.pi, lines):
    pylab.plot(np.cos(theta) * r_ab,
               np.sin(theta) * r_ab, color='red')

Finally, display:

pylab.axis('scaled')
pylab.show()

The result:

Image of plot

Option 2: Plot segments:

(After importing the libraries, and setting the constants, as above.) First, compute the point locations:

r,t   = np.meshgrid(np.linspace(r_a, r_b, circles),
                    np.linspace(0, 2 * np.pi, lines))
x = r * np.cos(t)
y = r * np.sin(t)

Then plot the circles (as you do) and plot the lines

# Plot circles
pylab.plot(x, y)
# Plot lines (first and last x and y of each theta)
pylab.plot(np.vstack((x[:,0], x[:, -1])),
           np.vstack((y[:,0], y[:, -1])))

Finally, display:

pylab.axis('scaled')
pylab.show()

The result:

Second option result

Note: After all this, I think all you really needed was the last bit in Option 2 about plotting the lines. I'll keep all this other answer stuff here for any future readers.

like image 163
Geoff Avatar answered Oct 12 '22 02:10

Geoff