5

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

5
  • You use some functions I'm not familiar with. What are where, ones, Fcm1, meshgrid, and Fc? Commented Dec 5, 2012 at 15:40
  • Also, I'm having difficulty imagining the shape you're trying to draw. Wouldn't the outer and inner circles have different radiuses (radii?)? So wouldn't you need more than just a to define a crown? Commented Dec 5, 2012 at 15:52
  • @Kevin most of those are numpy functions, and I think the rest are typos. Commented Dec 5, 2012 at 16:29
  • I changed the code to what I first thought I should do. It is cleaner that way. Sorry. And yes you need two radius. Commented Dec 5, 2012 at 16:40
  • I added a tag for matplotlib and its subset functions pylab Commented Dec 5, 2012 at 18:12

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Almost ! the lines should start at the intersection with the circle with radius r_a. So for the lines it would be something like this : for theta in np.linspace(0, 2 * np.pi, lines): pylab.plot(( np.cos(theta) * r_a , np.cos(theta) * r), ( np.sin(theta) * r_a, np.sin(theta) * r), 'red')
But my main problem is not to actually make the figure but to have a mesh with those points (so I guess the matrix I described in my post would be alright) and to verify which points I had I wanted to have the graphic that you showed ! So is it possible that from a matrix (with all the grid points) have a figure like yours above ?
I updated my picture, and code to show the wheel as you indicated in your first comment. From your second comment, I'm still not clear on what you want. You're correctly sub-sampling a bunch of concentric circles. Is the problem that the line-segments connecting your points are straight, and not fit to the circle? Plotting a mesh will always result in straight lines.
This is exactly what I needed thank you ! :) As the Jacobian matrix of the transformation has a singular point in the origin I needed to have a grid with 2 patches so this is the external patch. I now have my running program with both patches so thank you !
Great. Glad it's what you needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.