<p>对于Python中的3D图表,我已经用<code>matplotlib.pyplot</code>获得了最好的结果</p>
<pre><code>#!/usr/bin/python3
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.axes3d import Axes3D, get_test_data
from matplotlib import cm
import numpy as np
import random
X_k_list = range(1, 100, 10)
Y_p_list = [ float(x)/100.0 for x in range(1, 100, 10) ]
# set up a figure twice as wide as it is tall
fig = plt.figure(figsize=plt.figaspect(0.5))
# set up the axes for the first plot
ax = fig.add_subplot(1, 1, 1, projection='3d')
# plot a 3D surface like in the example mplot3d/surface3d_demo
X, Y = np.meshgrid(X_k_list, Y_p_list)
def critical_function(b, c):
num = random.uniform(0, 1) * 10.0
return num + (b * c)
Z_accuracy = X.copy()
Z_accuracy = Z_accuracy.astype(np.float32)
for i in range(len(X_k_list)):
for j in range(len(Y_p_list)):
Z_accuracy[j][i] = critical_function(Y_p_list[j], X_k_list[i])
surf = ax.plot_surface(X, Y, Z_accuracy,
rstride=1, cstride=1, cmap=cm.coolwarm,
linewidth=0, antialiased=False)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
fig.colorbar(surf, shrink=0.5, aspect=10)
plt.show()
</code></pre>
<p><a href="https://www.python-graph-gallery.com/371-surface-plot" rel="nofollow noreferrer">https://www.python-graph-gallery.com/371-surface-plot</a></p>
<p><a href="https://i.stack.imgur.com/7kS17.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7kS17.png" alt="enter image description here"/></a></p>
<p>您可以通过添加更多数据点来增加图表的平滑度,使用鼠标沿x、y、z轴旋转图表,还可以添加标题、图例和其他吸引眼球的内容</p>
<p><strong>matplotlib.mplot3d看起来像是欧几里德连续曲面</strong></p>
<pre><code>#!/usr/bin/python3
# -*- coding: utf-8 -*-
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
from matplotlib import cm
ax = plt.figure().add_subplot(projection='3d')
X, Y, Z = axes3d.get_test_data(0.05)
cset = ax.contour(X, Y, Z, extend3d=True, cmap=cm.coolwarm)
ax.clabel(cset, fontsize=9, inline=True)
plt.show()
</code></pre>
<p><a href="https://matplotlib.org/stable/gallery/mplot3d/contour3d_2.html#sphx-glr-gallery-mplot3d-contour3d-2-py" rel="nofollow noreferrer">https://matplotlib.org/stable/gallery/mplot3d/contour3d_2.html#sphx-glr-gallery-mplot3d-contour3d-2-py</a></p>
<p><a href="https://i.stack.imgur.com/GG9lL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GG9lL.png" alt="enter image description here"/></a></p>
<p>您正在使用matlab的<code>meshgrid(...)</code>工具生成x,y,z数据。Python可以通过将<code>numpy.meshgrid</code>馈送到<code>matplotlib.pyplot</code>中获得相同的结果</p>
<pre><code>#!/usr/bin/python3
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
def f(x, y):
return np.sin(np.sqrt(x ** 2 + y ** 2))
x = np.linspace(-6, 6, 30)
y = np.linspace(-6, 6, 30)
X, Y = np.meshgrid(x, y)
Z = f(X, Y)
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.contour3D(X, Y, Z, 50, cmap='binary')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
plt.show()
</code></pre>
<p><a href="https://jakevdp.github.io/PythonDataScienceHandbook/04.12-three-dimensional-plotting.html" rel="nofollow noreferrer">https://jakevdp.github.io/PythonDataScienceHandbook/04.12-three-dimensional-plotting.html</a></p>
<p><a href="https://i.stack.imgur.com/8gwuq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8gwuq.png" alt="enter image description here"/></a></p>