[Python] Wrong color values for 3d plot in matplotlib

Discussão em 'Python' iniciado por Stack, Março 19, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to render a complex function using matplotlib. I'm trying to plot a 3d graph using (x = Re z, y = Im z, arg(f(z))). The idea was to color the faces of the plot in a way that represents mod(f(z)). I've been trying to implement the code as follows:

    import matplotlib
    from mpl_toolkits.mplot3d import Axes3D
    from matplotlib import cm
    import matplotlib.pyplot as plt

    x = np.linspace(-10, 10, 150)
    y = np.linspace(-10, 10, 150)

    X, Y = np.meshgrid(x, y)
    arg = np.arctan2(fy(X, Y), fx(X, Y))
    mod = np.sqrt(self.fx(X, Y)**2 + self.fy(X, Y)**2)

    surface = ax.plot_surface(X, Y, arg, rstride=1, cstride=1, facecolors = plt.cm.hsv(mod), linewidth=0, antialiased=False)


    Where fx and fy represent the real and imaginary part of a complex function and ax is the axis object returned from fig.add_subplot(...) (fig = plt.figure(...)). This code won't display the correct colors. Plotting f(z) = z^2, results in the following plot:

    [​IMG]

    whose colors do not represent mod(f(z)) I suspect. In fact, I've tried to test how the color mapping mechanism works, by simply sobsituting plt.cm.hsv(mod) with plt.cm.hsv(X), and the output is the following (using f(x) = x and f(y) = y):

    [​IMG]

    it is not what I expect to be plotted at all. I would have expected a sort of "raimbow" of colors whose line of "constant color" are perpendicular to the x direction. I cannot figure out what is wrong with this plot. What am I doing wrong?

    Thank you very much in advance for your advice!

    Continue reading...

Compartilhe esta Página