I have this file: https://pastebin.com/WK6K97jv (columns are respectively x,y,z=f(x,y)).
I would like to plot using matplotlib (pyplot) unfortunately I never have to deal with 3d plot and I've tried to do this:
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
fig = plt.figure()
ax = fig.gca(projection='3d')
X=np.genfromtxt('./jpdf1/jpdf1.000533048.dat',usecols=(0,))
Y=np.genfromtxt('./jpdf1/jpdf1.000533048.dat',usecols=(1,))
X, Y = np.meshgrid(X, Y)
Z=np.genfromtxt('./jpdf1/jpdf1.000533048.dat',usecols=(2,))
surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,
linewidth=0, antialiased=False)
but interpreter back me this:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/homebrew/lib/python3.9/site-packages/matplotlib/_api/deprecation.py", line 431, in wrapper
return func(*inner_args, **inner_kwargs)
File "/opt/homebrew/lib/python3.9/site-packages/mpl_toolkits/mplot3d/axes3d.py", line 1658, in plot_surface
raise ValueError("Argument Z must be 2-dimensional.")
ValueError: Argument Z must be 2-dimensional.
check the Z.shape
whether equal to X.shape
or Y.shape
.
Z.shape == X.shape
then give your a example.
# import package
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
fig = plt.figure()
ax = fig.gca(projection='3d')
# generate x and y data
X = np.arange(20) # shape: 20
Y = np.arange(30) # shape:30
X, Y = np.meshgrid(X, Y) # convert x y data shape to new shape
print(X.shape) # (20,30)
print(Y.shape) # (20, 30)
Z= np.cos(X**2 + Y**2) # shape =(20,30)
printt(Z.shape) # (20, 30)
surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,
linewidth=0, antialiased=False)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.