matplotlib 中矩形阵列的曲面图
surface plots for rectangular arrays in matplotlib
我正在尝试使用 matplotlib 生成矩形阵列的表面图(在我的例子中,它是 47x70)。这个数组的组织方式是:
47 - 这个维度表示特征的数量
70 - 这个维度表示样本数
该数组包含每个样本中这些特征的值。
如果我要在 MATLAB 或 Octave 中生成曲面图,那真的很简单。
vals = csvread("vals.csv");
surf(vals)
输出看起来像这样 -
vals.csv中的数组生成如下 -
tempvals = np.random.randint(0, 10000, size = (47, 70))
np.savetxt("vals.csv", tempvals, delimiter=',')
如何在 python/matplotlib 中执行此操作?
There is a pretty nice answer here。但是,这个答案使用了一些我无法使用的插值。我想直接绘制我的值。
我尝试写一些非常基础的东西。像这样 -
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
vals = np.genfromtxt('vals.csv', delimiter=',')
fig1 = plt.figure(1, figsize = (9, 6))
ax1 = fig1.add_subplot(111, projection = '3d')
xax = np.arange(0, 46)
yax = np.arange(0, 70)
xax, yax = np.meshgrid(yax, xax)
Axes3D.plot3D(xax, yax, vals)
这当然会失败并出现错误 -
AttributeError: 'numpy.ndarray' object has no attribute 'has_data'
我已经完成 this entire page 但我遗漏了一些东西。如何生成矩形阵列的曲面图?
我认为这会产生类似于您链接到的 surf(vals)
matlab 图的结果:matplotlib - 3d surface from a rectangular array of heights。
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
# gen random 2d array and write to csv file
tempvals = np.random.randint(0, 10000, size = (47, 70))
np.savetxt("vals.csv", tempvals, delimiter=',')
# read from csv
vals = np.genfromtxt('vals.csv', delimiter=',')
val_xdim, val_ydim = vals.shape
# generate meshgrid for plot
xax = np.arange(0, val_xdim)
yax = np.arange(0, val_ydim)
xax, yax = np.meshgrid(yax, xax)
# plot and save
fig = plt.figure()
ax = fig.gca(projection='3d')
surf = ax.plot_surface(xax, yax, vals, rstride=1, cstride=1, cmap='viridis', linewidth=0, antialiased=False)
ax.plot_wireframe(xax, yax, vals, color='k', lw=0.05, alpha=0.3)
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.savefig("rand_3d_surf.png", dpi=160)
产生:
我正在尝试使用 matplotlib 生成矩形阵列的表面图(在我的例子中,它是 47x70)。这个数组的组织方式是:
47 - 这个维度表示特征的数量
70 - 这个维度表示样本数
该数组包含每个样本中这些特征的值。
如果我要在 MATLAB 或 Octave 中生成曲面图,那真的很简单。
vals = csvread("vals.csv");
surf(vals)
输出看起来像这样 -
vals.csv中的数组生成如下 -
tempvals = np.random.randint(0, 10000, size = (47, 70))
np.savetxt("vals.csv", tempvals, delimiter=',')
如何在 python/matplotlib 中执行此操作?
There is a pretty nice answer here。但是,这个答案使用了一些我无法使用的插值。我想直接绘制我的值。
我尝试写一些非常基础的东西。像这样 -
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
vals = np.genfromtxt('vals.csv', delimiter=',')
fig1 = plt.figure(1, figsize = (9, 6))
ax1 = fig1.add_subplot(111, projection = '3d')
xax = np.arange(0, 46)
yax = np.arange(0, 70)
xax, yax = np.meshgrid(yax, xax)
Axes3D.plot3D(xax, yax, vals)
这当然会失败并出现错误 -
AttributeError: 'numpy.ndarray' object has no attribute 'has_data'
我已经完成 this entire page 但我遗漏了一些东西。如何生成矩形阵列的曲面图?
我认为这会产生类似于您链接到的 surf(vals)
matlab 图的结果:matplotlib - 3d surface from a rectangular array of heights。
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
# gen random 2d array and write to csv file
tempvals = np.random.randint(0, 10000, size = (47, 70))
np.savetxt("vals.csv", tempvals, delimiter=',')
# read from csv
vals = np.genfromtxt('vals.csv', delimiter=',')
val_xdim, val_ydim = vals.shape
# generate meshgrid for plot
xax = np.arange(0, val_xdim)
yax = np.arange(0, val_ydim)
xax, yax = np.meshgrid(yax, xax)
# plot and save
fig = plt.figure()
ax = fig.gca(projection='3d')
surf = ax.plot_surface(xax, yax, vals, rstride=1, cstride=1, cmap='viridis', linewidth=0, antialiased=False)
ax.plot_wireframe(xax, yax, vals, color='k', lw=0.05, alpha=0.3)
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.savefig("rand_3d_surf.png", dpi=160)
产生: