-1

enter image description here

The graph I want to calculate and draw is the picture above.

from matplotlib import pyplot as plt
from numpy import *

def f(z):
    return (1/z)
    
#r is radius, p is radian
#below is strange part
for r in linspace(0,1,50):
    for p in linspace(0,2*pi,50):
        Z1=array([complex(r*cos(p),r*sin(p))])
        W1 = f(Z1)
    
plt.figure(figsize=(8,8))
plt.xlim(-pi/2,pi,2);plt.ylim(-pi/2,pi/2)
for i in range(0,2499):
     plt.plot(real(W1[i]), imag(W1[i]), c='b', lw=1.5, ls='-')
    
plt.axvline(x=0,color='k', lw=1)
plt.axhline(y=0,color='k', lw=1)
plt.show()

code is a circle with a center of (0,0) and a radius of 1.

I am new to Python, so the code is still incomplete,

but something is strange, so please fix it.

Or if there is a better way, please let me know.

kosskoss
  • 29
  • 4

1 Answers1

1

Here are some edits to your code that can be a place to start.

I rewrote the for loops using numpy vectorized oprations. Also, I am not sure that setting rs to linspace is correct. Since you are trying to plot a circle, radii should be the same, so you could use np.full instead.

from matplotlib import pyplot as plt
from numpy import *

def f(z):
    return (1/z)
    
#r is radius, p is radian
# for r in linspace(0,1,50):
#     for p in linspace(0,2*pi,50):
#         Z1=array([complex(r*cos(p),r*sin(p))])
#         W1 = f(Z1)

# rewriting the above code in a vectorized style
rs = linspace(0, 1, 50)
ps = linspace(0, 2*pi, 50)
Z1 = rs * cos(ps) + 1j * rs * sin(ps)
W1 = f(Z1) 

# gussing that you want to plot a circle of radius 1 centered at (1, 0)
# in this case, rs should be constant
rs = full(50, 1.0)
Z1 = rs * cos(ps) + 1j * rs * sin(ps)
# shift the real component
Z1.real += 1
W1 = f(Z1)
    
plt.figure(figsize=(8,8))
plt.xlim(-pi/2,pi,2);plt.ylim(-pi/2,pi/2)
plt.plot(Z1.real, Z1.imag, c='b', lw=1.5, ls="-")
plt.plot(W1.real, W1.imag, c='r', lw=1.5, ls="-")
# for i in range(0,2499):
#      plt.plot(real(W1[i]), imag(W1[i]), c='b', lw=1.5, ls='-')
    
plt.axvline(x=0,color='k', lw=1)
plt.axhline(y=0,color='k', lw=1)
plt.show()
hilberts_drinking_problem
  • 11,322
  • 3
  • 22
  • 51
  • Very similar. First of all, thank you. What should I do with the inside of the circle? – kosskoss Jan 29 '22 at 09:57
  • You could look [here](https://stackoverflow.com/questions/8270981/in-a-matplotlib-plot-can-i-highlight-specific-x-value-ranges) for a way to fill a half-plane and [here](https://stackoverflow.com/questions/9215658/plot-a-circle-with-pyplot) to fill the disk (or using [this](https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.fill_between.html) with two semi-circles). – hilberts_drinking_problem Jan 29 '22 at 10:00
  • Thank you, I don't draw the area, I thought there was a way in Python to automatically calculate and display the area. – kosskoss Jan 29 '22 at 10:09
  • Do you mean displaying area on the plot or calculating the area enclosed by a curve? – hilberts_drinking_problem Jan 29 '22 at 10:14
  • yes, i calculate the area enclosed by a circle. and then result is right picture. – kosskoss Jan 29 '22 at 10:22
  • In general, there is no easy way. You could use numerical integration, e.g. [here](https://stackoverflow.com/questions/13320262/calculating-the-area-under-a-curve-given-a-set-of-coordinates-without-knowing-t). In your particular example, you could easily calculate the area analytically. You could also try using symbolic computation, [e.g.](https://docs.sympy.org/latest/modules/integrals/integrals.html#sympy.integrals.integrals.line_integrate) with Green's theorem. – hilberts_drinking_problem Jan 29 '22 at 10:29
  • 1
    Thank you very much. ```plt.plot(Z2.real, Z2.imag, 'bo') plt.plot(W2.real, W2.imag, 'ro')``` guess the area by the location of the point. – kosskoss Jan 29 '22 at 10:30