ValueError: Argument Z must be 2-dimensional - python

I want to plot the number of spherical wavefronts I input using Python, this is my code.I'm using Axes3D to plot, but I have a problem, when I plot with "ax.plot_surface(x, y, z, facecolors=plt.cm.coolwarm(w), shade=False)" doesn´t work and appears me the next message:
ValueError: Argument Z must be 2-dimensional.
My code is the following:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
def spherical_wavefront(n):
x, y, z = np.meshgrid(np.linspace(-1, 1, n), np.linspace(-1, 1, n), np.linspace(-1, 1, n))
r = np.sqrt(x**2 + y**2 + z**2)
wavefront = np.empty((n, n, n))
wavefront[r <= 1] = 1/r[r <= 1] * np.exp(1j * r[r <= 1])
return x, y, z, np.real(wavefront)
num_wavefronts = int(input("How many spherical wavefronts would you like to plot? "))
fig = plt.figure()
for i in range(num_wavefronts):
x, y, z, w = spherical_wavefront(100)
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(x, y, z, facecolors=plt.cm.coolwarm(w), shade=False)
ax.set_title(f'Spherical Wavefront {i + 1}')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.show()

Related

Colormap a 3D curve in matplotlib

I have 4 arrays x, y, z and T of length n and I want to plot a 3D curve using matplotlib. The (x, y, z) are the points positions and T is the value of each point (which is plotted as color), like the temperature of each point. How can I do it?
Example code:
import numpy as np
from matplotlib import pyplot as plt
fig = plt.figure()
ax = fig.gca(projection='3d')
n = 100
cmap = plt.get_cmap("bwr")
theta = np.linspace(-4 * np.pi, 4 * np.pi, n)
z = np.linspace(-2, 2, n)
r = z**2 + 1
x = r * np.sin(theta)
y = r * np.cos(theta)
T = (2*np.random.rand(n) - 1) # All the values are in [-1, 1]
What I found over the internet:
It's possible to use cmap with scatter like shown in the docs and in this stackoverflow question
ax = plt.gca()
ax.scatter(x, y, z, cmap=cmap, c=T)
The problem is that scatter is a set of points, not a curve.
In this stackoverflow question the solution was divide in n-1 intervals and each interval we use a different color like
t = (T - np.min(T))/(np.max(T)-np.min(T)) # Normalize
for i in range(n-1):
plt.plot(x[i:i+2], y[i:i+2], z[i:i+2], c=cmap(t[i])
The problem is that each segment has only one color, but it should be an gradient. The last value is not even used.
Useful links:
Matplotlib - Colormaps
Matplotlib - Tutorial 3D
This is a case where you probably need to use Line3DCollection. This is the recipe:
create segments from your array of coordinates.
create a Line3DCollection object.
add that collection to the axis.
set the axis limits.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.art3d import Line3DCollection
from matplotlib.cm import ScalarMappable
from matplotlib.colors import Normalize
def get_segments(x, y, z):
"""Convert lists of coordinates to a list of segments to be used
with Matplotlib's Line3DCollection.
"""
points = np.ma.array((x, y, z)).T.reshape(-1, 1, 3)
return np.ma.concatenate([points[:-1], points[1:]], axis=1)
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
n = 100
cmap = plt.get_cmap("bwr")
theta = np.linspace(-4 * np.pi, 4 * np.pi, n)
z = np.linspace(-2, 2, n)
r = z**2 + 1
x = r * np.sin(theta)
y = r * np.cos(theta)
T = np.cos(theta)
segments = get_segments(x, y, z)
c = Line3DCollection(segments, cmap=cmap, array=T)
ax.add_collection(c)
fig.colorbar(c)
ax.set_xlim(x.min(), x.max())
ax.set_ylim(y.min(), y.max())
ax.set_zlim(z.min(), z.max())
plt.show()

How to plot a one to many function on matplotlib in python

Very simple, if I plot x^2+y^2=z it makes this shape on python it will make this shape:
When I would like to plot it this way:
Below is my code, I am new so I copied it from the internet and have changed the line with the function to plot.
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-4*np.pi,4*np.pi,50)
y = np.linspace(-4*np.pi,4*np.pi,50)
z = x**2+y**2
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot(x,y,z)
plt.show()
Also, how do I make it more high definition and smooth, this is a graph of z=sin(x)
You need to define a 2D mathematical domain with numpy.meshgrid, then you can compute the surface on that domain:
X, Y = np.meshgrid(x, y)
Z = X**2 + Y**2
In order to increase the smoothness of the surface, you have in increase the number of point N you use to compute x and y arrays:
Complete code
import matplotlib.pyplot as plt
import numpy as np
N = 50
x = np.linspace(-4*np.pi, 4*np.pi, N)
y = np.linspace(-4*np.pi, 4*np.pi, N)
X, Y = np.meshgrid(x, y)
Z = X**2 + Y**2
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z)
plt.show()

Overlapping surfaces with Matplotlib

