我有以下代码在matplotlib中制作3D散点图
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
def plot_scores_3d(lambdas, Ts, scores, title):
"""
Plot scores (accuracy) as a function of lambda and no. iterations
"""
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(lambdas, Ts, scores, c='r', marker='o')
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
plt.show()
我有
x = [1, 1, 1, 1, 1, 10, 10, 10, 10, 10, 20, 20, 20, 20, 20, 50, 50, 50, 50, 50, 100, 100, 100, 100, 100]
y = [1, 5, 10, 15, 20, 1, 5, 10, 15, 20, 1, 5, 10, 15, 20, 1, 5, 10, 15, 20, 1, 5, 10, 15, 20]
z = [74.44444444444444, 73.33333333333333, 93.88888888888889, 94.22222222222223, 93.94444444444444, 74.44444444444444, 73.33333333333333, 93.88888888888889, 94.22222222222223, 93.94444444444444, 74.44444444444444, 73.33333333333333, 93.88888888888889, 94.22222222222223, 93.94444444444444, 74.44444444444444, 73.33333333333333, 93.88888888888889, 94.22222222222223, 93.94444444444444, 74.44444444444444, 73.33333333333333, 93.88888888888889, 94.22222222222223, 93.94444444444444]
并拨打电话
plot_scores_3d(x, y, z, 'Test')
但获得以下堆栈跟踪
/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/mpl_toolkits/mplot3d/axes3d.pyc in scatter(self, xs, ys, zs, zdir, s, c, depthshade, *args, **kwargs)
2238 xs, ys, zs, s, c = cbook.delete_masked_points(xs, ys, zs, s, c)
2239
-> 2240 patches = Axes.scatter(self, xs, ys, s=s, c=c, *args, **kwargs)
2241 if not cbook.iterable(zs):
2242 is_2d = True
/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib/axes/_axes.pyc in scatter(self, x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, verts, **kwargs)
3644 linewidths=linewidths,
3645 offsets=offsets,
-> 3646 transOffset=kwargs.pop('transform', self.transData),
3647 )
3648 collection.set_transform(mtransforms.IdentityTransform())
/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib/collections.pyc in __init__(self, paths, sizes, **kwargs)
767 Collection.__init__(self, **kwargs)
768 self.set_paths(paths)
--> 769 self.set_sizes(sizes)
770
771 def set_paths(self, paths):
/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib/collections.pyc in set_sizes(self, sizes, dpi)
741 self._sizes = np.asarray(sizes)
742 self._transforms = np.zeros((len(self._sizes), 3, 3))
--> 743 scale = np.sqrt(self._sizes) * dpi / 72.0
744 self._transforms[:, 0, 0] = scale
745 self._transforms[:, 1, 1] = scale
TypeError: Not implemented for this type
所有输入数组都是相同的大小,因此不是问题。我尝试再次绘图,z是一个整数数组,但得到了同样的错误。非常感谢帮助!
答案 0 :(得分:0)
原来这个问题与Canopy中的Python内核有关。重新启动内核摆脱了错误。