我正在尝试在python中使用matplotlib制作一个三维折线图。数据集来自具有不同传感器的测量站。现在我的代码几乎就像我想要的那样,但每次我尝试运行它都会显示出这个问题:
TypeError:根据规则'safe'
,无法将数组数据从dtype('float64')转换为dtype('S32')到目前为止我的代码:
import csv
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
depth = {
1 : 11, 2 : 35, 3 : -1, 4 : 11, #position of graphs in cm
}
class SetXYZ:
def __init__(self, datalist, NameNum): #datalist: list with data, NameNum: number of variable
self.NameNum = NameNum
self.datalist = datalist
self.Name = datalist[0][NameNum]
self.length = datalist
def X(self): #Creates list with X Variables
Xlist = []
for element in self.datalist:
Xlist.append(element[0])
Xlist.pop(0)
return Xlist
def Y(self): #Creates list with Y Variables
Ylist = []
for element in self.datalist:
Ylist.append(element[self.NameNum])
Ylist.pop(0)
return Ylist
def Z(self): #list with Z variables
Zlist = []
for element in datalist: #Z is the same for every point on one graph
Zlist.append(depth[self.NameNum-1])
Zlist.pop(0)
return Zlist
def csv_to_list(filename): #returns the file as list
with open(filename, 'rb') as data:
data = csv.reader(data, delimiter=';')
datalist = list(data)
return datalist
filename = " " #Filename
datalist = csv_to_list(filename)
Graph1 = SetXYZ(datalist, 1) #creates the graphs
Graph2 = SetXYZ(datalist, 2)
#plots a graph, more or less to test
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(Graph1.X(), Graph1.Y(), Graph1.Z())
plt.show()
文件如下所示:
时间;传感器1;传感器2;传感器3; sensor4
41940.6791666667; 16; 19.96; 4.1; 11.52
41940.67986; 15.9; 20.51; 4.07; 11.4
41940.67986; 15.9; 20.53; 4.07; 11.41
完整错误如下所示:
Traceback (most recent call last):
File "C:\Anaconda\lib\site-packages\matplotlib\backends\backend_qt5.py", line 338, in resizeEvent
self.draw()
File "C:\Anaconda\lib\site-packages\matplotlib\backends\backend_qt5agg.py", line 148, in draw
FigureCanvasAgg.draw(self)
File "C:\Anaconda\lib\site-packages\matplotlib\backends\backend_agg.py", line 461, in draw
self.figure.draw(self.renderer)
File "C:\Anaconda\lib\site-packages\matplotlib\artist.py", line 59, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "C:\Anaconda\lib\site-packages\matplotlib\figure.py", line 1079, in draw
func(*args)
File "C:\Anaconda\lib\site-packages\mpl_toolkits\mplot3d\axes3d.py", line 254, in draw
for col in self.collections]
File "C:\Anaconda\lib\site-packages\mpl_toolkits\mplot3d\art3d.py", line 413, in do_3d_projection
vxs, vys, vzs, vis = proj3d.proj_transform_clip(xs, ys, zs, renderer.M)
File "C:\Anaconda\lib\site-packages\mpl_toolkits\mplot3d\proj3d.py", line 208, in proj_transform_clip
return proj_transform_vec_clip(vec, M)
File "C:\Anaconda\lib\site-packages\mpl_toolkits\mplot3d\proj3d.py", line 165, in proj_transform_vec_clip
vecw = np.dot(M, vec)
TypeError: Cannot cast array data from dtype('float64') to dtype('S32') according to the rule 'safe'
完成后,最多可以有30个传感器,数千次。
是否有解决方法,或者我做错了什么?
感谢您的帮助!