Basically I have two graphs and I want to plot them both without overlapping one over the other.
from matplotlib import cm
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
X = np.arange(0, 5, 0.05)
Y = np.arange(0, 5, 0.05)
X, Y = np.meshgrid(X, Y)
Z = (np.sin(X) / X) + 2
X1 = np.arange(0, 5, 0.05)
Y1 = np.arange(0, 5, 0.05)
X1, Y1 = np.meshgrid(X1, Y1)
Z1 = (X / X) + 1
ax.plot_surface(X, Y, Z, alpha = 1, rstride=10, cstride=10, cmap=cm.autumn,linewidth=0.5, antialiased=True, zorder = 0.3)
ax.plot_surface(X, Y, Z1, alpha = 1, rstride=10, cstride=10, cmap=cm.winter, linewidth=0.5, antialiased=True, zorder = 0.5)
plt.show()
We can see here that we have two graphs However when viewed at 90 degrees
Why does this happen and how to proceed?

Wire_frame in matplotlib 1.5 doesn't plot correctly

I am trying to plot a wireframe using plot_wireframe from the Axes3D module. My dataset: three 2D arrays: two created with np.arange and then np.meshgrid. The third one is an array containing results of function "f2" (f2 = f(x, y)). I expected 3D plot, but result is 2D plot in 3D space. Now I made some code that looks like a part of documantation of mpl, but still doesn't work. Code:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
def f2(x, y):
'''Return f(x, y) = exp(-(x * x + y * y)) * sin(-5. * (x * x + y * y))'''
return np.exp(-(x**2 + y**2)) * np.sin(-5.0 * (x**2 + y**2))
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x2 = np.arange(-1.5, 1.5, 0.02)
y2 = np.arange(1.5, -1.5, -0.02)
X, Y = np.meshgrid(x2, y2)
z2 = f2(X, Y)
ax.plot_wireframe(X, X, z2, rstride=10, cstride=10, linewidth=0.1,
label='$\exp(-(x^2 + y^2)){sin}(-5.0(x^2 + y^2)$')
plt.show()
That is what I want:
Beautiful 3D plot
But the reality is cruel:
"A plot I've got"
I have no idea what I'm doing wrong. Is it a problem with values of vectors?
Change
ax.plot_wireframe(X, X, z2, rstride=10, cstride=10, linewidth=0.1,
label='$\exp(-(x^2 + y^2)){sin}(-5.0(x^2 + y^2)$')
to
ax.plot_wireframe(X, Y, z2, rstride=10, cstride=10, linewidth=0.1,
label='$\exp(-(x^2 + y^2)){sin}(-5.0(x^2 + y^2)$')
You are using X instead of Y as the second argument to ax.plot_wireframe. Thus, all points fall on top of the diagonal Y=X.

Plot surface python with different width in mesh

I' m trying to plot a 3d surface with python in fact i have this code:
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
import matplotlib.pyplot as plt
from numpy import *
def f(x,y):
r=x**2 + y**2
return r
n=4.
b=1.
a=-b
h=(2*b)/n
print h
hx=h ##This line##
fig = plt.figure()
ax = Axes3D(fig)
X = arange(a, b+hx, hx)
Y = arange(a, b+h, h)
n = len(X)
m = len(Y)
Z = zeros([n,m])
for i in arange(n):
for j in arange(m):
Z[i,j] = f(X[i],Y[j])
X, Y = meshgrid(X, Y)
ax.plot_surface(Y, X, Z, rstride=1, cstride=1, cmap=cm.jet)
ax.set_xlabel("X Axis")
ax.set_ylabel("Y Axis")
ax.set_zlabel("Z Axis")
plt.show()
This runs Ok and show me the graph I am looking. But when I change ##This line## into hx=h/2. And run it, the graph goes to hell, it's horrible and impossible to understand. I want to have a closer grid in X than Y axis. How I can do this??
Of course this is an example I am solving a partial differential equation, and i need to have a grid closer in one axis than the other one to have numerical estability.
You have flipped your dimensions
Z = zeros([m,n])
for i in arange(n):
for j in arange(m):
Z[j,i] = f(X[i],Y[j])
X, Y = meshgrid(X, Y)
works for any ratio of n to m.
With the function you have, you can use numpy's broadcasting and write this whole section as
X, Y = meshgrid(X, Y)
Z = f(X,Y)
which is both easier to read and faster.
I would re-write this whole block of code as:
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
import matplotlib.pyplot as plt
from numpy import *
def f(x,y):
r=x**2 + y**2
return r
n = 5
m = 10
b = 1.
a = -b
fig = plt.figure()
ax = Axes3D(fig)
X = linspace(a,b,n)
Y = linspace(a,b,m)
X, Y = meshgrid(X, Y)
Z = f(X,Y)
ax.plot_surface(Y, X, Z, rstride=1, cstride=1, cmap=cm.jet)
ax.set_xlabel("X Axis")
ax.set_ylabel("Y Axis")
ax.set_zlabel("Z Axis")
plt.show()

Categories