Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中绘制可分离3D点的最简单方法_Python_Python 2.7_Matplotlib_Mplot3d - Fatal编程技术网

在python中绘制可分离3D点的最简单方法

在python中绘制可分离3D点的最简单方法,python,python-2.7,matplotlib,mplot3d,Python,Python 2.7,Matplotlib,Mplot3d,我正在实现Perceptron 3D,在这个例子中,我有n个可分离的点,在这个例子中,蓝点和红点,我需要从我的字典中绘制它们,我尝试这样做: training_data = { '0.46,0.98,-0.43' : 'Blue', '0.66,0.24,0.0' : 'Blue', '0.35,0.01,-0.11' : 'Blue', '-0.11,0.1,0.35' : 'Red', '-0.43,-0.65,0.46' : 'Red', '0.57,-0.97,0.8' : 'Red'

我正在实现Perceptron 3D,在这个例子中,我有n个可分离的点,在这个例子中,蓝点和红点,我需要从我的字典中绘制它们,我尝试这样做:

training_data = {
'0.46,0.98,-0.43' : 'Blue',
'0.66,0.24,0.0' : 'Blue',
'0.35,0.01,-0.11' : 'Blue',
'-0.11,0.1,0.35' : 'Red',
'-0.43,-0.65,0.46' : 'Red',
'0.57,-0.97,0.8' : 'Red'
}

def get_points_of_color(data, color):
    x_coords = [point.split(",")[0] for point in data.keys() if 
data[point] == color]
    y_coords = [point.split(",")[1] for point in data.keys() if 
data[point] == color]
    z_coords = [point.split(",")[2] for point in data.keys() if 
data[point] == color]
    return x_coords, y_coords, z_coords

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# Plot blue points
x_coords, y_coords, z_coords = get_points_of_color(training_data, 'Blue')
ax.scatter(x_coords, y_coords, z_coords, 'bo')

# Plot red points
x_coords, y_coords, z_coords = get_points_of_color(training_data, 'Red')
ax.scatter(x_coords, y_coords, z_coords, 'ro')

ax.set_xlim(-1, 1)
ax.set_ylim(-1, 1)
ax.set_zlim(-1, 1)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

plt.show()
但没有成功,我收到以下错误消息:

TypeError: Cannot cast array data from dtype('float64') to dtype('S32') according to the rule 'safe'
附言:我正在使用:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

我想知道我做错了什么,以及如何正确地绘制它们。

您的坐标是字符串,即使在拆分后,您的坐标是0.46,而不是0.46。您需要将它们转换为浮点,例如float0.46==0.46

因此,在这种情况下,转换可以在列表生成内部进行:

x_coords = [float(point.split(",")[0]) for point in data.keys() if data[point] == color]

您的坐标是字符串,即使在分割之后,您的坐标也是0.46,而不是0.46。您需要将它们转换为一个float,float0.46==0.46。