最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Problems when using np.arctan2 to plot domain colored complex function - Stack Overflow

programmeradmin5浏览0评论

I'm a physics student. I'm trying to write a program to visualize complex functions in different ways using Python with matplotlib and numpy. I've written a class whose object takes as input, among other things, the real and imaginary part of a complex function, respectively fx and fy and renders a domain colored plot. The class has a __render function that uses the following code to plot the domain coloring:

x = np.linspace(self.x_min, self.x_max, self.res)
y = np.linspace(self.y_min, self.y_max, self.res)
z = np.array([(np.arctan2(self.fy(i, j, val), self.fx(i, j, val))) for i in x for j in y])

X, Y = np.meshgrid(x, y)
Z = z.reshape(self.res, self.res)
            
self.coloring = self.ax.contourf(X, Y, Z, self.res, cmap='hsv')

Where self.x_min, self.x_max, self.y_min, self.y_max represent the limits of the section of the plane plotted. self.res. val is a value that controls how smoothed out the color will be rendered. The plot is interactive and can be controlled using a slider. val is a variable, passed to fx and fy that controls the animation progress of the plotting. I'm using the following complex function as example:

def fx(x, y, k):
    return np.power(np.power(np.power(x, 2) + np.power(y, 2), 0.5), k) * np.cos(k * np.arctan2(y, x))

def fy(x, y, k):
    return np.power(np.power(np.power(x, 2) + np.power(y, 2), 0.5), k) * np.sin(k * np.arctan2(y, x))

here k has the same role that val has in the function __render. Basically this function represented:

Now the plot is the following, at different animation stages

k = 1.0

k = 1.3

k = 1.6

k = 2.0

The problem I'm encountering is in the rendering of the negative y axis, on which, the phase of the function seems to be far off the intended value. I suspect that this has something to do with the definition of np.arctan2(y, x), that seems to make the plotted function discontinous. Is it possible to get rid of this discontinuity in colors? If so, how? Another thing I can't understand: why does the function split along the negative axis? In fact, the same function plotted using conformal transformation (on the correctness of which I'm relatively sure) split along the negative x axis, not the negative y as shown below using another section of the same program. Why so?:

k = 1.0

k = 1.05

k = 1.20

k = 1.3

k = 1.6

k = 2.0

I've put some more steps of the animation to make it clear how it unwraps. Thank you very much in advance for your help and clarifications!

I'm a physics student. I'm trying to write a program to visualize complex functions in different ways using Python with matplotlib and numpy. I've written a class whose object takes as input, among other things, the real and imaginary part of a complex function, respectively fx and fy and renders a domain colored plot. The class has a __render function that uses the following code to plot the domain coloring:

x = np.linspace(self.x_min, self.x_max, self.res)
y = np.linspace(self.y_min, self.y_max, self.res)
z = np.array([(np.arctan2(self.fy(i, j, val), self.fx(i, j, val))) for i in x for j in y])

X, Y = np.meshgrid(x, y)
Z = z.reshape(self.res, self.res)
            
self.coloring = self.ax.contourf(X, Y, Z, self.res, cmap='hsv')

Where self.x_min, self.x_max, self.y_min, self.y_max represent the limits of the section of the plane plotted. self.res. val is a value that controls how smoothed out the color will be rendered. The plot is interactive and can be controlled using a slider. val is a variable, passed to fx and fy that controls the animation progress of the plotting. I'm using the following complex function as example:

def fx(x, y, k):
    return np.power(np.power(np.power(x, 2) + np.power(y, 2), 0.5), k) * np.cos(k * np.arctan2(y, x))

def fy(x, y, k):
    return np.power(np.power(np.power(x, 2) + np.power(y, 2), 0.5), k) * np.sin(k * np.arctan2(y, x))

here k has the same role that val has in the function __render. Basically this function represented:

Now the plot is the following, at different animation stages

k = 1.0

k = 1.3

k = 1.6

k = 2.0

The problem I'm encountering is in the rendering of the negative y axis, on which, the phase of the function seems to be far off the intended value. I suspect that this has something to do with the definition of np.arctan2(y, x), that seems to make the plotted function discontinous. Is it possible to get rid of this discontinuity in colors? If so, how? Another thing I can't understand: why does the function split along the negative axis? In fact, the same function plotted using conformal transformation (on the correctness of which I'm relatively sure) split along the negative x axis, not the negative y as shown below using another section of the same program. Why so?:

k = 1.0

k = 1.05

k = 1.20

k = 1.3

k = 1.6

k = 2.0

I've put some more steps of the animation to make it clear how it unwraps. Thank you very much in advance for your help and clarifications!

Share Improve this question edited Mar 17 at 19:22 Luke__ asked Mar 17 at 15:31 Luke__Luke__ 2551 silver badge15 bronze badges 2
  • 1 Did you intend to use np.arctan2 in fx, but np.angle in fy? – lastchance Commented Mar 17 at 17:41
  • I've tried both options, probably I fot to change back np.angle to np.arctan2 – Luke__ Commented Mar 17 at 19:19
Add a comment  | 

1 Answer 1

Reset to default 1

I think you have simply ordered z in the wrong way compared with how meshgrid() works by default. The easiest way to check that is simply have z return x (say) rather than arctan2(y,x); then it should correspond to x.

Change

z = np.array([(np.arctan2(self.fy(i, j, val), self.fx(i, j, val))) for i in x for j in y])

to

z = np.array([(np.arctan2(self.fy(i, j, val), self.fx(i, j, val))) for j in y for i in x])

For a complete-ish test code, without being in a class:

import numpy as np
import matplotlib.pyplot as plt
x_min, x_max, y_min, y_max = -1, 1, -1, 1
res = 100
val = 1.6

def fx(x, y, k):
    return np.power(np.power(np.power(x, 2) + np.power(y, 2), 0.5), k) * np.cos(k * np.arctan2(y, x))

def fy(x, y, k):
    return np.power(np.power(np.power(x, 2) + np.power(y, 2), 0.5), k) * np.sin(k * np.arctan2(y, x))

x = np.linspace( x_min, x_max, res )
y = np.linspace( y_min, y_max, res)
z = np.array([(np.arctan2(fy(i, j, val), fx(i, j, val))) for j in y for i in x])     # <-------------

X, Y = np.meshgrid(x, y)
Z = z.reshape( res, res )
            
fig = plt.figure( figsize = ( 6, 6 ) )
ax = fig.add_subplot( 1, 1, 1 )
coloring = ax.contourf(X, Y, Z, res, cmap='hsv')
plt.show()

With val=1.6:

发布评论

评论列表(0)

  1. 暂无评论